From 7dde0f625ab1ac7c0b51da739092102cbd22c8fa Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 24 Jan 2026 11:06:59 -0500 Subject: [PATCH] Update BotService.php --- services/BotService.php | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/services/BotService.php b/services/BotService.php index ffa9d74..be038e6 100644 --- a/services/BotService.php +++ b/services/BotService.php @@ -231,6 +231,54 @@ class BotService { $isNav = in_array($normalized, ['menu','menú','inicio','atras','atrás','volver','back'], true) || is_numeric(trim($messageText)); if ($isNav) { try { error_log("[BotService] processMessage - bot_paused_until active but allowing navigation input for phone={$phoneNumber} message='{$messageText}'"); } catch (Throwable $t) {} + + // Si es un número, procesarlo inmediatamente como selección de menú + if (is_numeric(trim($messageText))) { + try { + $stateSvc2 = new ConversationStateService(); + $resolvedMenuId2 = $stateSvc2->getCurrentMenuId($phoneNumber); + } catch (Exception $e) { + $resolvedMenuId2 = null; + } + + if (empty($resolvedMenuId2)) { + try { + $cur = $this->db->fetch("SELECT current_menu_id FROM users WHERE phone_number = :phone", ['phone' => $phoneNumber]); + $resolvedMenuId2 = $cur ? $cur['current_menu_id'] : null; + } catch (Exception $e) { + $resolvedMenuId2 = null; + } + } + + // Si aún no hay menú activo, intentar con historial + if (empty($resolvedMenuId2)) { + try { + $hist = (array)$stateSvc2->getStateData($phoneNumber, 'menu_history'); + if (!empty($hist)) $resolvedMenuId2 = end($hist); + } catch (Exception $e) { + $resolvedMenuId2 = null; + } + } + + if (!empty($resolvedMenuId2)) { + // Asegurar que la BD refleje el menu activo para que processMenuSelection lo resuelva correctamente + try { + $this->db->update('users', ['current_menu_id' => $resolvedMenuId2, 'current_step' => 1], 'phone_number = :phone', ['phone' => $phoneNumber]); + try { + $stateSvc2->setCurrentMenu($phoneNumber, (int)$resolvedMenuId2); + } catch (Exception $e) { + // ignore + } + } catch (Exception $e) { + error_log('[BotService] failed to set current_menu_id before processing numeric selection during pause: ' . $e->getMessage()); + } + + $user['current_menu_id'] = $resolvedMenuId2; + $this->processMenuSelection($user, $messageText); + return; + } + } + // proceed to let navigation/menus be processed } else { return;