feat: Add asynchronous support and improved logging/error handling for calendar functions, introduce a calendar debug script, and refactor role-based menu logic.

This commit is contained in:
Marco Gallegos
2025-12-18 09:24:54 -06:00
parent 54f4e9ee41
commit 9a83fb93bb
6 changed files with 87 additions and 28 deletions

View File

@@ -3,31 +3,42 @@
# Permite obtener y mostrar las actividades programadas para el día.
import datetime
import logging
from google_calendar import get_events
def get_agenda():
logger = logging.getLogger(__name__)
async def get_agenda():
"""
Obtiene y muestra la agenda del usuario para el día actual desde Google Calendar.
"""
now = datetime.datetime.utcnow()
start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = start_of_day + datetime.timedelta(days=1)
try:
logger.info("Obteniendo agenda...")
now = datetime.datetime.now(datetime.timezone.utc)
start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = start_of_day + datetime.timedelta(days=1)
events = get_events(start_of_day, end_of_day)
logger.info(f"Buscando eventos desde {start_of_day} hasta {end_of_day}")
events = get_events(start_of_day, end_of_day)
if not events:
return "📅 *Agenda para Hoy*\n\nNo tienes eventos programados para hoy."
if not events:
logger.info("No se encontraron eventos.")
return "📅 *Agenda para Hoy*\n\nNo tienes eventos programados para hoy."
agenda_text = "📅 *Agenda para Hoy*\n\n"
for event in events:
start = event["start"].get("dateTime", event["start"].get("date"))
# Formatear la hora si es posible
if "T" in start:
time_str = start.split("T")[1][:5]
else:
time_str = "Todo el día"
summary = event.get("summary", "(Sin título)")
agenda_text += f"• *{time_str}* - {summary}\n"
agenda_text = "📅 *Agenda para Hoy*\n\n"
for event in events:
start = event["start"].get("dateTime", event["start"].get("date"))
# Formatear la hora si es posible
if "T" in start:
time_str = start.split("T")[1][:5]
else:
time_str = "Todo el día"
summary = event.get("summary", "(Sin título)")
agenda_text += f"• *{time_str}* - {summary}\n"
return agenda_text
logger.info("Agenda obtenida con éxito.")
return agenda_text
except Exception as e:
logger.error(f"Error al obtener la agenda: {e}")
return "❌ Error al obtener la agenda. Por favor, intenta de nuevo más tarde."

View File

@@ -55,9 +55,7 @@ def handle_start(user_role):
"""
welcome_message = "Hola, soy Talía. ¿En qué puedo ayudarte hoy?"
if user_role == "owner":
menu = get_owner_menu()
elif user_role == "admin":
if user_role in ["owner", "admin"]:
menu = get_admin_menu()
elif user_role == "team":
menu = get_team_menu()

View File

@@ -13,8 +13,8 @@ from telegram.ext import (
ContextTypes,
)
from app.config import VIKUNJA_API_URL, VIKUNJA_API_TOKEN
from app.permissions import is_admin
from config import VIKUNJA_API_URL, VIKUNJA_API_TOKEN
from permissions import is_admin
# Configuración del logger
logger = logging.getLogger(__name__)