bot y automatización
This commit is contained in:
@ -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()
|
||||
|
@ -13,4 +13,4 @@ python-telegram-bot
|
||||
apscheduler
|
||||
pymysql
|
||||
lxml
|
||||
|
||||
pyTelegramBotAPI
|
||||
|
@ -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()
|
Reference in New Issue
Block a user