#7 Notifier is now considering Telegram API sendMessage() rate limit.
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Alexey Skobkin 2022-05-30 23:22:01 +03:00
parent 3bffd32df0
commit f9ffdac38a

View file

@ -1,3 +1,5 @@
import time
from telebot import TeleBot from telebot import TeleBot
from telebot.handler_backends import BaseMiddleware from telebot.handler_backends import BaseMiddleware
from telebot.types import Message from telebot.types import Message
@ -88,13 +90,22 @@ class CommandProcessor:
class Notifier: class Notifier:
"""Sends notifications to users about new RSS feed items.""" """Sends notifications to users about new RSS feed items."""
BATCH_LIMIT: int = 30
sent_counter: int = 0
def __init__(self, token: str): def __init__(self, token: str):
self.bot: TeleBot = TeleBot(token) 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""" """Send notification about new items to the user"""
for update in updates: for chat_id in chat_ids:
self.__send_update(chat_id, update) 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): def __send_update(self, telegram_id: int, update: FeedItem):
self.bot.send_message( self.bot.send_message(