mirror of https://github.com/Bunsly/JobSpy
added buttons to job message sent to chat and a callback to update the message
parent
61cb80839c
commit
7f7d7f96d6
|
@ -1,7 +1,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.ext import Application, CommandHandler
|
from telegram.ext import Application, CommandHandler, CallbackQueryHandler
|
||||||
|
|
||||||
from src.jobspy import Site
|
from src.jobspy import Site
|
||||||
from src.jobspy.scrapers.utils import create_logger
|
from src.jobspy.scrapers.utils import create_logger
|
||||||
|
@ -47,5 +47,6 @@ if __name__ == "__main__":
|
||||||
title_filters=title_filters,
|
title_filters=title_filters,
|
||||||
search_term=search_term)
|
search_term=search_term)
|
||||||
application.add_handler(CommandHandler(Site.INDEED.value, tg_handler_indeed.handle))
|
application.add_handler(CommandHandler(Site.INDEED.value, tg_handler_indeed.handle))
|
||||||
|
application.add_handler(CallbackQueryHandler(tg_handler_linkedin.button))
|
||||||
logger.info("Run polling from telegram")
|
logger.info("Run polling from telegram")
|
||||||
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
||||||
|
|
|
@ -1,14 +1,8 @@
|
||||||
import os
|
import os
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from telegram import Bot, Update
|
from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup
|
||||||
from telegram.ext import (
|
from telegram.constants import ReactionEmoji
|
||||||
Application,
|
|
||||||
CommandHandler,
|
|
||||||
ContextTypes,
|
|
||||||
ConversationHandler,
|
|
||||||
MessageHandler,
|
|
||||||
filters,
|
|
||||||
)
|
|
||||||
from src.jobspy.jobs import JobPost
|
from src.jobspy.jobs import JobPost
|
||||||
from src.jobspy.scrapers.utils import create_logger
|
from src.jobspy.scrapers.utils import create_logger
|
||||||
|
|
||||||
|
@ -24,7 +18,7 @@ class TelegramBot:
|
||||||
self.chatId = os.getenv("TELEGRAM_CHAT_ID")
|
self.chatId = os.getenv("TELEGRAM_CHAT_ID")
|
||||||
self.bot = Bot(token=self._api_token)
|
self.bot = Bot(token=self._api_token)
|
||||||
|
|
||||||
async def sendJob(self, job: JobPost):
|
async def send_job(self, job: JobPost):
|
||||||
"""
|
"""
|
||||||
Send JobPost details to Telegram chat.
|
Send JobPost details to Telegram chat.
|
||||||
"""
|
"""
|
||||||
|
@ -33,9 +27,53 @@ class TelegramBot:
|
||||||
f"Company: {job.company_name}\n" \
|
f"Company: {job.company_name}\n" \
|
||||||
f"Location: {job.location.display_location()}\n" \
|
f"Location: {job.location.display_location()}\n" \
|
||||||
f"Link: {job.job_url}\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:
|
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(f"Sent job to Telegram: {job.id}")
|
logger.info(f"Sent job to Telegram: {job.id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
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_test_message(self):
|
||||||
|
"""
|
||||||
|
Send Test Message to Telegram chat.
|
||||||
|
"""
|
||||||
|
message = "Test Test Testing"
|
||||||
|
try:
|
||||||
|
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)
|
||||||
|
await self.bot.sendMessage(chat_id=self.chatId, text=message, reply_markup=reply_markup)
|
||||||
|
logger.info("Sent test message to Telegram")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Failed o send test message to Telegram")
|
||||||
|
logger.error(f"Error: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
async def set_message_reaction(self, message_id: int, emoji_reaction: str):
|
||||||
|
"""
|
||||||
|
Send Test Message to Telegram chat.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
await self.bot.set_message_reaction(chat_id=self.chatId, message_id=message_id,
|
||||||
|
reaction=ReactionEmoji[emoji_reaction])
|
||||||
|
logger.info("Sent test message to Telegram")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Failed to send test message to Telegram")
|
||||||
|
logger.error(f"Error: {e}")
|
||||||
|
|
|
@ -16,13 +16,22 @@ class TelegramDefaultHandler(TelegramHandler):
|
||||||
self.locations = locations
|
self.locations = locations
|
||||||
self.search_term = search_term
|
self.search_term = search_term
|
||||||
self.title_filters = title_filters
|
self.title_filters = title_filters
|
||||||
self.telegramBot = TelegramBot()
|
self.telegram_bot = TelegramBot()
|
||||||
self.jobRepository = JobRepository()
|
self.jobRepository = JobRepository()
|
||||||
if len(sites) == 1:
|
if len(sites) == 1:
|
||||||
self.logger = create_logger(f"Telegram{sites[0].name.title()}Handler")
|
self.logger = create_logger(f"Telegram{sites[0].name.title()}Handler")
|
||||||
else:
|
else:
|
||||||
self.logger = create_logger("TelegramAllHandler")
|
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):
|
async def handle(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
self.logger.info("start handling")
|
self.logger.info("start handling")
|
||||||
jobs = scrape_jobs(
|
jobs = scrape_jobs(
|
||||||
|
@ -36,5 +45,5 @@ class TelegramDefaultHandler(TelegramHandler):
|
||||||
self.logger.info(f"Found {len(jobs)} jobs")
|
self.logger.info(f"Found {len(jobs)} jobs")
|
||||||
new_jobs = self.jobRepository.insertManyIfNotFound(jobs)
|
new_jobs = self.jobRepository.insertManyIfNotFound(jobs)
|
||||||
for newJob in new_jobs:
|
for newJob in new_jobs:
|
||||||
await self.telegramBot.sendJob(newJob)
|
await self.telegram_bot.send_job(newJob)
|
||||||
self.logger.info("finished handling")
|
self.logger.info("finished handling")
|
||||||
|
|
|
@ -34,5 +34,5 @@ class TelegramIndeedHandler(TelegramHandler):
|
||||||
self.logger.info(f"Found {len(jobs)} jobs")
|
self.logger.info(f"Found {len(jobs)} jobs")
|
||||||
new_jobs = self.jobRepository.insertManyIfNotFound(jobs)
|
new_jobs = self.jobRepository.insertManyIfNotFound(jobs)
|
||||||
for newJob in new_jobs:
|
for newJob in new_jobs:
|
||||||
await self.telegramBot.sendJob(newJob)
|
await self.telegramBot.send_job(newJob)
|
||||||
self.logger.info("finished handling")
|
self.logger.info("finished handling")
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from jobspy.db.job_repository import JobRepository
|
|
||||||
from jobspy.telegram_bot import TelegramBot
|
from src.telegram_bot import TelegramBot
|
||||||
from tests.test_util import createMockJob
|
from src.tests.test_util import createMockJob
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ class TelegramTests:
|
||||||
Sents a mock job Telegram using Telegram Bot.
|
Sents a mock job Telegram using Telegram Bot.
|
||||||
"""
|
"""
|
||||||
job = createMockJob()
|
job = createMockJob()
|
||||||
await self.bot.sendJob(job)
|
await self.bot.send_job(job)
|
||||||
print(f"Test sent job finished.")
|
print(f"Test sent job finished.")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue