ahort change to recieve text
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
89a7d91be7
commit
692a342f77
@ -18,15 +18,9 @@ from .handlers import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Configura tu API Key de OpenAI
|
|
||||||
openai.api_key = os.getenv("OPENAI_API_KEY")
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------
|
|
||||||
# 🛠 Modular local handlers inside views.py
|
|
||||||
# -------------------------------
|
|
||||||
|
|
||||||
async def handle_location_message(update):
|
async def handle_location_message(update):
|
||||||
if update.message.location:
|
if update.message.location:
|
||||||
await handle_location(update)
|
await handle_location(update)
|
||||||
@ -90,38 +84,30 @@ async def dispatch_private_commands(update, text):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------
|
|
||||||
# 🛠 Voice transcription helper
|
|
||||||
# -------------------------------
|
|
||||||
|
|
||||||
async def transcribe_with_whisper(update, bot):
|
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)
|
tg_file = await 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"
|
||||||
# Con python-telegram-bot async: usa download_to_drive
|
|
||||||
await tg_file.download_to_drive(download_path)
|
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:
|
with open(download_path, "rb") as audio:
|
||||||
resp = openai.audio.transcriptions.create(
|
# Como response_format="text", esto retorna un str
|
||||||
model="gpt-4o-transcribe", # o "whisper-1" si prefieres
|
transcript_str = openai.audio.transcriptions.create(
|
||||||
|
model="gpt-4o-transcribe", # o "whisper-1"
|
||||||
file=audio,
|
file=audio,
|
||||||
response_format="text",
|
response_format="text",
|
||||||
language="es"
|
language="es"
|
||||||
)
|
)
|
||||||
return resp.text.strip()
|
return transcript_str.strip()
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------
|
|
||||||
# 🌐 Main webhook
|
|
||||||
# -------------------------------
|
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
async def telegram_webhook(request, bot_name):
|
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 configuración del bot (sync ORM)
|
# Carga bot (solo ORM en sync_to_async)
|
||||||
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
|
||||||
@ -131,11 +117,9 @@ async def telegram_webhook(request, bot_name):
|
|||||||
|
|
||||||
if not bot_instance.assistant:
|
if not bot_instance.assistant:
|
||||||
return JsonResponse({"error": "Assistant not configured."}, status=400)
|
return JsonResponse({"error": "Assistant not configured."}, status=400)
|
||||||
|
|
||||||
if request.method != "POST":
|
if request.method != "POST":
|
||||||
return JsonResponse({"error": "Invalid request method"}, status=400)
|
return JsonResponse({"error": "Invalid request method"}, status=400)
|
||||||
|
|
||||||
# Parsea el update de Telegram
|
|
||||||
payload = json.loads(request.body.decode("utf-8"))
|
payload = json.loads(request.body.decode("utf-8"))
|
||||||
update = Update.de_json(payload, Bot(token=bot_instance.token))
|
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):
|
if await handle_location_message(update):
|
||||||
return JsonResponse({"status": "ok"})
|
return JsonResponse({"status": "ok"})
|
||||||
|
|
||||||
# 2) Voz: transcribir con Whisper y llamar a report_trash
|
# 2) Voz: transcribe y report_trash
|
||||||
if update.message.voice:
|
if update.message.voice:
|
||||||
bot = Bot(token=bot_instance.token)
|
bot = Bot(token=bot_instance.token)
|
||||||
transcript = await transcribe_with_whisper(update, bot)
|
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."
|
"No pude entender tu mensaje de voz. Intenta de nuevo."
|
||||||
)
|
)
|
||||||
return JsonResponse({"status": "ok"})
|
return JsonResponse({"status": "ok"})
|
||||||
# Patch interno para que report_trash use este texto
|
|
||||||
setattr(update.message, "_text", transcript)
|
setattr(update.message, "_text", transcript)
|
||||||
await report_trash(update)
|
await report_trash(update)
|
||||||
return JsonResponse({"status": "ok"})
|
return JsonResponse({"status": "ok"})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user