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:
co-authored by
Claude Sonnet 4.6
parent
19667da4e9
commit
80dd7ee9ca
@@ -250,15 +250,29 @@ class TelegramBotService
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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)
|
$response = Http::timeout(60)
|
||||||
->withBasicAuth('whisper', $whisperToken)
|
->withBasicAuth('whisper', $whisperToken)
|
||||||
->attach('audio_file', $audioContent, 'audio.ogg')
|
->attach('audio_file', $audioContent, 'audio.ogg')
|
||||||
->post($whisperUrl, ['response_format' => 'json']);
|
->post($url);
|
||||||
|
|
||||||
$tiempoMs = (int) ((microtime(true) - $inicio) * 1000);
|
$tiempoMs = (int) ((microtime(true) - $inicio) * 1000);
|
||||||
$json = $response->json();
|
|
||||||
$texto = trim($json['text'] ?? $json['transcription'] ?? '');
|
// Intentar JSON primero, luego texto plano como fallback
|
||||||
$ok = $response->successful() && $texto !== '';
|
$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([
|
AiUsageLog::registrar([
|
||||||
'servicio' => 'whisper',
|
'servicio' => 'whisper',
|
||||||
|
|||||||
Reference in New Issue
Block a user