From d0c1b1d3be1e408308a8a58d829b15ff523b9e00 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sun, 5 Jul 2026 21:38:25 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20mid-flow=20audio=20=E2=80=94=20transcri?= =?UTF-8?q?bed=20text=20goes=20to=20NormalBot,=20not=20NLU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a user sends audio while in an active flow (__cap, __foreach, collecting, __api_vars), the Whisper transcription is passed directly to NormalBot as the step answer instead of going through NLU routing. Bot still shows '🎤 Escuché: _texto_' confirmation before processing. Co-Authored-By: Claude Sonnet 4.6 --- services/BotRouter.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/services/BotRouter.php b/services/BotRouter.php index 126f0aa..67d36a2 100644 --- a/services/BotRouter.php +++ b/services/BotRouter.php @@ -125,13 +125,20 @@ class BotRouter if ($text !== null && trim($text) !== '') { self::log("NLU media [{$inputType}]: texto extraído → \"{$text}\""); - // Confirmar al usuario lo que entendió - $prefix = $inputType === 'audio' ? '🎤 Transcripción' : '🖼️ Imagen analizada'; + $prefix = $inputType === 'audio' ? '🎤 Escuché' : '🖼️ Imagen analizada'; WhatsAppSender::sendText( $context['from'], "{$prefix}: _{$text}_", $context['phone_number_id'] ?? '' ); + + // Si el usuario está en medio de un flujo activo, el texto va directo + // a NormalBot como respuesta al paso actual — no al NLU + if (self::hasActiveFlow((int)$company['id'], $context['from'])) { + self::log("NLU media: flujo activo detectado → NormalBot recibe \"{$text}\""); + return self::runNormalBot($company, $context, $text, 'text', false); + } + return self::runNluOnText($company, $context, $text, $config); } } @@ -155,6 +162,13 @@ class BotRouter return self::categoryMenuFallback($company, $context, $config); } + private static function hasActiveFlow(int $companyId, string $phone): bool + { + $botCtx = ConversationContext::getOrCreate($companyId, $phone, 'normal'); + $meta = ConversationContext::getMetadata((int)$botCtx['id']); + return !empty($meta['__cap']) || !empty($meta['__foreach']) || !empty($meta['collecting']) || !empty($meta['__api_vars']); + } + private static function runNluOnText(array $company, array $context, string $input, array $config): ?array { $result = AiBot::routeOrChat($company, $context, $input);