From 1718d98e2eb1793969ee03ed78d46577ebfc9929 Mon Sep 17 00:00:00 2001 From: lavadk Date: Tue, 18 May 2021 18:34:15 +0300 Subject: [PATCH] error handling improved, config_example improved --- data/config_example.json | 2 +- liker/command/handler_take_message.py | 33 +++++++++++++--------- liker/custom_markup/markup_synchronizer.py | 8 ++++-- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/data/config_example.json b/data/config_example.json index dce33b8..bb6a8bb 100644 --- a/data/config_example.json +++ b/data/config_example.json @@ -24,7 +24,7 @@ "reply_markup_trail": 100, "comment_trail": 100, "channel_state_save_seconds": 2, - "response_start": "Liker bot allows you to add reactions (likes, etc.) to channel posts. Give Liker bot an admin permission to edit posts in your channel.", + "response_start": "Liker bot allows you to add reactions (likes, etc.) to channel posts. Give Liker bot an admin permission to edit posts in your channel. Use /help to get more information.", "response_help": "Give Liker bot an admin permission to edit posts in your channel. Use /set_reactions command to customize channel post reactions", "response_reaction_added": "{}", "response_reaction_removed": "{} removed", diff --git a/liker/command/handler_take_message.py b/liker/command/handler_take_message.py index 75930c6..1256046 100644 --- a/liker/command/handler_take_message.py +++ b/liker/command/handler_take_message.py @@ -36,9 +36,16 @@ def handle(self, context: CommandContext): from_message_id = context.get_mandatory_arg('message_id') n_backward_messages = context.get_optional_arg('n', default=1) - arr_messages = self.telegram_api.get_chat_messages_backward(chat_id=channel_id, - message_id=from_message_id, - n_messages=n_backward_messages) + try: + arr_messages = self.telegram_api.get_chat_messages_backward(chat_id=channel_id, + message_id=from_message_id, + n_messages=n_backward_messages) + except ValueError as ex: + # Error like "Could not find the input entity for PeerChannel(channel_id=1322520409)", most likely + # means the user wasn't added to the channel or channel doesn't exist + context.reply(text=str(ex), log_level=logging.INFO) + return + n_messages = len(arr_messages) period = 60 / context.config['channel_rate_per_minute'] response_text = f'There are {n_messages:,} messages, will take approximately {n_messages * period:,.0f} ' \ @@ -76,23 +83,21 @@ def handle(self, context: CommandContext): logger.debug(f'Sleeping {iteration_remaining:.2f}') time.sleep(iteration_remaining) except ApiTelegramException as ex: - logger.exception(ex) if ex.error_code == telegram_error.TOO_MANY_REQUESTS: logging.warning(ex) time.sleep(10) + elif ex.error_code == telegram_error.UNAUTHORIZED: + logging.info(str(ex)) + context.reply('Bot has no rights to perform the operation') + return + elif ex.error_code == telegram_error.BAD_REQUEST: + logging.info(str(ex)) + context.reply(f'Message {msg.id} was deleted or not yet posted') else: raise ex except Exception as ex: - try: - context.reply(f'Error processing message {msg.id}: {str(ex)}', - log_level=logging.ERROR) - except ApiTelegramException as ex: - logger.exception(ex) - if ex.error_code == telegram_error.TOO_MANY_REQUESTS: - logging.warning(ex) - time.sleep(10) - else: - raise ex + logger.exception(ex) + context.reply(f'Error processing message {msg.id}: {str(ex)}') context.reply(f'for {channel_id} {n_messages} message(s) were taken', log_level=logging.INFO) diff --git a/liker/custom_markup/markup_synchronizer.py b/liker/custom_markup/markup_synchronizer.py index 6dc3f46..47d3305 100644 --- a/liker/custom_markup/markup_synchronizer.py +++ b/liker/custom_markup/markup_synchronizer.py @@ -105,10 +105,14 @@ def update(self): if ex.error_code == telegram_error.TOO_MANY_REQUESTS: logger.error(f'Got TOO_MANY_REQUESTS error, will skip current channel update: {ex}') break + elif (ex.error_code == telegram_error.BAD_REQUEST) and ('are exactly the same' in str(ex)): + # Error: Bad Request: message is not modified: specified new message content and reply + # markup are exactly the same as a current content and reply markup of the message" + logger.warning(str(ex)) else: - logger.exception(ex) + logger.exception(f'Chat {ch_id}, message {m_id_str}\n{ex}') except Exception as ex: - logger.exception(ex) + logger.exception(f'Chat {ch_id}, message {m_id_str}\n{ex}') # We delete markup from the queue only after it's synchronized if m_id_str is not None: