diff --git a/app/Http/Livewire/Chat/ShowConfiguracionChat.php b/app/Http/Livewire/Chat/ShowConfiguracionChat.php index 30d85b6..3dc4e93 100644 --- a/app/Http/Livewire/Chat/ShowConfiguracionChat.php +++ b/app/Http/Livewire/Chat/ShowConfiguracionChat.php @@ -99,9 +99,15 @@ class ShowConfiguracionChat extends Component $data = $response->json(); - $this->webhookResult = ($data['ok'] ?? false) - ? '✅ Webhook registrado correctamente.' - : '❌ Error de Telegram: ' . ($data['description'] ?? 'respuesta desconocida'); + if (! ($data['ok'] ?? false)) { + $this->webhookResult = '❌ Error de Telegram: ' . ($data['description'] ?? 'respuesta desconocida'); + return; + } + + // Registrar comandos del bot (/menu, /salir, /cancelar) + app(\App\Services\TelegramBotService::class)->registrarComandos(); + + $this->webhookResult = '✅ Webhook registrado y comandos configurados correctamente.'; } catch (\Throwable $e) { $this->webhookResult = '❌ No se pudo conectar con Telegram: ' . $e->getMessage(); diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index 3686f7e..9d8e0a0 100644 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -60,9 +60,22 @@ class TelegramBotService } } - $lower = strtolower($text); - if (in_array($lower, ['/start', '/menu', 'menu', 'inicio', 'hola'])) { + $lower = strtolower(trim($text)); + + // Palabras que siempre regresan al menú, desde cualquier punto del flujo + $menuTriggers = [ + '/start', '/menu', '/salir', '/inicio', '/cancelar', + 'menu', 'menú', 'inicio', 'hola', 'salir', 'volver', + 'regresar', 'cancelar', 'empezar', 'comenzar', 'start', + ]; + + if (in_array($lower, $menuTriggers)) { if ($state['user_id'] ?? null) { + // Limpiar cualquier flujo pendiente + $cleanState = $this->getState($chatId); + unset($cleanState['step']); + $cleanState['step'] = 'idle'; + $this->setState($chatId, $cleanState); $this->showMainMenu($chatId); } else { $this->askEmail($chatId); @@ -1049,6 +1062,18 @@ class TelegramBotService // ─── Telegram API ───────────────────────────────────────── + public function registrarComandos(): bool + { + return $this->apiCall('setMyCommands', [ + 'commands' => [ + ['command' => 'start', 'description' => 'Iniciar / Menú principal'], + ['command' => 'menu', 'description' => 'Ver el menú principal'], + ['command' => 'salir', 'description' => 'Volver al menú desde cualquier punto'], + ['command' => 'cancelar','description' => 'Cancelar la acción actual'], + ], + ]); + } + public function send(string $chatId, string $text): bool { if ($this->convId) {