Add menu/salir commands to Telegram bot
- Any message matching salir/menu/volver/cancelar/inicio resets flow and shows main menu - Register /menu /salir /cancelar as official bot commands via setMyCommands - Commands are registered automatically when saving the webhook from admin panel Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
b0c1407f2d
commit
3f2bdc0d47
@@ -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();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user