From 8996c59492ae4100aa8bd0394b06edb67cadcf62 Mon Sep 17 00:00:00 2001 From: Yariv Menachem Date: Sun, 29 Dec 2024 17:18:03 +0200 Subject: [PATCH] extracted button call back --- src/main.py | 4 +++- .../telegram_callback_handler.py | 21 +++++++++++++++++++ .../telegram_default_handler.py | 9 -------- 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/telegram_handler/telegram_callback_handler.py diff --git a/src/main.py b/src/main.py index 36d568b..f40a6cf 100644 --- a/src/main.py +++ b/src/main.py @@ -6,6 +6,7 @@ from telegram.ext import Application, CommandHandler, CallbackQueryHandler from src.jobspy import Site from src.jobspy.scrapers.utils import create_logger from src.telegram_handler import TelegramIndeedHandler, TelegramDefaultHandler +from src.telegram_handler.telegram_callback_handler import TelegramCallHandler logger = create_logger("Main") title_filters: list[str] = ["test", "qa", "Lead", "Full-Stack", "Full Stack", "Fullstack", "Frontend", "Front-end", @@ -19,6 +20,7 @@ if __name__ == "__main__": search_term = "software engineer" locations = ["Tel Aviv, Israel", "Ramat Gan, Israel", "Central, Israel", "Rehovot ,Israel"] application = Application.builder().token(_api_token).build() + tg_callback_handler = TelegramCallHandler() tg_handler_all = TelegramDefaultHandler(sites=[Site.LINKEDIN, Site.GLASSDOOR, Site.INDEED, Site.GOOZALI], locations=locations, title_filters=title_filters, @@ -47,6 +49,6 @@ if __name__ == "__main__": title_filters=title_filters, search_term=search_term) application.add_handler(CommandHandler(Site.INDEED.value, tg_handler_indeed.handle)) - application.add_handler(CallbackQueryHandler(tg_handler_linkedin.button)) + application.add_handler(CallbackQueryHandler(tg_callback_handler.button_callback)) logger.info("Run polling from telegram") application.run_polling(allowed_updates=Update.ALL_TYPES) diff --git a/src/telegram_handler/telegram_callback_handler.py b/src/telegram_handler/telegram_callback_handler.py new file mode 100644 index 0000000..b28df7b --- /dev/null +++ b/src/telegram_handler/telegram_callback_handler.py @@ -0,0 +1,21 @@ +from telegram import Update +from telegram.ext import ( + ContextTypes, +) + +from src.telegram_bot import TelegramBot + + +class TelegramCallHandler: + def __init__(self): + self.telegram_bot = TelegramBot() + + async def button_callback(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Parses the CallbackQuery and updates the message text.""" + 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() + await self.telegram_bot.set_message_reaction(query.message.message_id, query.data) + diff --git a/src/telegram_handler/telegram_default_handler.py b/src/telegram_handler/telegram_default_handler.py index 23d8496..0c7c2f7 100644 --- a/src/telegram_handler/telegram_default_handler.py +++ b/src/telegram_handler/telegram_default_handler.py @@ -23,15 +23,6 @@ class TelegramDefaultHandler(TelegramHandler): else: self.logger = create_logger("TelegramAllHandler") - async def button(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Parses the CallbackQuery and updates the message text.""" - 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() - await self.telegram_bot.set_message_reaction(query.message.message_id, query.data) - async def handle(self, update: Update, context: ContextTypes.DEFAULT_TYPE): self.logger.info("start handling") jobs = scrape_jobs(