This commit is contained in:
parent
7119d35f7f
commit
9c10e2a226
@ -2,7 +2,7 @@ import os
|
||||
import json
|
||||
import logging
|
||||
|
||||
from openai import OpenAI
|
||||
import openai
|
||||
from telegram import Update, Bot
|
||||
from django.http import JsonResponse
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
@ -19,8 +19,9 @@ from .handlers import (
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Configura tu cliente de OpenAI
|
||||
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
||||
# Configura tu API Key de OpenAI
|
||||
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
|
||||
# -------------------------------
|
||||
# 🛠 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):
|
||||
# 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"
|
||||
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
|
||||
with open(download_path, "rb") as audio_file:
|
||||
transcript = client.audio.transcriptions.create(
|
||||
model="gpt-4o-transcribe", # o "whisper-1"
|
||||
file=audio_file,
|
||||
# 2. Envía el audio a la API Whisper de OpenAI
|
||||
with open(download_path, "rb") as audio:
|
||||
transcription = openai.audio.transcriptions.create(
|
||||
model="gpt-4o-transcribe",
|
||||
file=audio,
|
||||
response_format="text",
|
||||
language="es"
|
||||
)
|
||||
return transcript.text if hasattr(transcript, 'text') else transcript
|
||||
return transcription.text.strip()
|
||||
|
||||
|
||||
# -------------------------------
|
||||
# 🌐 Main webhook
|
||||
# -------------------------------
|
||||
|
||||
@csrf_exempt
|
||||
@ -116,7 +120,7 @@ async def telegram_webhook(request, bot_name):
|
||||
try:
|
||||
logger.info(f"Webhook called for bot: {bot_name}")
|
||||
|
||||
# Carga la configuración del bot (sync ORM)
|
||||
# Carga la configuración del bot
|
||||
try:
|
||||
bot_instance = await sync_to_async(TelegramBot.objects.get)(
|
||||
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."
|
||||
)
|
||||
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)
|
||||
return JsonResponse({"status": "ok"})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user