From 4d7dd0f3064eba92dd109363a70db1f12cb2cc2d Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sun, 28 Jun 2026 13:15:12 -0500 Subject: [PATCH] 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 --- services/AiBot.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/services/AiBot.php b/services/AiBot.php index 2f09771..f4dd86c 100644 --- a/services/AiBot.php +++ b/services/AiBot.php @@ -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