diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index 9fe743f..ef97c00 100755 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -14,10 +14,8 @@ class TelegramBotService public function __construct() { - $this->botToken = config('services.telegram.bot_token', env('TELEGRAM_BOT_TOKEN')); - - $appUrl = config('app.url', env('APP_URL', 'http://nomina-pegaso.casa')); - $this->webhookUrl = rtrim($appUrl, '/') . '/telegram/webhook'; + $this->botToken = config('services.telegram.bot_token'); + $this->webhookUrl = rtrim(config('app.url'), '/') . '/telegram/webhook'; } /** @@ -56,30 +54,33 @@ class TelegramBotService */ private function handleUnverifiedUser(string $chatId, string $text): array { - // Si es un código de verificación + // Si es un código de verificación (6 dígitos numéricos) if (strlen($text) === 6 && is_numeric($text)) { - $telegramAccount = TelegramAccount::where('chat_id', $chatId) - ->where('verification_code', $text) + // Buscamos la cuenta que tiene este código de verificación y no está verificada + $telegramAccount = TelegramAccount::where('verification_code', $text) + ->where('is_verified', false) ->first(); if ($telegramAccount) { + // Actualizar la cuenta con el chat_id del usuario que mandó el código $telegramAccount->update([ + 'chat_id' => $chatId, 'is_verified' => true, 'verification_code' => null ]); $user = $telegramAccount->user; - $this->sendMessage($chatId, "¡Verificación exitosa! Tu cuenta de Telegram está vinculada a {$user->name}. Ahora recibirás notificaciones de tus comisiones."); + $this->sendMessage($chatId, "¡Hola {$user->name}! 👋\n\nVerificación exitosa. Tu cuenta de Telegram ha sido vinculada correctamente. Ahora puedes usar comandos como /resumen para ver tu estado."); return ['ok' => true, 'verified' => true]; } else { - $this->sendMessage($chatId, "Código de verificación inválido. Por favor intenta con el código correcto."); + $this->sendMessage($chatId, "❌ El código $text es inválido o ya ha sido usado. Por favor, genera un código nuevo en tu panel de usuario."); return ['ok' => true, 'verified' => false]; } } // Mensaje de bienvenida para usuarios no verificados - $this->sendMessage($chatId, "¡Hola! Para usar este bot necesitas verificar tu cuenta.\n\nPor favor ingresa el código de verificación de 6 dígitos que encontrarás en la sección de Telegram de tu panel de usuario."); + $this->sendMessage($chatId, "👋 ¡Hola! Soy el bot de Nómina Pegaso.\n\nPara usar este bot, primero debes vincular tu cuenta:\n\n1️⃣ Ve a tu panel web\n2️⃣ Sección Telegram -> Vincular\n3️⃣ Envía aquí el código de 6 dígitos que veas allá."); return ['ok' => true, 'verified' => false]; } diff --git a/bootstrap/app.php b/bootstrap/app.php index c183276..0be650f 100755 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -11,7 +11,11 @@ return Application::configure(basePath: dirname(__DIR__)) health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { - // + $middleware->trustProxies(at: '*'); + + $middleware->validateCsrfTokens(except: [ + '/telegram/webhook', + ]); }) ->withExceptions(function (Exceptions $exceptions): void { // diff --git a/config/services.php b/config/services.php index 6a90eb8..9cfe2ad 100755 --- a/config/services.php +++ b/config/services.php @@ -35,4 +35,8 @@ return [ ], ], + 'telegram' => [ + 'bot_token' => env('TELEGRAM_BOT_TOKEN'), + ], + ];