From f010ce51b1a05554778a358945a4e6aa34bf0fe1 Mon Sep 17 00:00:00 2001 From: Yariv Menachem Date: Thu, 26 Dec 2024 16:44:35 +0200 Subject: [PATCH] init commit --- src/jobspy/main.py | 16 +++++++++++- src/jobspy/telegram_bot.py | 20 ++++++++++---- src/jobspy/telegram_handler.py | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 src/jobspy/telegram_handler.py diff --git a/src/jobspy/main.py b/src/jobspy/main.py index 88a097c..a01d3a1 100644 --- a/src/jobspy/main.py +++ b/src/jobspy/main.py @@ -1,10 +1,15 @@ import asyncio +import os import re + +from telegram.ext import Application, CommandHandler + from src.jobspy import Site, scrape_jobs from src.jobspy.db.job_repository import JobRepository from src.jobspy.jobs import JobPost from src.jobspy.scrapers.utils import create_logger from src.jobspy.telegram_bot import TelegramBot +from src.jobspy.telegram_handler import TelegramHandler logger = create_logger("Main") filter_by_title: list[str] = ["test", "qa", "Lead", "Full-Stack", "Full Stack", "Fullstack", "Frontend", "Front-end", "Front End", "DevOps", "Physical", "Staff", @@ -24,6 +29,7 @@ def filter_jobs_by_title_name(job: JobPost): async def main(): telegramBot = TelegramBot() jobRepository = JobRepository() + tg_handler = TelegramHandler() # sites_to_scrap = [Site.LINKEDIN, Site.GLASSDOOR, Site.INDEED, Site.GOOZALI] sites_to_scrap = [Site.GOOZALI] # sites_to_scrap = [Site.GOOZALI] @@ -44,4 +50,12 @@ async def main(): # Run the async main function if __name__ == "__main__": - asyncio.run(main()) + # asyncio.run(main()) + _api_token = os.getenv("TELEGRAM_API_TOKEN") + application = Application.builder().token(_api_token).build() + application.add_handler(CommandHandler("find", findAll)) + application.add_handler(CommandHandler("galssdoor", find_glassdoor)) + application.add_handler(CommandHandler("linkedin", find_linkedin)) + application.add_handler(CommandHandler("indeed", find_indeed)) + application.add_handler(CommandHandler("goozali", find_goozali)) + tg_handler = TelegramHandler().handler() diff --git a/src/jobspy/telegram_bot.py b/src/jobspy/telegram_bot.py index d71f511..0ab1b8a 100644 --- a/src/jobspy/telegram_bot.py +++ b/src/jobspy/telegram_bot.py @@ -1,7 +1,14 @@ import os from dotenv import load_dotenv -from telegram import Bot - +from telegram import Bot, Update +from telegram.ext import ( + Application, + CommandHandler, + ContextTypes, + ConversationHandler, + MessageHandler, + filters, +) from .jobs import JobPost from .scrapers.utils import create_logger @@ -16,13 +23,16 @@ class TelegramBot: self._api_token = os.getenv("TELEGRAM_API_TOKEN") self.chatId = os.getenv("TELEGRAM_CHAT_ID") self.bot = Bot(token=self._api_token) + # Create the Application and pass it your bot's token. + self.application = Application.builder().token(self._api_token).build() + # Run the bot until the user presses Ctrl-C + self.application.run_polling(allowed_updates=Update.ALL_TYPES) async def sendJob(self, job: JobPost): """ Send JobPost details to Telegram chat. """ - message = f"New Job Posted:\n\n" \ - f"Job ID: {job.id}\n" \ + 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" \ @@ -32,4 +42,4 @@ class TelegramBot: 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}") + logger.error(f"Error: {e}") \ No newline at end of file diff --git a/src/jobspy/telegram_handler.py b/src/jobspy/telegram_handler.py new file mode 100644 index 0000000..348446a --- /dev/null +++ b/src/jobspy/telegram_handler.py @@ -0,0 +1,48 @@ +import os +from dotenv import load_dotenv +from telegram import Bot, Update, ReplyKeyboardMarkup +from telegram.ext import ( + Application, + CommandHandler, + ContextTypes, + ConversationHandler, + MessageHandler, + filters, +) +from .scrapers.utils import create_logger + +load_dotenv() + +logger = create_logger("TelegramBot") + + +class TelegramHandler: + def __init__(self): + self._api_token = os.getenv("TELEGRAM_API_TOKEN") + self.chatId = os.getenv("TELEGRAM_CHAT_ID") + self.bot = Bot(token=self._api_token) + # Create the Application and pass it your bot's token. + self.application = Application.builder().token(self._api_token).build() + + async def findAll(self, update: Update, context: ContextTypes.DEFAULT_TYPE): + """Start the conversation and ask user for input.""" + await update.message.reply_text( + "Hi! My name is Doctor Botter. I will hold a more complex conversation with you. " + "Why don't you tell me something about yourself?" + ) + + async def find_glassdoor(self, update: Update, context: ContextTypes.DEFAULT_TYPE): + """Start the conversation and ask user for input.""" + await update.message.reply_text( + "Hi! My name is Doctor Botter. I will hold a more complex conversation with you. " + "Why don't you tell me something about yourself?" + ) + + def handler(self): + self.application.add_handler(CommandHandler("find", self.findAll)) + self.application.add_handler(CommandHandler("galssdoor", self.find_glassdoor)) + self.application.add_handler(CommandHandler("linkedin", self.findAll)) + self.application.add_handler(CommandHandler("indeed", self.findAll)) + self.application.add_handler(CommandHandler("goozali", self.findAll)) + # Run the bot until the user presses Ctrl-C + self.application.run_polling(allowed_updates=Update.ALL_TYPES) \ No newline at end of file