From ec6872aa0f5023ff184cee2663b4f68a83b2ed5b Mon Sep 17 00:00:00 2001 From: furkansimsekli Date: Thu, 5 Sep 2024 03:24:29 +0300 Subject: [PATCH] fix: dont let telegram timed out error to interrupt mass message sending I will be testing this in production because I'm not 100% sure that Telegram actually delivers the message to the user. Relevant stackoverflow thread: https://stackoverflow.com/questions/68390839/timedout-in-python-telegram-bot-but-message-is-sent I hope there isn't any ip blocking or whatsoever. --- src/handler.py | 9 +++++++++ src/task.py | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/handler.py b/src/handler.py index c816470..2c95d14 100644 --- a/src/handler.py +++ b/src/handler.py @@ -338,6 +338,15 @@ async def admin_announcement_done(update: Update, context: ContextTypes.DEFAULT_ logger.info(f"Admin message has been sent to {target['user_id']}") except telegram.error.Forbidden: logger.info(f"FORBIDDEN: Admin message couldn't be delivered to {target['user_id']}") + except telegram.error.BadRequest: + logger.info(f"BAD REQUEST: Message couldn't be delivered to {user['user_id']}") + continue + except telegram.error.TimedOut: + # Message might be still sent to the user even if Telegram doesn't return a response + # That's why I'm not trying to resend the message. If this is not the case FIXME. + logger.info(f"WARNING: Telegram didn't return a response in time while sending a message to " + f"{user['user_id']}") + continue message = f"{decode('admin-announcement-successful', language)}" await context.bot.send_message(chat_id=user_id, text=message, reply_markup=ReplyKeyboardRemove()) diff --git a/src/task.py b/src/task.py index 0a7c2e5..ad0d2a7 100644 --- a/src/task.py +++ b/src/task.py @@ -60,7 +60,13 @@ async def notify_users(context: ContextTypes.DEFAULT_TYPE, announcement: dict, u logger.info(f"FORBIDDEN: Message couldn't be delivered to {user['user_id']}") continue except telegram.error.BadRequest: - logger.info(f"FORBIDDEN: Message couldn't be delivered to {user['user_id']}") + logger.info(f"BAD REQUEST: Message couldn't be delivered to {user['user_id']}") + continue + except telegram.error.TimedOut: + # Message might be still sent to the user even if Telegram doesn't return a response + # That's why I'm not trying to resend the message. If this is not the case FIXME. + logger.info(f"WARNING: Telegram didn't return a response in time while sending a message to " + f"{user['user_id']}") continue