Webhook should not have a upddate before instancing the bot
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ekaropolus 2025-05-20 02:41:46 -06:00
parent 7414c37c77
commit 38fd3652f7

View File

@ -16,73 +16,83 @@ logger = logging.getLogger(__name__)
@csrf_exempt @csrf_exempt
async def telegram_webhook(request, bot_name): async def telegram_webhook(request, bot_name):
""" """
Webhook view that handles Telegram updates asynchronously and only uses LangChain. Webhook view that handles Telegram updates asynchronously and logs messages.
""" """
try: try:
logger.info(f"Webhook called for bot: {bot_name}") logger.info(f"Webhook called for bot: {bot_name}")
# Step 1: Fetch the bot instance asynchronously # 1) Fetch the bot instance
try: try:
bot_instance = await sync_to_async(TelegramBot.objects.get)(name=bot_name, is_active=True) bot_instance = await sync_to_async(TelegramBot.objects.get)(
# Step 1.5: get_or_create de la conversación name=bot_name, is_active=True
user_id = str(update.effective_user.id)
conv, _ = await sync_to_async(TelegramConversation.objects.get_or_create)(
bot=bot_instance,
user_id=user_id,
defaults={'started_at': timezone.now()}
) )
# Step 1.6: guardar mensaje entrante
incoming_text = update.message.text or ""
await sync_to_async(TelegramMessage.objects.create)(
conversation=conv,
direction=TelegramMessage.IN,
content=incoming_text
)
logger.info(f"Loaded bot configuration: {bot_instance}") logger.info(f"Loaded bot configuration: {bot_instance}")
except TelegramBot.DoesNotExist: except TelegramBot.DoesNotExist:
logger.error(f"Bot '{bot_name}' not found or inactive.") logger.error(f"Bot '{bot_name}' not found or inactive.")
return JsonResponse({"error": f"Bot '{bot_name}' not found."}, status=400) return JsonResponse({"error": f"Bot '{bot_name}' not found."}, status=400)
# Step 2: Ensure the bot has a LangChain assistant if request.method != "POST":
if not bot_instance.assistant: logger.warning("Received non-POST request")
logger.error(f"No assistant configured for bot '{bot_name}'.") return JsonResponse({"error": "Invalid request method"}, status=405)
return JsonResponse({"error": "Assistant not configured."}, status=400)
# Step 3: Process POST request from Telegram # 2) Parse the incoming update
if request.method == "POST": try:
try: payload = json.loads(request.body.decode("utf-8"))
request_body = json.loads(request.body.decode("utf-8")) update = Update.de_json(payload, Bot(token=bot_instance.token))
update = Update.de_json(request_body, Bot(token=bot_instance.token)) logger.info(f"Update received: {update}")
logger.info(f"Update received: {update}") except Exception as e:
except json.JSONDecodeError as e: logger.error(f"Failed to parse update: {e}")
logger.error(f"Failed to decode JSON: {e}") return JsonResponse({"error": "Invalid JSON payload"}, status=400)
return JsonResponse({"error": "Invalid JSON payload"}, status=400)
# Step 4: Route commands to the appropriate handlers # 3) Log conversation & inbound message
if update.message: user_id = str(update.effective_user.id)
if update.message.text == "/start": conv, _ = await sync_to_async(TelegramConversation.objects.get_or_create)(
await start(update) bot=bot_instance,
elif update.message.text == "/help": user_id=user_id,
await help_command(update) defaults={'started_at': timezone.now()}
elif update.message.text == "/dream_city": )
await dream_city_command(update) incoming_text = update.message.text or ""
elif update.message.location: await sync_to_async(TelegramMessage.objects.create)(
await handle_location(update) conversation=conv,
else: direction=TelegramMessage.IN,
# Step 5: Process AI-generated response using LangChain content=incoming_text
assistant_instance = await sync_to_async(LangchainAIService)(bot_instance.assistant) )
bot_response = await sync_to_async(assistant_instance.generate_response)(update.message.text)
# Step 6: Send the response back to Telegram # 4) Route commands or AI
await update.message.reply_text(bot_response) if update.message:
# built-in commands
if update.message.text == "/start":
await start(update)
elif update.message.text == "/help":
await help_command(update)
elif update.message.text == "/dream_city":
await dream_city_command(update)
elif update.message.location:
await handle_location(update)
else:
# AI fallback
assistant_instance = await sync_to_async(LangchainAIService)(
bot_instance.assistant
)
start_time = timezone.now()
bot_response = await sync_to_async(
assistant_instance.generate_response
)(update.message.text)
response_time = int((timezone.now() - start_time).total_seconds() * 1000)
return JsonResponse({"status": "ok"}) # Send reply
await update.message.reply_text(bot_response)
logger.warning("Received non-POST request") # 5) Log outbound message
return JsonResponse({"error": "Invalid request method"}, status=400) await sync_to_async(TelegramMessage.objects.create)(
conversation=conv,
direction=TelegramMessage.OUT,
content=bot_response,
response_time_ms=response_time
)
return JsonResponse({"status": "ok"})
except Exception as e: except Exception as e:
logger.error(f"Error in webhook: {e}") logger.error(f"Error in telegram_webhook: {e}")
return JsonResponse({"error": f"Unexpected error: {str(e)}"}, status=500) return JsonResponse({"error": f"Unexpected error: {e}"}, status=500)