fix: AiBot returns null on API errors instead of hardcoded error string

Returning an error string caused the bot to send "Lo siento, tengo
problemas para procesar tu mensaje" to the user whenever OpenAI/Gemini
was unavailable or misconfigured. Now it returns null so BotRouter can
fall back to the category greeting menu instead of sending the error.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-28 13:15:12 -05:00
co-authored by Claude Sonnet 4.6
parent b6a85c33f7
commit 4d7dd0f306
+5 -12
View File
@@ -88,21 +88,13 @@ class AiBot
if ($httpCode !== 200 || $response === false) {
WpWebhook::log('ERROR', "OpenAI API error: {$error} HTTP:{$httpCode}");
return [
'content' => 'Lo siento, tengo problemas para procesar tu mensaje. Por favor intenta de nuevo más tarde.',
];
return null;
}
$data = json_decode($response, true);
$text = $data['choices'][0]['message']['content'] ?? null;
if ($text === null) {
return [
'content' => 'No pude generar una respuesta adecuada. ¿Puedes reformular tu pregunta?',
];
}
return ['content' => $text];
return $text !== null ? ['content' => $text] : null;
}
private static function callGemini(string $systemPrompt, array $history): ?array
@@ -146,13 +138,14 @@ class AiBot
curl_close($ch);
if ($httpCode !== 200 || $response === false) {
return ['content' => 'Lo siento, tengo problemas para procesar tu mensaje. Por favor intenta de nuevo.'];
WpWebhook::log('ERROR', "Gemini API error: {$error} HTTP:{$httpCode}");
return null;
}
$data = json_decode($response, true);
$text = $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
return $text !== null ? ['content' => $text] : ['content' => 'No pude generar una respuesta. ¿Puedes reformular tu pregunta?'];
return $text !== null ? ['content' => $text] : null;
}
private static function mockResponse(array $history): array