From f9ffdac38a6baf56cff1892be5beea0cdbff8aa0 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Mon, 30 May 2022 23:22:01 +0300 Subject: [PATCH] #7 Notifier is now considering Telegram API sendMessage() rate limit. --- telegram.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/telegram.py b/telegram.py index 38646b7..6b909db 100644 --- a/telegram.py +++ b/telegram.py @@ -1,3 +1,5 @@ +import time + from telebot import TeleBot from telebot.handler_backends import BaseMiddleware from telebot.types import Message @@ -88,13 +90,22 @@ class CommandProcessor: class Notifier: """Sends notifications to users about new RSS feed items.""" + BATCH_LIMIT: int = 30 + + sent_counter: int = 0 + def __init__(self, token: str): self.bot: TeleBot = TeleBot(token) - def send_updates(self, chat_id: int, updates: list[FeedItem]): + def send_updates(self, chat_ids: list[int], updates: list[FeedItem]): """Send notification about new items to the user""" - for update in updates: - self.__send_update(chat_id, update) + for chat_id in chat_ids: + for update in updates: + 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) def __send_update(self, telegram_id: int, update: FeedItem): self.bot.send_message(