diff --git a/src/telegram_bot.py b/src/telegram_bot.py index 2875eb8..68545e2 100644 --- a/src/telegram_bot.py +++ b/src/telegram_bot.py @@ -44,20 +44,23 @@ class TelegramBot: logger.error(f"Failed to send job to Telegram: {job.id}") logger.error(f"Error: {e}") + def get_reply_markup(self): + keyboard = [ + [ + InlineKeyboardButton(ReactionEmoji.FIRE, callback_data=ReactionEmoji.FIRE.name), + InlineKeyboardButton(ReactionEmoji.PILE_OF_POO, callback_data=ReactionEmoji.PILE_OF_POO.name) + ], + ] + + return InlineKeyboardMarkup(keyboard) + async def send_test_message(self): """ Send Test Message to Telegram chat. """ message = "Test Test Testing" try: - keyboard = [ - [ - InlineKeyboardButton(ReactionEmoji.FIRE, callback_data=ReactionEmoji.FIRE.name), - InlineKeyboardButton(ReactionEmoji.PILE_OF_POO, callback_data=ReactionEmoji.PILE_OF_POO.name) - ], - ] - - reply_markup = InlineKeyboardMarkup(keyboard) + reply_markup = self.get_reply_markup() await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup) logger.info("Sent test message to Telegram") except Exception as e: diff --git a/src/telegram_handler/button_callback/telegram_callback_handler.py b/src/telegram_handler/button_callback/telegram_callback_handler.py index b6e9915..c7f2e42 100644 --- a/src/telegram_handler/button_callback/telegram_callback_handler.py +++ b/src/telegram_handler/button_callback/telegram_callback_handler.py @@ -4,6 +4,7 @@ from telegram.ext import ( ContextTypes, ) +from src.jobspy import create_logger from src.telegram_bot import TelegramBot from src.telegram_handler.button_callback.button_callback_context import ButtonCallBackContext from src.telegram_handler.button_callback.button_fire_strategy import FireStrategy @@ -13,25 +14,20 @@ from src.telegram_handler.button_callback.button_poo_strategy import PooStrategy class TelegramCallHandler: def __init__(self): self.telegram_bot = TelegramBot() + self.logger = create_logger("TelegramCallHandler") async def button_callback(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Parses the CallbackQuery and updates the message text.""" + """Parses the CallbackQuery and updates the message.""" query = update.callback_query - - # CallbackQueries need to be answered, even if no notification to the user is needed - # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery await query.answer() button_context = ButtonCallBackContext() - if ReactionEmoji.FIRE.name == query.data: - strategy = FireStrategy(query.message) - # elif ReactionEmoji.PILE_OF_POO.name == query.data: - # strategy = PooStrategy(query.message) - else: - raise ValueError("Invalid enum value") - if not strategy: + if ReactionEmoji.FIRE.name == query.data: + button_context.strategy = FireStrategy(query.message) + elif ReactionEmoji.PILE_OF_POO.name == query.data: + button_context.strategy = PooStrategy(query.message) + else: + self.logger.error("Invalid enum value") return - button_context.strategy = strategy await button_context.run() -