Initial commit: Sistema de comisiones y gastos personales
This commit is contained in:
140
app/Http/Controllers/TelegramController.php
Executable file
140
app/Http/Controllers/TelegramController.php
Executable file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\TelegramAccount;
|
||||
use App\Models\User;
|
||||
use App\Services\TelegramBotService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TelegramController extends Controller
|
||||
{
|
||||
/**
|
||||
* Mostrar página de vinculación de Telegram
|
||||
*/
|
||||
public function showVerifyPage()
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
// Obtener o crear cuenta de Telegram
|
||||
$telegramAccount = $user->telegramAccount()->first();
|
||||
|
||||
if (!$telegramAccount) {
|
||||
// Generar nuevo código de verificación
|
||||
$verificationCode = TelegramBotService::generateVerificationCode();
|
||||
|
||||
$telegramAccount = TelegramAccount::create([
|
||||
'user_id' => $user->id,
|
||||
'chat_id' => null,
|
||||
'verification_code' => $verificationCode,
|
||||
'is_verified' => false,
|
||||
]);
|
||||
} elseif (!$telegramAccount->is_verified && !$telegramAccount->verification_code) {
|
||||
// Regenerar código si no existe
|
||||
$verificationCode = TelegramBotService::generateVerificationCode();
|
||||
$telegramAccount->update(['verification_code' => $verificationCode]);
|
||||
}
|
||||
|
||||
return view('telegram.verify', compact('telegramAccount'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerar código de verificación
|
||||
*/
|
||||
public function regenerateCode(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$telegramAccount = $user->telegramAccount;
|
||||
|
||||
if (!$telegramAccount) {
|
||||
return back()->with('error', 'No tienes una cuenta de Telegram vinculada.');
|
||||
}
|
||||
|
||||
if ($telegramAccount->is_verified) {
|
||||
return back()->with('error', 'Tu cuenta ya está verificada.');
|
||||
}
|
||||
|
||||
$verificationCode = TelegramBotService::generateVerificationCode();
|
||||
$telegramAccount->update(['verification_code' => $verificationCode]);
|
||||
|
||||
return back()->with('success', 'Nuevo código de verificación generado.');
|
||||
}
|
||||
|
||||
/**
|
||||
* desvincular cuenta de Telegram
|
||||
*/
|
||||
public function unlink(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$telegramAccount = $user->telegramAccount;
|
||||
|
||||
if ($telegramAccount) {
|
||||
$telegramAccount->delete();
|
||||
|
||||
// Crear nueva cuenta sin verificar
|
||||
TelegramAccount::create([
|
||||
'user_id' => $user->id,
|
||||
'chat_id' => null,
|
||||
'verification_code' => TelegramBotService::generateVerificationCode(),
|
||||
'is_verified' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Cuenta de Telegram desvinculada.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Webhook para recibir mensajes de Telegram
|
||||
*/
|
||||
public function webhook(Request $request)
|
||||
{
|
||||
try {
|
||||
$update = $request->all();
|
||||
|
||||
Log::info('Telegram webhook received', $update);
|
||||
|
||||
if (empty($update)) {
|
||||
return response()->json(['ok' => true, 'message' => 'No update
|
||||
received']);
|
||||
}
|
||||
|
||||
$botService = new TelegramBotService();
|
||||
$result = $botService->handleUpdate($update);
|
||||
|
||||
return response()->json($result);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Telegram webhook error', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
return response()->json(['ok' => false, 'error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configurar webhook (ruta temporal para configurar desde el navegador)
|
||||
*/
|
||||
public function setupWebhook(Request $request)
|
||||
{
|
||||
$token = $request->get('token');
|
||||
|
||||
// Verificar token de seguridad simple
|
||||
if ($token !== config('app.key')) {
|
||||
abort(403, 'Token inválido');
|
||||
}
|
||||
|
||||
$botService = new TelegramBotService();
|
||||
$result = $botService->setWebhook();
|
||||
|
||||
if ($result) {
|
||||
return response()->json(['success' => true, 'message' => 'Webhook configurado correctamente']);
|
||||
}
|
||||
|
||||
return response()->json(['success' => false, 'error' => 'Error al configurar webhook'], 500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user