Ekaropolus 38fd3652f7
All checks were successful
continuous-integration/drone/push Build is passing
Webhook should not have a upddate before instancing the bot
2025-05-20 02:41:46 -06:00

99 lines
3.8 KiB
Python

import json
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 dream_city_command, start, help_command, handle_location
import logging
from .models import TelegramConversation, TelegramMessage, TelegramBot
from django.utils import timezone
logger = logging.getLogger(__name__)
@csrf_exempt
async def telegram_webhook(request, bot_name):
"""
Webhook view that handles Telegram updates asynchronously and logs messages.
"""
try:
logger.info(f"Webhook called for bot: {bot_name}")
# 1) Fetch the bot instance
try:
bot_instance = await sync_to_async(TelegramBot.objects.get)(
name=bot_name, is_active=True
)
logger.info(f"Loaded bot configuration: {bot_instance}")
except TelegramBot.DoesNotExist:
logger.error(f"Bot '{bot_name}' not found or inactive.")
return JsonResponse({"error": f"Bot '{bot_name}' not found."}, status=400)
if request.method != "POST":
logger.warning("Received non-POST request")
return JsonResponse({"error": "Invalid request method"}, status=405)
# 2) Parse the incoming update
try:
payload = json.loads(request.body.decode("utf-8"))
update = Update.de_json(payload, Bot(token=bot_instance.token))
logger.info(f"Update received: {update}")
except Exception as e:
logger.error(f"Failed to parse update: {e}")
return JsonResponse({"error": "Invalid JSON payload"}, status=400)
# 3) Log conversation & inbound message
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()}
)
incoming_text = update.message.text or ""
await sync_to_async(TelegramMessage.objects.create)(
conversation=conv,
direction=TelegramMessage.IN,
content=incoming_text
)
# 4) Route commands or AI
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)
# Send reply
await update.message.reply_text(bot_response)
# 5) Log outbound message
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:
logger.error(f"Error in telegram_webhook: {e}")
return JsonResponse({"error": f"Unexpected error: {e}"}, status=500)