This commit is contained in:
Lizandro Guarnizo
2026-01-24 02:15:54 -05:00
parent ab7299db2c
commit a481a20e9a
4 changed files with 128 additions and 1 deletions
+17
View File
@@ -780,6 +780,8 @@ class BotService {
$previousMenu = null;
}
try { error_log("[BotService] updateUserMenuState - phone={$phoneNumber} previousMenu=" . json_encode($previousMenu) . " newMenu=" . json_encode($menuId)); } catch (Throwable $t) {}
// Si hay un menú previo diferente al nuevo, empujarlo al historial
if (!empty($previousMenu) && $previousMenu != $menuId) {
try {
@@ -813,6 +815,21 @@ class BotService {
private function pushMenuToHistory(string $phoneNumber, $menuId) {
try {
$stateSvc = new ConversationStateService();
$currentState = $stateSvc->getState($phoneNumber);
// Si no existe una fila de estado, crearla con el historial inicial
if (!$currentState) {
try {
error_log("[BotService] pushMenuToHistory - no existing state, creating initial state with menu_history for phone={$phoneNumber} menuId={$menuId}");
$res = $stateSvc->setState($phoneNumber, ConversationStateService::STATE_INITIAL, ['menu_history' => [$menuId]]);
error_log('[BotService] pushMenuToHistory - setState result: ' . json_encode($res));
return $res;
} catch (Exception $e) {
error_log('[BotService] pushMenuToHistory create initial state failed: ' . $e->getMessage());
return false;
}
}
$history = (array)$stateSvc->getStateData($phoneNumber, 'menu_history');
if (!is_array($history)) $history = [];