From 5befc16ea8ea99678d9ab9374f92db0373270f1a Mon Sep 17 00:00:00 2001 From: Yariv Menachem Date: Tue, 31 Dec 2024 15:48:21 +0200 Subject: [PATCH] moved method of extract --- .../button_callback_context.py | 5 +-- .../button_callback/button_fire_strategy.py | 32 +++---------------- .../telegram_callback_handler.py | 30 ++++++++++++++--- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/telegram_handler/button_callback/button_callback_context.py b/src/telegram_handler/button_callback/button_callback_context.py index 7530ac7..99e5ca1 100644 --- a/src/telegram_handler/button_callback/button_callback_context.py +++ b/src/telegram_handler/button_callback/button_callback_context.py @@ -15,11 +15,12 @@ class ButtonCallBackContext: The Context defines the interface """ - def __init__(self, data: str, message: MaybeInaccessibleMessage) -> None: + def __init__(self, data: str, message: MaybeInaccessibleMessage,job_id:str) -> None: self._logger = create_logger("Button CallBack Context") self._message = message self._data = data self._telegram_bot = TelegramBot() + self._job_id = job_id @property def strategy(self) -> ButtonStrategy: @@ -42,7 +43,7 @@ class ButtonCallBackContext: async def run(self) -> None: self._logger.info("Starting") if ReactionEmoji.FIRE.name == self._data: - self.strategy = FireStrategy(self._message) + self.strategy = FireStrategy(self._message,self._job_id) elif ReactionEmoji.PILE_OF_POO.name == self._data: self.strategy = PooStrategy(self._message) else: diff --git a/src/telegram_handler/button_callback/button_fire_strategy.py b/src/telegram_handler/button_callback/button_fire_strategy.py index 31c5e2e..8c3767a 100644 --- a/src/telegram_handler/button_callback/button_fire_strategy.py +++ b/src/telegram_handler/button_callback/button_fire_strategy.py @@ -7,32 +7,8 @@ from telegram_bot import TelegramBot from telegram_handler.button_callback.button_strategy import ButtonStrategy -def _extract_job_id(message: str) -> str: - """ - Extracts the job ID from a job description string. - - Args: - message: The string containing the job description. - - Returns: - The extracted job ID, or an empty string if not found. - """ - # Find the starting position of the ID - start_pos = message.find("Job ID: ") - if start_pos == -1: - return "" # Not found - - # Find the ending position of the ID (excluding newline) - end_pos = message.find("\n", start_pos + len("Job ID: ")) - if end_pos == -1: - end_pos = len(message) # No newline, use string end - - # Extract the ID substring - return message[start_pos + len("Job ID: "):end_pos] - - class FireStrategy(ButtonStrategy): - def __init__(self, message: MaybeInaccessibleMessage) -> None: + def __init__(self, message: MaybeInaccessibleMessage, job_id: str) -> None: """ Usually, the Context accepts a strategy through the constructor, but also provides a setter to change it at runtime. @@ -41,13 +17,13 @@ class FireStrategy(ButtonStrategy): self._emoji = ReactionEmoji.FIRE self._telegram_bot = TelegramBot() self._job_repository = JobRepository() + self._job_id = job_id self._logger = create_logger("FireStrategy") async def execute(self): - job_id = _extract_job_id(self._message.text) - job = self._job_repository.find_by_id(job_id) + job = self._job_repository.find_by_id(self._job_id) if not job: - self._logger.error(f"Job with ID {job_id} not found.") + self._logger.error(f"Job with ID {self._job_id} not found.") return job["applied"] = True self._job_repository.update(job) diff --git a/src/telegram_handler/button_callback/telegram_callback_handler.py b/src/telegram_handler/button_callback/telegram_callback_handler.py index 16a156e..b43f4cc 100644 --- a/src/telegram_handler/button_callback/telegram_callback_handler.py +++ b/src/telegram_handler/button_callback/telegram_callback_handler.py @@ -1,5 +1,4 @@ from telegram import Update -from telegram.constants import ReactionEmoji from telegram.ext import ( ContextTypes, ) @@ -7,8 +6,30 @@ from telegram.ext import ( from jobspy import create_logger from telegram_bot import TelegramBot from telegram_handler.button_callback.button_callback_context import ButtonCallBackContext -from telegram_handler.button_callback.button_fire_strategy import FireStrategy -from telegram_handler.button_callback.button_poo_strategy import PooStrategy + + +def _extract_job_id(message: str) -> str: + """ + Extracts the job ID from a job description string. + + Args: + message: The string containing the job description. + + Returns: + The extracted job ID, or an empty string if not found. + """ + # Find the starting position of the ID + start_pos = message.find("Job ID: ") + if start_pos == -1: + return "" # Not found + + # Find the ending position of the ID (excluding newline) + end_pos = message.find("\n", start_pos + len("Job ID: ")) + if end_pos == -1: + end_pos = len(message) # No newline, use string end + + # Extract the ID substring + return message[start_pos + len("Job ID: "):end_pos] class TelegramCallHandler: @@ -20,6 +41,7 @@ class TelegramCallHandler: """Parses the CallbackQuery and updates the message.""" query = update.callback_query await query.answer() - button_context = ButtonCallBackContext(query.data, query.message) + job_id = _extract_job_id(query.message.text) + button_context = ButtonCallBackContext(query.data, query.message, job_id) await button_context.run()