From 8a4bec01c164abc7f2aba34bc6b5af50dccb6b13 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Fri, 8 Jul 2022 21:49:13 +0000 Subject: [PATCH] refactor send_updates method in Notifier class and add __register_request_and_wait method --- telegram.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/telegram.py b/telegram.py index 98bfd4f..e8ddc0a 100644 --- a/telegram.py +++ b/telegram.py @@ -90,7 +90,7 @@ class CommandProcessor: class Notifier: """Sends notifications to users about new RSS feed items.""" - BATCH_LIMIT: int = 29 + BATCH_LIMIT: int = 30 sent_counter: int = 0 @@ -99,7 +99,11 @@ class Notifier: def send_updates(self, chat_ids: list[int], updates: list[FeedItem], feed_title: str): """Send notification about new items to the user""" + if not updates: + return + for chat_id in chat_ids: + self.__register_request_and_wait() self.bot.send_message( chat_id=chat_id, text=f'Updates from the {feed_title} feed:' @@ -107,12 +111,9 @@ class Notifier: self.sent_counter += 1 for update in updates: + self.__register_request_and_wait() self.__send_update(chat_id, update) self.sent_counter += 1 - if self.sent_counter >= self.BATCH_LIMIT: - # TODO: probably implement better later - time.sleep(1) - self.sent_counter = 0 def __send_update(self, chat_id: int, update: FeedItem): self.bot.send_message( @@ -121,6 +122,12 @@ class Notifier: parse_mode='HTML' ) + def __register_request_and_wait(self): + if self.sent_counter >= self.BATCH_LIMIT: + # TODO: probably implement better later + time.sleep(1) + self.sent_counter = 0 + @staticmethod def __format_message(item: FeedItem) -> str: return (