diff --git a/pxy_bots/views.py b/pxy_bots/views.py index 3efb340..a04cefb 100644 --- a/pxy_bots/views.py +++ b/pxy_bots/views.py @@ -15,11 +15,89 @@ from .handlers import ( logger = logging.getLogger(__name__) + +# ------------------------------- +# 🛠 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 handle_voice_or_general_message(update): + if update.message.voice: + file_id = update.message.voice.file_id + await update.message.reply_text( + f"🎙 Recibí tu mensaje de voz con ID {file_id}. Pronto calcularé tu CO₂." + ) + 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 + + +# ------------------------------- +# 🌐 Main webhook +# ------------------------------- + @csrf_exempt async def telegram_webhook(request, bot_name): - """ - Webhook view that routes each bot to its own set of handlers based on bot_name. - """ try: logger.info(f"Webhook called for bot: {bot_name}") @@ -46,63 +124,28 @@ async def telegram_webhook(request, bot_name): if update.message: text = update.message.text or "" - # Si hay localización primero - if update.message.location: - await handle_location(update) + # Handle location always first + if await handle_location_message(update): return JsonResponse({"status": "ok"}) - # Primero procesar con LLM para cualquier texto + # Handle voice or general simple responses next + if await handle_voice_or_general_message(update): + return JsonResponse({"status": "ok"}) + + # Then dispatch commands per bot + if bot_name == "PepeBasuritaCoinsBot": + if await dispatch_citizen_commands(update, text): + return JsonResponse({"status": "ok"}) + elif bot_name == "PepeCamioncitoBot": + if await dispatch_city_commands(update, text): + return JsonResponse({"status": "ok"}) + elif bot_name == "PepeMotitoBot": + if await dispatch_private_commands(update, text): + return JsonResponse({"status": "ok"}) + + # Otherwise fallback to LLM assistant_instance = await sync_to_async(LangchainAIService)(bot_instance.assistant) bot_response = await sync_to_async(assistant_instance.generate_response)(text) - - # 🚀 Citizen bot - if bot_name == "PepeBasuritaCoinsBot": - if text == "/start": - await start(update); return JsonResponse({"status": "ok"}) - elif text == "/help": - await help_command(update); return JsonResponse({"status": "ok"}) - elif text == "/next_truck": - await next_truck(update); return JsonResponse({"status": "ok"}) - elif text == "/report_trash": - await report_trash(update); return JsonResponse({"status": "ok"}) - elif text == "/private_pickup": - await private_pickup(update); return JsonResponse({"status": "ok"}) - elif text == "/green_balance": - await green_balance(update); return JsonResponse({"status": "ok"}) - - # 🚚 City collector bot - elif bot_name == "PepeCamioncitoBot": - if text == "/start": - await start(update); return JsonResponse({"status": "ok"}) - elif text == "/help": - await help_command(update); return JsonResponse({"status": "ok"}) - elif text == "/next_route": - await next_route(update); return JsonResponse({"status": "ok"}) - elif text == "/complete_stop": - await complete_stop(update); return JsonResponse({"status": "ok"}) - elif text == "/missed_stop": - await missed_stop(update); return JsonResponse({"status": "ok"}) - elif text == "/my_eco_score": - await city_eco_score(update); return JsonResponse({"status": "ok"}) - - # 🚛 Private collector bot - elif bot_name == "PepeMotitoBot": - if text == "/start": - await start(update); return JsonResponse({"status": "ok"}) - elif text == "/help": - await help_command(update); return JsonResponse({"status": "ok"}) - elif text == "/available_jobs": - await available_jobs(update); return JsonResponse({"status": "ok"}) - elif text.startswith("/accept_job"): - await accept_job(update); return JsonResponse({"status": "ok"}) - elif text == "/next_pickup": - await next_pickup(update); return JsonResponse({"status": "ok"}) - elif text == "/complete_pickup": - await complete_pickup(update); return JsonResponse({"status": "ok"}) - elif text == "/my_eco_score": - await private_eco_score(update); return JsonResponse({"status": "ok"}) - - # Si no fue comando, respondemos con LLM await update.message.reply_text(bot_response) return JsonResponse({"status": "ok"})