Skip to content

Commit

Permalink
fix: dont let telegram timed out error to interrupt mass message sending
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
furkansimsekli committed Sep 5, 2024
1 parent 479503f commit ec6872a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 7 additions & 1 deletion src/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit ec6872a

Please sign in to comment.