From bdb70ff4de5f427627cd9754039402e0f2a84e8d Mon Sep 17 00:00:00 2001 From: Yariv Menachem Date: Sun, 29 Dec 2024 19:03:23 +0200 Subject: [PATCH] cleaned some code --- src/telegram_bot.py | 44 ++++++++----------- .../button_callback/button_fire_strategy.py | 8 ++-- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/telegram_bot.py b/src/telegram_bot.py index 68545e2..8c2068a 100644 --- a/src/telegram_bot.py +++ b/src/telegram_bot.py @@ -18,32 +18,6 @@ class TelegramBot: self.chatId = os.getenv("TELEGRAM_CHAT_ID") self.bot = Bot(token=self._api_token) - async def send_job(self, job: JobPost): - """ - Send JobPost details to Telegram chat. - """ - message = f"Job ID: {job.id}\n" \ - f"Job Title: {job.title}\n" \ - f"Company: {job.company_name}\n" \ - f"Location: {job.location.display_location()}\n" \ - f"Link: {job.job_url}\n" - - 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) - - try: - await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup) - logger.info(f"Sent job to Telegram: {job.id}") - except Exception as e: - logger.error(f"Failed to send job to Telegram: {job.id}") - logger.error(f"Error: {e}") - def get_reply_markup(self): keyboard = [ [ @@ -54,6 +28,24 @@ class TelegramBot: return InlineKeyboardMarkup(keyboard) + async def send_job(self, job: JobPost): + """ + Send JobPost details to Telegram chat. + """ + message = f"Job ID: {job.id}\n" \ + f"Job Title: {job.title}\n" \ + f"Company: {job.company_name}\n" \ + f"Location: {job.location.display_location()}\n" \ + f"Link: {job.job_url}\n" + reply_markup = self.get_reply_markup() + + try: + await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup) + logger.info(f"Sent job to Telegram: {job.id}") + except Exception as e: + logger.error(f"Failed to send job to Telegram: {job.id}") + logger.error(f"Error: {e}") + async def send_test_message(self): """ Send Test Message to Telegram chat. diff --git a/src/telegram_handler/button_callback/button_fire_strategy.py b/src/telegram_handler/button_callback/button_fire_strategy.py index 5c1e4cd..0f35d69 100644 --- a/src/telegram_handler/button_callback/button_fire_strategy.py +++ b/src/telegram_handler/button_callback/button_fire_strategy.py @@ -6,17 +6,17 @@ from src.telegram_handler.button_callback.button_strategy import ButtonStrategy class FireStrategy(ButtonStrategy): - def __init__(self, data: MaybeInaccessibleMessage) -> None: + def __init__(self, message: MaybeInaccessibleMessage) -> None: """ Usually, the Context accepts a strategy through the constructor, but also provides a setter to change it at runtime. """ - self.data = data + self.message = message self._emoji = ReactionEmoji.FIRE + self.telegram_bot = TelegramBot() async def execute(self): - telegram_bot = TelegramBot() - await telegram_bot.set_message_reaction(self.data.message_id, self._emoji) + await self.telegram_bot.set_message_reaction(self.message.message_id, self._emoji) # find the position in DB # set applied to True # save