Text problem in async
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ekaropolus 2025-07-07 03:55:09 -06:00
parent 7119d35f7f
commit 9c10e2a226

View File

@ -2,7 +2,7 @@ import os
import json import json
import logging import logging
from openai import OpenAI import openai
from telegram import Update, Bot from telegram import Update, Bot
from django.http import JsonResponse from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
@ -19,8 +19,9 @@ from .handlers import (
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Configura tu cliente de OpenAI # Configura tu API Key de OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) openai.api_key = os.getenv("OPENAI_API_KEY")
# ------------------------------- # -------------------------------
# 🛠 Modular local handlers inside views.py # 🛠 Modular local handlers inside views.py
@ -90,25 +91,28 @@ async def dispatch_private_commands(update, text):
# ------------------------------- # -------------------------------
# 🛠 Voice transcription helper (Whisper via OpenAI client) # 🛠 Voice transcription helper
# ------------------------------- # -------------------------------
async def transcribe_with_whisper(update, bot): async def transcribe_with_whisper(update, bot):
# 1. Descarga el archivo de voz desde Telegram # 1. Descarga el archivo de voz desde Telegram
tg_file = await bot.get_file(update.message.voice.file_id) tg_file = await sync_to_async(bot.get_file)(update.message.voice.file_id)
download_path = f"/tmp/{update.message.voice.file_id}.ogg" download_path = f"/tmp/{update.message.voice.file_id}.ogg"
await tg_file.download_to_drive(download_path) await sync_to_async(tg_file.download)(download_path)
# 2. Envía el audio a la API de OpenAI para transcripción # 2. Envía el audio a la API Whisper de OpenAI
with open(download_path, "rb") as audio_file: with open(download_path, "rb") as audio:
transcript = client.audio.transcriptions.create( transcription = openai.audio.transcriptions.create(
model="gpt-4o-transcribe", # o "whisper-1" model="gpt-4o-transcribe",
file=audio_file, file=audio,
response_format="text", response_format="text",
language="es" language="es"
) )
return transcript.text if hasattr(transcript, 'text') else transcript return transcription.text.strip()
# -------------------------------
# 🌐 Main webhook
# ------------------------------- # -------------------------------
@csrf_exempt @csrf_exempt
@ -116,7 +120,7 @@ async def telegram_webhook(request, bot_name):
try: try:
logger.info(f"Webhook called for bot: {bot_name}") logger.info(f"Webhook called for bot: {bot_name}")
# Carga la configuración del bot (sync ORM) # Carga la configuración del bot
try: try:
bot_instance = await sync_to_async(TelegramBot.objects.get)( bot_instance = await sync_to_async(TelegramBot.objects.get)(
name=bot_name, is_active=True name=bot_name, is_active=True
@ -155,7 +159,8 @@ async def telegram_webhook(request, bot_name):
"No pude entender tu mensaje de voz. Intenta de nuevo." "No pude entender tu mensaje de voz. Intenta de nuevo."
) )
return JsonResponse({"status": "ok"}) return JsonResponse({"status": "ok"})
update.message.text = transcript # Monkey-patch para que report_trash lea el texto
setattr(update.message, "_text", transcript)
await report_trash(update) await report_trash(update)
return JsonResponse({"status": "ok"}) return JsonResponse({"status": "ok"})