From 84ebbeffd4b259ae89adda5fe010d4c10e8edb09 Mon Sep 17 00:00:00 2001 From: imunnic Date: Thu, 13 Mar 2025 09:39:53 +0100 Subject: [PATCH] =?UTF-8?q?bot=20y=20automatizaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 7 ++++--- app/requirements.txt | 2 +- app/telegrambot.py | 21 +++++++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index a594a8c..1e09a35 100644 --- a/app/main.py +++ b/app/main.py @@ -3,20 +3,21 @@ from contextlib import asynccontextmanager from database import Base, engine from routes import router from apscheduler.schedulers.background import BackgroundScheduler -from telegrambot import enviar_resumen_diario from webscrapper import search_from_keywords_file +import telegrambot # Crear las tablas en MySQL si no existen Base.metadata.create_all(bind=engine) # Configurar el scheduler scheduler = BackgroundScheduler() -scheduler.add_job(enviar_resumen_diario, "cron", hour=22, minute=0) # Ejecutar a las 08:00 +scheduler.add_job(telegrambot.enviar_resumen_diario, "cron", hour=22, minute=0) # Ejecutar a las 08:00 scheduler.add_job(search_from_keywords_file, "cron", hour=1, minute=0) #Ejecutar a las 01:00 @asynccontextmanager async def lifespan(app: FastAPI): - enviar_resumen_diario() + telegrambot.run() + telegrambot.enviar_resumen_diario() scheduler.start() yield scheduler.shutdown() diff --git a/app/requirements.txt b/app/requirements.txt index 10caf60..e455ae2 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -13,4 +13,4 @@ python-telegram-bot apscheduler pymysql lxml - +pyTelegramBotAPI diff --git a/app/telegrambot.py b/app/telegrambot.py index 7c143f0..1016970 100644 --- a/app/telegrambot.py +++ b/app/telegrambot.py @@ -1,4 +1,5 @@ -from telegram import Bot +import telebot +import threading from sqlalchemy.orm import Session from database import get_db from models import NewsItem @@ -17,7 +18,11 @@ logging.basicConfig( TOKEN = "7626026035:AAHEMp_iIN3y8AwywL0R6OTQvNi7EcJZ0iY" CHAT_ID = "-4731993289" # Reemplaza con el ID del grupo -bot = Bot(token=TOKEN) +bot = telebot.TeleBot(token=TOKEN) + +@bot.message_handler(commands=["start", "help"]) +def send_welcome(message): + bot.reply_to(message, "Welcome to YourBot! Type /info to get more information.") async def enviar_mensaje(mensaje: str): try: @@ -66,6 +71,8 @@ def obtener_titulares_por_keyword(): finally: db.close() # Cerrar la conexión + +@bot.message_handler(commands=["resumen"]) def enviar_resumen_diario(): """ Construye el mensaje con los titulares por keyword y lo envía por Telegram. @@ -85,3 +92,13 @@ def enviar_resumen_diario(): # Enviar mensaje de forma asíncrona asyncio.create_task(enviar_mensaje(mensaje)) + +# Función para correr el bot en un hilo separado +def start_bot(): + logging.info("Iniciando el bot") + bot.infinity_polling() + +# Iniciar en un hilo separado (solo cuando se llame explícitamente) +def run(): + thread = threading.Thread(target=start_bot, daemon=True) + thread.start() \ No newline at end of file