diff --git a/services/BotRouter.php b/services/BotRouter.php index e741148..f2cde89 100644 --- a/services/BotRouter.php +++ b/services/BotRouter.php @@ -108,6 +108,21 @@ class BotRouter return $response; } + // Only escalate to AI if bot type truly needs it; avoid AI for menu-driven bots + $config = self::getConfig($company); + $permType = (string)($context['permission_type'] ?? 1); + $perType = $config['per_type'][$permType] ?? []; + + // If a greeting_menu is configured for this category, use it as fallback instead of AI + $greetingMenuKey = $perType['greeting_menu'] ?? null; + if ($greetingMenuKey !== null) { + $menus = array_merge($config['menus'] ?? [], $perType['menus'] ?? []); + if (isset($menus[$greetingMenuKey])) { + self::log("NormalBot no manejó '{$input}' — mostrando menú de categoría en lugar de IA"); + return NormalBot::buildGreetingMenu($menus[$greetingMenuKey], $context['from'], $company); + } + } + self::log("NormalBot no manejó '{$input}', escalando a IA"); return self::runAiBot($company, $context, $input); } diff --git a/services/NormalBot.php b/services/NormalBot.php index ac29eeb..8b9f7cd 100644 --- a/services/NormalBot.php +++ b/services/NormalBot.php @@ -119,6 +119,10 @@ class NormalBot ConversationContext::reset($ctxId); return self::sendText('¿En qué más puedo ayudarte?', $context['from'], $company); })(), + 'goodbye' => (function () use ($ctxId, $context, $company) { + ConversationContext::reset($ctxId); + return self::sendText("Hasta luego 👋\n\nEscribe *menu* cuando quieras volver.", $context['from'], $company); + })(), 'forward_to_ai' => null, 'api_report' => self::executeApiReport($params, $context, $company, $ctxId), default => null, @@ -241,6 +245,11 @@ class NormalBot return self::sendText('✅ Reporte enviado.' . $nav, $context['from'], $company); } + public static function buildGreetingMenu(array $menu, string $to, array $company): ?array + { + return self::buildMenuResponse($menu, $to, $company); + } + private static function buildMenuResponse(array $menu, string $to, array $company): ?array { $menuType = $menu['type'] ?? 'list'; @@ -284,7 +293,7 @@ class NormalBot if ($menuType === 'button') { $buttons = []; - foreach ($menu['buttons'] ?? [] as $btn) { + foreach (array_slice($menu['buttons'] ?? [], 0, 3) as $btn) { $buttons[] = [ 'type' => 'reply', 'reply' => [