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

This commit is contained in:
Alexey Skobkin 2022-05-30 23:22:01 +03:00
parent 3bffd32df0
commit f9ffdac38a
1 changed files with 14 additions and 3 deletions

View File

@ -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(