Skip to content

Commit

Permalink
error handling improved, config_example improved
Browse files Browse the repository at this point in the history
  • Loading branch information
lavadk committed May 18, 2021
1 parent 3fa02ea commit 1718d98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion data/config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 19 additions & 14 deletions liker/command/handler_take_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} ' \
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions liker/custom_markup/markup_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1718d98

Please sign in to comment.