Fix: Corregida verificacion de Telegram y configurado TrustProxies para HTTPS

This commit is contained in:
2026-04-19 19:43:35 -06:00
parent c4bd4b62e3
commit d05324a28a
3 changed files with 20 additions and 11 deletions

View File

@@ -14,10 +14,8 @@ class TelegramBotService
public function __construct() public function __construct()
{ {
$this->botToken = config('services.telegram.bot_token', env('TELEGRAM_BOT_TOKEN')); $this->botToken = config('services.telegram.bot_token');
$this->webhookUrl = rtrim(config('app.url'), '/') . '/telegram/webhook';
$appUrl = config('app.url', env('APP_URL', 'http://nomina-pegaso.casa'));
$this->webhookUrl = rtrim($appUrl, '/') . '/telegram/webhook';
} }
/** /**
@@ -56,30 +54,33 @@ class TelegramBotService
*/ */
private function handleUnverifiedUser(string $chatId, string $text): array 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)) { if (strlen($text) === 6 && is_numeric($text)) {
$telegramAccount = TelegramAccount::where('chat_id', $chatId) // Buscamos la cuenta que tiene este código de verificación y no está verificada
->where('verification_code', $text) $telegramAccount = TelegramAccount::where('verification_code', $text)
->where('is_verified', false)
->first(); ->first();
if ($telegramAccount) { if ($telegramAccount) {
// Actualizar la cuenta con el chat_id del usuario que mandó el código
$telegramAccount->update([ $telegramAccount->update([
'chat_id' => $chatId,
'is_verified' => true, 'is_verified' => true,
'verification_code' => null 'verification_code' => null
]); ]);
$user = $telegramAccount->user; $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]; return ['ok' => true, 'verified' => true];
} else { } 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]; return ['ok' => true, 'verified' => false];
} }
} }
// Mensaje de bienvenida para usuarios no verificados // 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]; return ['ok' => true, 'verified' => false];
} }

View File

@@ -11,7 +11,11 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up', health: '/up',
) )
->withMiddleware(function (Middleware $middleware): void { ->withMiddleware(function (Middleware $middleware): void {
// $middleware->trustProxies(at: '*');
$middleware->validateCsrfTokens(except: [
'/telegram/webhook',
]);
}) })
->withExceptions(function (Exceptions $exceptions): void { ->withExceptions(function (Exceptions $exceptions): void {
// //

View File

@@ -35,4 +35,8 @@ return [
], ],
], ],
'telegram' => [
'bot_token' => env('TELEGRAM_BOT_TOKEN'),
],
]; ];