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();
|
$data = $response->json();
|
||||||
|
|
||||||
$this->webhookResult = ($data['ok'] ?? false)
|
if (! ($data['ok'] ?? false)) {
|
||||||
? '✅ Webhook registrado correctamente.'
|
$this->webhookResult = '❌ Error de Telegram: ' . ($data['description'] ?? 'respuesta desconocida');
|
||||||
: '❌ 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) {
|
} catch (\Throwable $e) {
|
||||||
$this->webhookResult = '❌ No se pudo conectar con Telegram: ' . $e->getMessage();
|
$this->webhookResult = '❌ No se pudo conectar con Telegram: ' . $e->getMessage();
|
||||||
|
|||||||
@@ -60,9 +60,22 @@ class TelegramBotService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$lower = strtolower($text);
|
$lower = strtolower(trim($text));
|
||||||
if (in_array($lower, ['/start', '/menu', 'menu', 'inicio', 'hola'])) {
|
|
||||||
|
// 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) {
|
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);
|
$this->showMainMenu($chatId);
|
||||||
} else {
|
} else {
|
||||||
$this->askEmail($chatId);
|
$this->askEmail($chatId);
|
||||||
@@ -1049,6 +1062,18 @@ class TelegramBotService
|
|||||||
|
|
||||||
// ─── Telegram API ─────────────────────────────────────────
|
// ─── 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
|
public function send(string $chatId, string $text): bool
|
||||||
{
|
{
|
||||||
if ($this->convId) {
|
if ($this->convId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user