ahort change to recieve text
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ekaropolus 2025-07-07 04:26:11 -06:00
parent 89a7d91be7
commit 692a342f77

View File

@ -18,15 +18,9 @@ from .handlers import (
)
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)
@ -90,38 +84,30 @@ async def dispatch_private_commands(update, text):
return True
# -------------------------------
# 🛠 Voice transcription helper
# -------------------------------
async def transcribe_with_whisper(update, bot):
# 1. Descarga el archivo de voz directamente
# 1) Descarga el audio
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
# 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
# Como response_format="text", esto retorna un str
transcript_str = openai.audio.transcriptions.create(
model="gpt-4o-transcribe", # o "whisper-1"
file=audio,
response_format="text",
language="es"
)
return resp.text.strip()
return transcript_str.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)
# Carga bot (solo ORM en sync_to_async)
try:
bot_instance = await sync_to_async(TelegramBot.objects.get)(
name=bot_name, is_active=True
@ -131,11 +117,9 @@ async def telegram_webhook(request, bot_name):
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))
@ -146,7 +130,7 @@ async def telegram_webhook(request, bot_name):
if await handle_location_message(update):
return JsonResponse({"status": "ok"})
# 2) Voz: transcribir con Whisper y llamar a report_trash
# 2) Voz: transcribe y report_trash
if update.message.voice:
bot = Bot(token=bot_instance.token)
transcript = await transcribe_with_whisper(update, bot)
@ -155,7 +139,6 @@ async def telegram_webhook(request, bot_name):
"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"})