From fb075394d76f2633e7e4b13a673dcbe0db2ae0b9 Mon Sep 17 00:00:00 2001 From: Guillermo Serrahima Date: Mon, 2 Feb 2026 16:20:15 +0100 Subject: [PATCH] Added rudimentary error catches for sending messages --- velascobot2/bot.py | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/velascobot2/bot.py b/velascobot2/bot.py index 2a997ff..19ec090 100644 --- a/velascobot2/bot.py +++ b/velascobot2/bot.py @@ -344,7 +344,11 @@ class Bot: logger.info( "Sending a {} to {}: '{}'".format(msg, chat_id, content) ) - await bot.send_message(chat_id, content, **kwargs) + try: + await bot.send_message(chat_id, content, **kwargs) + except (telegram.error.Forbidden, telegram.error.BadRequest, telegram.error.NetworkError) as e: + logger.error(str(e)) + else: assert isinstance(content, tuple) graph_type, graph_id = content @@ -355,21 +359,26 @@ class Bot: 'Sending {} "{}" to {}'.format(graph_type, graph_id, chat_id) ) - if graph_type == GraphicType.STICKER: - sent = await bot.send_sticker( - chat_id=chat_id, sticker=graph_id, **kwargs - ) - elif graph_type == GraphicType.ANIMATION: - sent = await bot.send_animation( - chat_id=chat_id, animation=graph_id, **kwargs - ) - elif graph_type == GraphicType.VIDEO: - sent = await bot.send_video( - chat_id=chat_id, video=graph_id, **kwargs - ) - else: - raise Exception("Sending unkown graphic type") - logger.debug(f"Sent graphic: {sent}") + try: + if graph_type == GraphicType.STICKER: + sent = await bot.send_sticker( + chat_id=chat_id, sticker=graph_id, **kwargs + ) + elif graph_type == GraphicType.ANIMATION: + sent = await bot.send_animation( + chat_id=chat_id, animation=graph_id, **kwargs + ) + elif graph_type == GraphicType.VIDEO: + sent = await bot.send_video( + chat_id=chat_id, video=graph_id, **kwargs + ) + else: + raise Exception("Sending unkown graphic type") + logger.debug(f"Sent graphic: {sent}") + except (telegram.error.Forbidden, telegram.error.BadRequest, telegram.error.NetworkError) as e: + logger.error(str(e)) + except Exception as e: + raise e async def announce(self, announcement, check=(lambda _: True)) -> None: """Sends an announcement to all chats that pass a check.""" @@ -601,4 +610,7 @@ class Bot: f"{chat_data.answer_chance}.\n\nThis chat is {permissions}." ) - await msg.reply_markdown(answer) + try: + await msg.reply_markdown(answer) + except Exception: + pass -- 2.51.2