Added reaction to command and messages for start and finish

pull/231/head
Yariv Menachem 2024-12-30 16:01:11 +02:00
parent d563ab81ed
commit 9c18c2e936
3 changed files with 31 additions and 5 deletions

View File

@ -11,6 +11,10 @@ load_dotenv()
class JobRepository: class JobRepository:
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(JobRepository, cls).__new__(cls)
return cls.instance
def __init__(self, database_name: str = None): def __init__(self, database_name: str = None):
self.logger = create_logger("JobRepository") self.logger = create_logger("JobRepository")

View File

@ -12,6 +12,10 @@ logger = create_logger("TelegramBot")
class TelegramBot: class TelegramBot:
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(TelegramBot, cls).__new__(cls)
return cls.instance
def __init__(self): def __init__(self):
self._api_token = os.getenv("TELEGRAM_API_TOKEN") self._api_token = os.getenv("TELEGRAM_API_TOKEN")
@ -48,6 +52,17 @@ class TelegramBot:
logger.error(f"Failed to send job to Telegram: {job.id}") logger.error(f"Failed to send job to Telegram: {job.id}")
logger.error(f"Error: {e}") logger.error(f"Error: {e}")
async def send_text(self, message: str):
"""
Send Text han Message to Telegram chat.
"""
try:
await self.bot.sendMessage(chat_id=self.chatId, text=message)
logger.info("Sent text message to Telegram")
except Exception as e:
logger.error("Failed to send text message to Telegram")
logger.error(f"Error: {e}")
async def send_test_message(self): async def send_test_message(self):
""" """
Send Test Message to Telegram chat. Send Test Message to Telegram chat.
@ -58,7 +73,7 @@ class TelegramBot:
await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup) await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup)
logger.info("Sent test message to Telegram") logger.info("Sent test message to Telegram")
except Exception as e: except Exception as e:
logger.error("Failed o send test message to Telegram") logger.error("Failed to send test message to Telegram")
logger.error(f"Error: {e}") logger.error(f"Error: {e}")
async def set_message_reaction(self, message_id: int, emoji_reaction: ReactionEmoji): async def set_message_reaction(self, message_id: int, emoji_reaction: ReactionEmoji):

View File

@ -1,4 +1,5 @@
from telegram import Update from telegram import Update
from telegram.constants import ReactionEmoji
from telegram.ext import ( from telegram.ext import (
ContextTypes, ContextTypes,
) )
@ -26,6 +27,10 @@ class TelegramDefaultHandler(TelegramHandler):
async def handle(self, update: Update, context: ContextTypes.DEFAULT_TYPE): async def handle(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.logger.info("start handling") self.logger.info("start handling")
await self.telegram_bot.send_text(
f"Start scarping: {self.sites_to_scrap[0].name}")
await self.telegram_bot.set_message_reaction(
update.message.message_id, ReactionEmoji.FIRE)
jobs = scrape_jobs( jobs = scrape_jobs(
site_name=self.sites_to_scrap, site_name=self.sites_to_scrap,
search_term=self.search_term, search_term=self.search_term,
@ -38,4 +43,6 @@ class TelegramDefaultHandler(TelegramHandler):
new_jobs = self.jobRepository.insertManyIfNotFound(jobs) new_jobs = self.jobRepository.insertManyIfNotFound(jobs)
for newJob in new_jobs: for newJob in new_jobs:
await self.telegram_bot.send_job(newJob) await self.telegram_bot.send_job(newJob)
await self.telegram_bot.send_text(
f"Finished scarping: {self.sites_to_scrap[0].name}")
self.logger.info("finished handling") self.logger.info("finished handling")