2022-05-30 20:54:26 +00:00
|
|
|
import time
|
2022-07-10 12:07:26 +00:00
|
|
|
from logging import Logger
|
2022-07-10 14:40:19 +00:00
|
|
|
from bleach.sanitizer import Cleaner
|
2022-05-30 20:54:26 +00:00
|
|
|
from telebot import TeleBot
|
|
|
|
from telebot.handler_backends import BaseMiddleware
|
|
|
|
from telebot.types import Message
|
|
|
|
import validators
|
|
|
|
|
|
|
|
from database import Database
|
|
|
|
from exceptions import DisplayableException
|
|
|
|
from rss import FeedItem
|
|
|
|
|
|
|
|
|
|
|
|
class CommandProcessor:
|
|
|
|
"""Processes user input and dispatches the data to other services."""
|
|
|
|
|
2022-07-10 12:07:26 +00:00
|
|
|
def __init__(self, token: str, database: Database, logger: Logger):
|
|
|
|
self.log = logger
|
|
|
|
self.log.debug(
|
|
|
|
'CommandProcessor.__init__(token=\'%s\', database=%s, logger=%s)', token[:8] + '...', database, logger
|
|
|
|
)
|
2022-05-30 20:54:26 +00:00
|
|
|
if token is None or len(token) == 0:
|
|
|
|
raise ValueError("Token should not be empty")
|
|
|
|
self.bot: TeleBot = TeleBot(token, use_class_middlewares=True)
|
2022-07-10 12:07:26 +00:00
|
|
|
self.bot.setup_middleware(UserAuthMiddleware(database, logger))
|
|
|
|
self.bot.setup_middleware(ExceptionHandlerMiddleware(self.bot, logger))
|
2022-05-30 20:54:26 +00:00
|
|
|
self.database: Database = database
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Run a bot and poll for new messages indefinitely."""
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('Registering handlers')
|
2022-05-30 20:54:26 +00:00
|
|
|
self.bot.register_message_handler(commands=['add'], callback=self.__add_feed)
|
|
|
|
self.bot.register_message_handler(commands=['list'], callback=self.__list_feeds)
|
|
|
|
self.bot.register_message_handler(commands=['del'], callback=self.__delete_feed)
|
|
|
|
self.bot.register_message_handler(commands=['help', 'start'], callback=self.__command_help)
|
|
|
|
self.bot.register_message_handler(callback=self.__command_help)
|
|
|
|
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.info('Starting to poll the servers')
|
2022-05-30 20:54:26 +00:00
|
|
|
self.bot.infinity_polling()
|
|
|
|
|
2022-07-08 23:49:12 +00:00
|
|
|
def __command_help(self, message: Message, data: dict):
|
2022-07-10 10:22:44 +00:00
|
|
|
# pylint: disable=unused-argument
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__command_help(message=\'%s\', data=\'%s\')', message, data)
|
2022-05-30 20:54:26 +00:00
|
|
|
self.bot.reply_to(
|
|
|
|
message,
|
|
|
|
'Supported commands:\n'
|
|
|
|
' /add <feed url> - Add new feed\n'
|
|
|
|
' /list - List currently added feeds\n'
|
|
|
|
' /del <feed url> - Remove feed\n'
|
|
|
|
' /help - Get this help message'
|
|
|
|
)
|
|
|
|
|
|
|
|
def __add_feed(self, message: Message, data: dict):
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__add_feed(message=\'%s\', data=\'%s\')', message, data)
|
2022-05-30 20:54:26 +00:00
|
|
|
args = message.text.split()
|
|
|
|
if len(args) < 2:
|
|
|
|
raise DisplayableException('Feed URL should be specified')
|
|
|
|
|
|
|
|
url = str(args[1])
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.info('User %s requested to subscribe to %s', data['user_id'], url)
|
2022-05-30 20:54:26 +00:00
|
|
|
if not self.__is_url_valid(url):
|
|
|
|
raise DisplayableException('Invalid feed URL')
|
|
|
|
|
|
|
|
self.database.subscribe_user_by_url(data['user_id'], url)
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.info('Subscription added')
|
2022-05-30 20:54:26 +00:00
|
|
|
|
|
|
|
self.bot.reply_to(message, 'Successfully subscribed to feed.')
|
|
|
|
|
|
|
|
def __list_feeds(self, message: Message, data: dict):
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__list_feeds(message=\'%s\', data=\'%s\')', message, data)
|
2022-05-30 20:54:26 +00:00
|
|
|
feeds = self.database.find_user_feeds(data['user_id'])
|
|
|
|
|
|
|
|
feed_list = ''
|
2022-07-08 19:23:12 +00:00
|
|
|
for index, feed in enumerate(feeds, start=1):
|
2022-07-08 23:49:12 +00:00
|
|
|
feed_list += str(index) + ': ' + f"{feed['url']}" + '\n'
|
2022-05-30 20:54:26 +00:00
|
|
|
|
|
|
|
self.bot.reply_to(message, 'Your feeds:\n' + feed_list)
|
|
|
|
|
|
|
|
def __delete_feed(self, message: Message, data: dict):
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__delete_feed(message=\'%s\', data=\'%s\')', message, data)
|
2022-05-30 20:54:26 +00:00
|
|
|
args = message.text.split()
|
|
|
|
if len(args) < 2:
|
|
|
|
raise DisplayableException('Feed URL should be specified')
|
|
|
|
|
|
|
|
url = str(args[1])
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.info('User %s requested to unsubscribe from %s', data['user_id'], url)
|
2022-05-30 20:54:26 +00:00
|
|
|
if not self.__is_url_valid(url):
|
|
|
|
raise DisplayableException('Invalid feed URL')
|
|
|
|
|
|
|
|
self.database.unsubscribe_user_by_url(data['user_id'], url)
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.info('Subscription removed')
|
2022-05-30 20:54:26 +00:00
|
|
|
|
|
|
|
self.bot.reply_to(message, 'Unsubscribed.')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __is_url_valid(url: str) -> bool:
|
|
|
|
if not validators.url(url):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# For security reasons we should not allow anything except HTTP/HTTPS.
|
|
|
|
if not url.startswith(('http://', 'https://')):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Notifier:
|
|
|
|
"""Sends notifications to users about new RSS feed items."""
|
|
|
|
|
2022-07-13 22:53:53 +00:00
|
|
|
# https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this
|
|
|
|
BATCH_LIMIT: int = 25
|
2022-05-30 20:54:26 +00:00
|
|
|
|
|
|
|
sent_counter: int = 0
|
|
|
|
|
2022-07-10 12:07:26 +00:00
|
|
|
def __init__(self, token: str, logger: Logger):
|
|
|
|
self.log = logger
|
|
|
|
self.log.debug('Notifier.__init__(token=\'%s\', logger=%s)', token[:8] + '...', logger)
|
2022-05-30 20:54:26 +00:00
|
|
|
self.bot: TeleBot = TeleBot(token)
|
2022-07-10 10:22:44 +00:00
|
|
|
self.html_sanitizer: Cleaner = Cleaner(
|
2022-07-13 22:53:53 +00:00
|
|
|
tags=['b', 'strong', 'i', 'em', 'u', 'ins', 's', 'strike', 'del', 'tg-spoiler', 'a', 'code', 'pre'],
|
2022-07-10 10:22:44 +00:00
|
|
|
attributes={"a": ["href", "title"]},
|
|
|
|
protocols=['http', 'https'],
|
|
|
|
strip=True,
|
|
|
|
)
|
2022-05-30 20:54:26 +00:00
|
|
|
|
2022-07-08 19:23:12 +00:00
|
|
|
def send_updates(self, chat_ids: list[int], updates: list[FeedItem], feed_title: str):
|
2022-05-30 20:54:26 +00:00
|
|
|
"""Send notification about new items to the user"""
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug(
|
|
|
|
'send_updates(chat_ids=list(%d), updates=list(%d), feed_title=\'%s\')',
|
|
|
|
len(chat_ids), len(updates), feed_title
|
|
|
|
)
|
2022-07-08 19:23:12 +00:00
|
|
|
if not updates:
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('No updates to send')
|
2022-07-08 19:23:12 +00:00
|
|
|
return
|
|
|
|
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('%d updates to send to %d chats', len(updates), len(chat_ids))
|
2022-05-30 20:54:26 +00:00
|
|
|
for chat_id in chat_ids:
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('Processing chat_id=%s', chat_id)
|
2022-07-08 19:23:12 +00:00
|
|
|
self.__count_request_and_wait()
|
|
|
|
self.bot.send_message(
|
|
|
|
chat_id=chat_id,
|
|
|
|
text=f'Updates from the {feed_title} feed:'
|
|
|
|
)
|
|
|
|
|
2022-05-30 20:54:26 +00:00
|
|
|
for update in updates:
|
2022-07-08 19:23:12 +00:00
|
|
|
self.__count_request_and_wait()
|
2022-05-30 20:54:26 +00:00
|
|
|
self.__send_update(chat_id, update)
|
|
|
|
|
|
|
|
def __send_update(self, chat_id: int, update: FeedItem):
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__send_update(chat_id=\'%s\', update=\'%s\')', chat_id, update)
|
2022-05-30 20:54:26 +00:00
|
|
|
self.bot.send_message(
|
|
|
|
chat_id=chat_id,
|
|
|
|
text=self.__format_message(update),
|
|
|
|
parse_mode='HTML'
|
|
|
|
)
|
|
|
|
|
2022-07-08 19:23:12 +00:00
|
|
|
def __count_request_and_wait(self):
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__count_request_and_wait()')
|
2022-07-08 19:23:12 +00:00
|
|
|
if self.sent_counter >= self.BATCH_LIMIT:
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('Requests limit exceeded, sleeping for a second')
|
2022-07-08 19:23:12 +00:00
|
|
|
time.sleep(1)
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('Resetting counter')
|
2022-07-08 19:23:12 +00:00
|
|
|
self.sent_counter = 0
|
|
|
|
self.sent_counter += 1
|
|
|
|
|
2022-07-10 10:22:44 +00:00
|
|
|
def __format_message(self, item: FeedItem) -> str:
|
2023-06-25 16:37:52 +00:00
|
|
|
date_string = ''
|
|
|
|
if item.date is not None:
|
|
|
|
date_string = item.date.strftime('%m.%d.%Y %H:%M')
|
|
|
|
|
2022-05-30 20:54:26 +00:00
|
|
|
return (
|
2022-07-10 14:40:19 +00:00
|
|
|
f"<strong><a href=\"{item.url}\">{item.title}</a></strong>\n"
|
2023-06-25 16:37:52 +00:00
|
|
|
f"{date_string}\n\n"
|
2022-07-10 10:22:44 +00:00
|
|
|
f"{self.__sanitize_html(item.description)}"
|
2022-05-30 20:54:26 +00:00
|
|
|
)
|
|
|
|
|
2022-07-10 10:22:44 +00:00
|
|
|
def __sanitize_html(self, html: str) -> str:
|
|
|
|
if not html:
|
|
|
|
return ''
|
|
|
|
return self.html_sanitizer.clean(html)
|
|
|
|
|
2022-05-30 20:54:26 +00:00
|
|
|
|
|
|
|
class UserAuthMiddleware(BaseMiddleware):
|
|
|
|
"""Transparently authenticates and registers the user if needed."""
|
|
|
|
|
2022-07-10 12:07:26 +00:00
|
|
|
def __init__(self, database: Database, logger: Logger):
|
|
|
|
self.log: Logger = logger
|
|
|
|
self.log.debug('UserAuthMiddleware.__init__(database=%s, logger=%s)', database, logger)
|
2022-05-30 20:54:26 +00:00
|
|
|
super().__init__()
|
|
|
|
self.update_types = ['message']
|
|
|
|
self.database: Database = database
|
|
|
|
|
|
|
|
def pre_process(self, message: Message, data: dict):
|
|
|
|
"""Pre-process update, find user and add it's ID to the handler data dictionary."""
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('UserAuthMiddleware.pre_process()')
|
2022-05-30 20:54:26 +00:00
|
|
|
data['user_id'] = self.__find_or_register_user(message)
|
|
|
|
|
|
|
|
def post_process(self, message: Message, data: dict, exception):
|
|
|
|
"""Post-process update."""
|
|
|
|
|
|
|
|
def __find_or_register_user(self, message: Message) -> int:
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('__find_or_register_user()')
|
2022-05-30 20:54:26 +00:00
|
|
|
telegram_id = message.from_user.id
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('Telegram chat_id=%s', telegram_id)
|
2022-05-30 20:54:26 +00:00
|
|
|
|
|
|
|
user_id = self.database.find_user(telegram_id)
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('Database user ID is \'%s\'', user_id)
|
2022-05-30 20:54:26 +00:00
|
|
|
if user_id is None:
|
|
|
|
return self.database.add_user(telegram_id)
|
|
|
|
return user_id
|
|
|
|
|
|
|
|
|
|
|
|
class ExceptionHandlerMiddleware(BaseMiddleware):
|
|
|
|
"""Sends messages to the user on exception."""
|
|
|
|
|
2022-07-10 12:07:26 +00:00
|
|
|
def __init__(self, bot: TeleBot, logger: Logger):
|
|
|
|
self.log: Logger = logger
|
|
|
|
self.log.debug('ExceptionHandlerMiddleware.__init__(bot=%s, logger=%s)', bot, logger)
|
2022-05-30 20:54:26 +00:00
|
|
|
super().__init__()
|
|
|
|
self.update_types = ['message']
|
|
|
|
self.bot: TeleBot = bot
|
|
|
|
|
|
|
|
def pre_process(self, message: Message, data: dict):
|
|
|
|
"""Pre-process update."""
|
|
|
|
|
|
|
|
# pylint: disable=W0613
|
|
|
|
def post_process(self, message: Message, data: dict, exception: Exception | None):
|
|
|
|
"""Post-process update. Send user an error notification."""
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('ExceptionHandlerMiddleware.post_process()')
|
|
|
|
|
2022-05-30 20:54:26 +00:00
|
|
|
if exception is None:
|
|
|
|
return
|
2022-07-10 12:07:26 +00:00
|
|
|
|
|
|
|
self.log.exception('Exception caught during message processing: %s', exception)
|
2022-05-30 20:54:26 +00:00
|
|
|
if isinstance(exception, DisplayableException):
|
|
|
|
self.bot.reply_to(message, 'Error: ' + str(exception))
|
|
|
|
else:
|
|
|
|
self.bot.reply_to(message, 'Something went wrong. Please try again (maybe later).')
|