feat: Add database health checks and host configuration, enhance the start command with a quick reply keyboard, and improve general text cleaning.

This commit is contained in:
Marco Gallegos
2025-12-14 15:33:47 -06:00
parent cf128960cb
commit cbfcee557a
6 changed files with 40 additions and 16 deletions

View File

@@ -39,6 +39,13 @@ def _build_engine():
logging.error(f"No se pudo crear el engine de base de datos: {exc}")
return None
def _disable_db_logging(reason: str):
"""Deshabilita el logging a DB después de un error para evitar spam."""
global engine, SessionLocal
engine = None
SessionLocal = None
logging.warning(f"DB logging deshabilitado: {reason}")
# Crear el engine y sesión si es posible
engine = _build_engine()
metadata = MetaData() if engine else None
@@ -46,6 +53,7 @@ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) if e
# Función para inicializar la base de datos
def init_db():
global engine, SessionLocal
if not engine:
return
try:
@@ -54,6 +62,7 @@ def init_db():
logging.info("Tablas verificadas/creadas correctamente.")
except Exception as e:
logging.error(f"Error al inicializar la base de datos: {e}")
_disable_db_logging("no se pudo inicializar la base de datos (se omitirán logs).")
# No propagamos para que el bot pueda seguir levantando aunque no haya DB
# Función para registrar una solicitud en la base de datos
@@ -62,7 +71,12 @@ def log_request(telegram_id, username, command, message):
logging.debug("Log de DB omitido (DB no configurada).")
return
db_session = SessionLocal()
try:
db_session = SessionLocal()
except Exception as exc:
logging.error(f"No se pudo crear sesión DB, se deshabilita el log: {exc}")
_disable_db_logging("no se pudo abrir sesión")
return
try:
log_entry = RequestLog(
telegram_id=str(telegram_id),

View File

@@ -32,12 +32,12 @@ logging.basicConfig(
)
# Convertimos la string del webhook en una lista (por si en el futuro hay varios separados por coma)
# Se aceptan los nombres WEBHOOK_CONTRATO (nuevo) y WEBHOOK_ONBOARDING (legacy).
_webhook_raw = os.getenv("WEBHOOK_CONTRATO") or os.getenv("WEBHOOK_ONBOARDING") or ""
# Se aceptan los nombres WEBHOOK_ONBOARDING (principal) y WEBHOOK_CONTRATO (alias).
_webhook_raw = os.getenv("WEBHOOK_ONBOARDING") or os.getenv("WEBHOOK_CONTRATO") or ""
WEBHOOK_URLS = [w.strip() for w in _webhook_raw.split(",") if w.strip()]
if not WEBHOOK_URLS:
logging.warning("No se configuró WEBHOOK_CONTRATO/WEBHOOK_ONBOARDING; el onboarding no enviará datos.")
logging.warning("No se configuró WEBHOOK_ONBOARDING (o alias WEBHOOK_CONTRATO); el onboarding no enviará datos.")
# --- 2. ESTADOS DEL FLUJO ---
(
@@ -63,7 +63,8 @@ def normalizar_id(texto: str) -> str:
return "N/A" if limpio == "0" else limpio
def limpiar_texto_general(texto: str) -> str:
t = texto.strip()
# Colapsa espacios múltiples que deja el autocorrector y recorta extremos
t = " ".join(texto.split())
return "N/A" if t == "0" else t
# --- 4. TECLADOS DINÁMICOS ---