mirror of
https://github.com/marcogll/talia_bot.git
synced 2026-01-13 21:35:19 +00:00
Remove empty placeholder modules talia_bot/modules/printer.py and talia_bot/modules/sales_rag.py. Update import paths across multiple modules to be absolute from the project root (talia_bot), improving consistency. Corrected import paths for config variables and utility functions. Standardized the use of the ADMIN_ID configuration variable, resolving a discrepancy where OWNER_CHAT_ID was used. Removed a duplicated docstring in scheduler.py.
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
# app/scheduler.py
|
|
# Este script se encarga de programar tareas automáticas, como el resumen diario.
|
|
|
|
# app/scheduler.py
|
|
# Este script se encarga de programar tareas automáticas, como el resumen diario.
|
|
|
|
import logging
|
|
from datetime import time
|
|
from telegram.ext import ContextTypes
|
|
import pytz
|
|
|
|
from talia_bot.config import ADMIN_ID, TIMEZONE, DAILY_SUMMARY_TIME
|
|
from talia_bot.modules.agenda import get_agenda
|
|
|
|
# Configuramos el registro de eventos (logging) para ver qué pasa en la consola
|
|
logger = logging.getLogger(__name__)
|
|
|
|
async def send_daily_summary(context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
"""
|
|
Función que envía el resumen diario al dueño del bot.
|
|
Se ejecuta automáticamente según lo programado.
|
|
"""
|
|
job = context.job
|
|
chat_id = job.chat_id
|
|
|
|
logger.info(f"Ejecutando tarea de resumen diario para el chat_id: {chat_id}")
|
|
|
|
try:
|
|
# Obtenemos la agenda del día
|
|
agenda_text = get_agenda()
|
|
# Preparamos el mensaje
|
|
summary_text = f"🔔 *Resumen Diario - Buen día, Marco!*\n\n{agenda_text}"
|
|
|
|
# Enviamos el mensaje por Telegram
|
|
await context.bot.send_message(
|
|
chat_id=chat_id,
|
|
text=summary_text,
|
|
parse_mode='Markdown'
|
|
)
|
|
logger.info(f"Resumen diario enviado con éxito a {chat_id}")
|
|
except Exception as e:
|
|
# Si hay un error, lo registramos
|
|
logger.error(f"Error al enviar el resumen diario a {chat_id}: {e}")
|
|
|
|
def schedule_daily_summary(application) -> None:
|
|
"""
|
|
Programa la tarea del resumen diario para que ocurra todos los días.
|
|
"""
|
|
# Si no hay un ID de dueño configurado, no programamos nada
|
|
if not ADMIN_ID:
|
|
logger.warning("ADMIN_ID no configurado. No se programará el resumen diario.")
|
|
return
|
|
|
|
job_queue = application.job_queue
|
|
|
|
# Configuramos la zona horaria (ej. America/Mexico_City)
|
|
tz = pytz.timezone(TIMEZONE)
|
|
|
|
# Obtenemos la hora y minutos desde la configuración (ej. "07:00")
|
|
try:
|
|
hour, minute = map(int, DAILY_SUMMARY_TIME.split(':'))
|
|
except ValueError:
|
|
logger.error(f"Formato de DAILY_SUMMARY_TIME inválido: {DAILY_SUMMARY_TIME}. Usando 07:00 por defecto.")
|
|
hour, minute = 7, 0
|
|
|
|
# Programamos la tarea para que corra todos los días a la hora configurada
|
|
scheduled_time = time(hour=hour, minute=minute, tzinfo=tz)
|
|
|
|
job_queue.run_daily(
|
|
send_daily_summary,
|
|
time=scheduled_time,
|
|
chat_id=int(ADMIN_ID),
|
|
name="daily_summary"
|
|
)
|
|
|
|
logger.info(f"Resumen diario programado para {ADMIN_ID} a las {scheduled_time} ({TIMEZONE})")
|