added keyboard to filtered out jobs with names

pull/231/head
Yariv Menachem 2024-12-31 16:35:41 +02:00
parent 3a2e47f7b6
commit 3c2fed876b
2 changed files with 25 additions and 10 deletions

View File

@ -52,12 +52,12 @@ class TelegramBot:
logger.error(f"Failed to send job to Telegram: {job.id}")
logger.error(f"Error: {e}")
async def send_text(self, message: str):
async def send_text(self, message: str, reply_markup: InlineKeyboardMarkup = None):
"""
Send Text han Message to Telegram chat.
"""
try:
await self.bot.sendMessage(chat_id=self.chatId, text=message)
await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup)
logger.info("Sent text message to Telegram")
except Exception as e:
logger.error("Failed to send text message to Telegram")

View File

@ -1,4 +1,4 @@
from telegram import Update
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ReactionEmoji
from telegram.ext import (
ContextTypes,
@ -11,6 +11,27 @@ from telegram_bot import TelegramBot
from telegram_handler.telegram_handler import TelegramHandler
def map_jobs_to_keyboard(jobs: list[JobPost]) -> InlineKeyboardMarkup:
"""
Maps a list of JobPost objects to a list of lists of InlineKeyboardButton objects.
Args:
jobs: A list of JobPost objects.
Returns:
A list of lists of InlineKeyboardButton objects, where each inner list contains
a single button representing a job.
"""
keyboard = []
for job in jobs:
# Create a new inner list for each job
inner_list = [InlineKeyboardButton(job.title, callback_data=job.id)]
# Append the inner list to the main keyboard list
keyboard.append(inner_list)
return InlineKeyboardMarkup(keyboard)
class TelegramDefaultHandler(TelegramHandler):
def __init__(self, sites: list[Site], locations: list[str], title_filters: list[str], search_term: str):
self.sites_to_scrap = sites
@ -25,10 +46,6 @@ class TelegramDefaultHandler(TelegramHandler):
else:
self.logger = create_logger("TelegramAllHandler")
async def send_old_job(self, old_jobs: list[JobPost]):
pass
async def handle(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.logger.info("start handling")
await self.telegram_bot.set_message_reaction(
@ -50,9 +67,7 @@ class TelegramDefaultHandler(TelegramHandler):
old_jobs, new_jobs = self.jobRepository.insert_many_if_not_found(jobs)
for newJob in new_jobs:
await self.telegram_bot.send_job(newJob)
filtered_by_title = [job.title for job in filtered_out_jobs]
result_string = "filtered by title:\n" + "\n".join(filtered_by_title)
await self.telegram_bot.send_text(result_string)
await self.telegram_bot.send_text("filtered by title: ", reply_markup=map_jobs_to_keyboard(filtered_out_jobs))
self.logger.info(f"Found {len(old_jobs)} old jobs")
await self.telegram_bot.send_text(
f"Finished scarping: {site_names_print}")