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 <noreply@anthropic.com>
This commit is contained in:
Lizandro
2026-07-14 19:25:10 +00:00
co-authored by Claude Sonnet 4.6
parent 19667da4e9
commit 80dd7ee9ca
+18 -4
View File
@@ -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',