diff --git a/bot_logic.py b/bot_logic.py index 1b2c49e..bb417e0 100644 --- a/bot_logic.py +++ b/bot_logic.py @@ -1,6 +1,7 @@ import os import asyncio import re +import logging from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes from orchestrator import ( @@ -12,6 +13,13 @@ import speech_recognition as sr from pydub import AudioSegment from gtts import gTTS +# Configuração de logging +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO +) +logger = logging.getLogger(__name__) + TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") ALLOWED_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID") @@ -74,8 +82,9 @@ async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE): else: # Fallback AI Agent from config import get_config + from ai_agent import query_agent_async cfg = get_config() - reply = query_agent(user_msg, override_provider=cfg.get("active_provider")) + reply = await query_agent_async(user_msg, override_provider=cfg.get("active_provider")) await update.message.reply_text(reply) async def handle_voice(update: Update, context: ContextTypes.DEFAULT_TYPE): @@ -94,6 +103,10 @@ def get_telegram_app(): return app if __name__ == "__main__": - app = get_telegram_app() - print("Bot iniciando...") - app.run_polling(drop_pending_updates=True) + logger.info("Bot começando a inicialização...") + try: + app = get_telegram_app() + logger.info("Bot online. Iniciando polling...") + app.run_polling(drop_pending_updates=True) + except Exception as e: + logger.error(f"Erro fatal ao iniciar bot: {e}") diff --git a/main.py b/main.py index ac0b851..133ffba 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ from fastapi.templating import Jinja2Templates from dotenv import load_dotenv from starlette.concurrency import run_in_threadpool -from ai_agent import query_agent +from ai_agent import query_agent_async from config import get_config, save_config from credential_manager import fetch_from_gitea_repo_async from orchestrator import ( @@ -58,7 +58,7 @@ async def get_system_status(is_auth: bool = Depends(verify_password)): async def web_chat(message: dict, is_auth: bool = Depends(verify_password)): user_text = message.get("text", "") if not user_text: return {"reply": "Vazio."} - reply = query_agent(user_text) + reply = await query_agent_async(user_text) return {"reply": reply} @app.post("/api/orchestrate") diff --git a/start.sh b/start.sh index 4625dd4..3692e11 100755 --- a/start.sh +++ b/start.sh @@ -1,13 +1,15 @@ #!/bin/bash -# Tenta limpar processos conflitantes no host se tiver acesso privilégido +# Tenta limpar processos conflitantes no host se tiver acesso privilegiado if [ -d "/host_root" ]; then echo "Limpando processos conflitantes no HOST..." - chroot /host_root /bin/bash -c "pkill -f telegram_bot.js" || true - chroot /host_root /bin/bash -c "pkill -f bot_logic.py" || true + chroot /host_root /bin/bash -c "pkill -9 -f telegram_bot.js" || true + chroot /host_root /bin/bash -c "pkill -9 -f bot_logic.py" || true fi -# Inicia o serviço web +# Inicia o serviço web com log unbuffered +echo "Iniciando Uvicorn..." uvicorn main:app --host 0.0.0.0 --port 8000 & -# Inicia o Polling do Bot -python bot_logic.py +# Inicia o Polling do Bot com log unbuffered +echo "Iniciando Bot (bot_logic.py)..." +python3 -u bot_logic.py