From 80dd7ee9ca7f3c201110d31cdea26c34262adc54 Mon Sep 17 00:00:00 2001 From: Lizandro Date: Tue, 14 Jul 2026 19:25:10 +0000 Subject: [PATCH] Fix Whisper response parsing: query param + plain text fallback - Send response_format=json as URL query param (not form field) - If JSON parse yields empty text, fall back to raw plain-text body - Prevents false 'error' logs when Whisper returns plain text Co-Authored-By: Claude Sonnet 4.6 --- app/Services/TelegramBotService.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index 29afc5d..421ee1d 100644 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -250,15 +250,29 @@ class TelegramBotService } try { + // response_format como query param (algunos servidores no lo leen del form) + $url = rtrim($whisperUrl, '?') . (str_contains($whisperUrl, '?') ? '&' : '?') . 'response_format=json'; + $response = Http::timeout(60) ->withBasicAuth('whisper', $whisperToken) ->attach('audio_file', $audioContent, 'audio.ogg') - ->post($whisperUrl, ['response_format' => 'json']); + ->post($url); $tiempoMs = (int) ((microtime(true) - $inicio) * 1000); - $json = $response->json(); - $texto = trim($json['text'] ?? $json['transcription'] ?? ''); - $ok = $response->successful() && $texto !== ''; + + // Intentar JSON primero, luego texto plano como fallback + $json = $response->json(); + $texto = trim($json['text'] ?? $json['transcription'] ?? ''); + + if ($texto === '' && $response->successful()) { + // El servidor devolvió texto plano + $raw = trim($response->body()); + if ($raw !== '' && ! str_starts_with($raw, '{') && ! str_starts_with($raw, '[')) { + $texto = $raw; + } + } + + $ok = $response->successful() && $texto !== ''; AiUsageLog::registrar([ 'servicio' => 'whisper',