2022-05-01 23:43:04 +00:00
|
|
|
import telebot
|
|
|
|
from telebot.types import Message
|
|
|
|
|
2022-05-02 20:44:16 +00:00
|
|
|
|
2022-05-01 23:43:04 +00:00
|
|
|
class CommandProcessor:
|
2022-05-02 15:18:13 +00:00
|
|
|
"""Processes user input and dispatches the data to other services."""
|
2022-05-02 14:53:53 +00:00
|
|
|
|
2022-05-01 23:43:04 +00:00
|
|
|
bot: telebot.TeleBot
|
|
|
|
|
|
|
|
def __init__(self, token: str):
|
2022-05-02 16:14:12 +00:00
|
|
|
if token is None or len(token) == 0:
|
|
|
|
raise ValueError("Token should not be empty")
|
2022-05-01 23:43:04 +00:00
|
|
|
self.bot = telebot.TeleBot(token)
|
|
|
|
|
|
|
|
def run(self):
|
2022-05-02 15:18:13 +00:00
|
|
|
"""Runs a bot and polls for new messages indefinitely."""
|
2022-05-01 23:43:04 +00:00
|
|
|
self.bot.register_message_handler(commands=['help', 'start'], callback=self.__command_help)
|
2022-05-02 15:18:13 +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)
|
|
|
|
|
2022-05-01 23:43:04 +00:00
|
|
|
self.bot.infinity_polling()
|
|
|
|
|
|
|
|
def __command_help(self, message: Message):
|
|
|
|
self.bot.reply_to(message, 'Commands: "/add", "/list", "/del", "/help".')
|
|
|
|
|
|
|
|
def __add_feed(self, message: Message):
|
|
|
|
self.bot.reply_to(message, 'Feed added (stub)')
|
|
|
|
|
|
|
|
def __list_feeds(self, message: Message):
|
|
|
|
self.bot.reply_to(message, 'Your feeds: (stub)')
|
|
|
|
|
|
|
|
def __delete_feed(self, message: Message):
|
|
|
|
self.bot.reply_to(message, 'Feed deleted: (stub)')
|
2022-05-29 21:33:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Notifier:
|
|
|
|
"""Sends notifications to users about new RSS feed items."""
|
|
|
|
|
|
|
|
bot: telebot.TeleBot
|
|
|
|
|
|
|
|
def __init__(self, token: str):
|
|
|
|
self.bot = telebot.TeleBot(token)
|
|
|
|
|
|
|
|
def send_updates(self, chat_id: int, updates: list):
|
|
|
|
"""Send notification about new items to the user"""
|
|
|
|
for update in updates:
|
|
|
|
self.__send_update(chat_id, update)
|
|
|
|
|
|
|
|
def __send_update(self, chat_id: int, update):
|
|
|
|
self.bot.send_message(
|
|
|
|
chat_id=chat_id,
|
|
|
|
text=self.__format_message(),
|
|
|
|
parse_mode='MarkdownV2'
|
|
|
|
)
|
|
|
|
|
|
|
|
def __format_message(self) -> str:
|
|
|
|
update = {
|
|
|
|
'title': 'Item Title',
|
|
|
|
'text': 'Short item text here...',
|
|
|
|
'url': 'https://fediland.github.io/',
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
f"**[{update['title']}]({update['url']})**\n\n"
|
|
|
|
f"{update['text']}"
|
|
|
|
)
|