182 lines
6.1 KiB
Python
182 lines
6.1 KiB
Python
import os
|
|
import json
|
|
import logging
|
|
|
|
import openai
|
|
from telegram import Update, Bot
|
|
from django.http import JsonResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from .models import TelegramBot
|
|
from pxy_langchain.services import LangchainAIService
|
|
from .handlers import (
|
|
start, help_command, handle_location,
|
|
next_truck, report_trash, private_pickup, green_balance,
|
|
next_route, complete_stop, missed_stop, city_eco_score,
|
|
available_jobs, accept_job, next_pickup, complete_pickup, private_eco_score
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Configura tu API Key de OpenAI
|
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
# -------------------------------
|
|
# 🛠 Modular local handlers inside views.py
|
|
# -------------------------------
|
|
|
|
async def handle_location_message(update):
|
|
if update.message.location:
|
|
await handle_location(update)
|
|
return True
|
|
return False
|
|
|
|
|
|
async def dispatch_citizen_commands(update, text):
|
|
if text == "/start":
|
|
await start(update)
|
|
elif text == "/help":
|
|
await help_command(update)
|
|
elif text == "/next_truck":
|
|
await next_truck(update)
|
|
elif text == "/report_trash":
|
|
await report_trash(update)
|
|
elif text == "/private_pickup":
|
|
await private_pickup(update)
|
|
elif text == "/green_balance":
|
|
await green_balance(update)
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
|
|
async def dispatch_city_commands(update, text):
|
|
if text == "/start":
|
|
await start(update)
|
|
elif text == "/help":
|
|
await help_command(update)
|
|
elif text == "/next_route":
|
|
await next_route(update)
|
|
elif text == "/complete_stop":
|
|
await complete_stop(update)
|
|
elif text == "/missed_stop":
|
|
await missed_stop(update)
|
|
elif text == "/my_eco_score":
|
|
await city_eco_score(update)
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
|
|
async def dispatch_private_commands(update, text):
|
|
if text == "/start":
|
|
await start(update)
|
|
elif text == "/help":
|
|
await help_command(update)
|
|
elif text == "/available_jobs":
|
|
await available_jobs(update)
|
|
elif text.startswith("/accept_job"):
|
|
await accept_job(update)
|
|
elif text == "/next_pickup":
|
|
await next_pickup(update)
|
|
elif text == "/complete_pickup":
|
|
await complete_pickup(update)
|
|
elif text == "/my_eco_score":
|
|
await private_eco_score(update)
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
|
|
# -------------------------------
|
|
# 🛠 Voice transcription helper
|
|
# -------------------------------
|
|
|
|
async def transcribe_with_whisper(update, bot):
|
|
# 1. Descarga el archivo de voz directamente
|
|
tg_file = await bot.get_file(update.message.voice.file_id)
|
|
download_path = f"/tmp/{update.message.voice.file_id}.ogg"
|
|
# Con python-telegram-bot async: usa download_to_drive
|
|
await tg_file.download_to_drive(download_path)
|
|
|
|
# 2. Llama al endpoint de transcripción
|
|
with open(download_path, "rb") as audio:
|
|
resp = openai.audio.transcriptions.create(
|
|
model="gpt-4o-transcribe", # o "whisper-1" si prefieres
|
|
file=audio,
|
|
response_format="text",
|
|
language="es"
|
|
)
|
|
return resp.text.strip()
|
|
|
|
|
|
# -------------------------------
|
|
# 🌐 Main webhook
|
|
# -------------------------------
|
|
|
|
@csrf_exempt
|
|
async def telegram_webhook(request, bot_name):
|
|
try:
|
|
logger.info(f"Webhook called for bot: {bot_name}")
|
|
|
|
# Carga configuración del bot (sync ORM)
|
|
try:
|
|
bot_instance = await sync_to_async(TelegramBot.objects.get)(
|
|
name=bot_name, is_active=True
|
|
)
|
|
except TelegramBot.DoesNotExist:
|
|
return JsonResponse({"error": f"Bot '{bot_name}' not found."}, status=400)
|
|
|
|
if not bot_instance.assistant:
|
|
return JsonResponse({"error": "Assistant not configured."}, status=400)
|
|
|
|
if request.method != "POST":
|
|
return JsonResponse({"error": "Invalid request method"}, status=400)
|
|
|
|
# Parsea el update de Telegram
|
|
payload = json.loads(request.body.decode("utf-8"))
|
|
update = Update.de_json(payload, Bot(token=bot_instance.token))
|
|
|
|
if not update.message:
|
|
return JsonResponse({"status": "no message"})
|
|
|
|
# 1) Geolocalización
|
|
if await handle_location_message(update):
|
|
return JsonResponse({"status": "ok"})
|
|
|
|
# 2) Voz: transcribir con Whisper y llamar a report_trash
|
|
if update.message.voice:
|
|
bot = Bot(token=bot_instance.token)
|
|
transcript = await transcribe_with_whisper(update, bot)
|
|
if not transcript:
|
|
await update.message.reply_text(
|
|
"No pude entender tu mensaje de voz. Intenta de nuevo."
|
|
)
|
|
return JsonResponse({"status": "ok"})
|
|
# Patch interno para que report_trash use este texto
|
|
setattr(update.message, "_text", transcript)
|
|
await report_trash(update)
|
|
return JsonResponse({"status": "ok"})
|
|
|
|
# 3) Comandos de texto
|
|
text = update.message.text or ""
|
|
if bot_name == "PepeBasuritaCoinsBot" and await dispatch_citizen_commands(update, text):
|
|
return JsonResponse({"status": "ok"})
|
|
if bot_name == "PepeCamioncitoBot" and await dispatch_city_commands(update, text):
|
|
return JsonResponse({"status": "ok"})
|
|
if bot_name == "PepeMotitoBot" and await dispatch_private_commands(update, text):
|
|
return JsonResponse({"status": "ok"})
|
|
|
|
# 4) Fallback LLM
|
|
assistant_instance = await sync_to_async(LangchainAIService)(bot_instance.assistant)
|
|
bot_response = await sync_to_async(assistant_instance.generate_response)(text)
|
|
await update.message.reply_text(bot_response)
|
|
|
|
return JsonResponse({"status": "ok"})
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error in webhook: {e}")
|
|
return JsonResponse({"error": f"Unexpected error: {str(e)}"}, status=500)
|