diff --git a/app/Http/Controllers/TelegramWebhookController.php b/app/Http/Controllers/TelegramWebhookController.php index 3d7c7dd..82a31e2 100644 --- a/app/Http/Controllers/TelegramWebhookController.php +++ b/app/Http/Controllers/TelegramWebhookController.php @@ -4,10 +4,25 @@ namespace App\Http\Controllers; use App\Services\TelegramBotService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; class TelegramWebhookController extends Controller { public function handle(Request $request) + { + try { + return $this->process($request); + } catch (\Throwable $e) { + Log::error('[TelegramWebhook] Uncaught exception: ' . $e->getMessage(), [ + 'trace' => $e->getTraceAsString(), + 'body' => $request->all(), + ]); + // Always return 200 so Telegram doesn't retry + return response()->json(['ok' => true]); + } + } + + private function process(Request $request) { $update = $request->all(); $bot = new TelegramBotService(); @@ -57,3 +72,4 @@ class TelegramWebhookController extends Controller return response()->json(['ok' => true]); } } + diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index 6bd6468..cb81c79 100644 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -342,7 +342,13 @@ class TelegramBotService return; } - $user = User::whereRaw('LOWER(email) = ?', [$email])->first(); + try { + $user = User::whereRaw('LOWER(email) = ?', [$email])->first(); + } catch (\Throwable $e) { + Log::error('[TelegramBot] processEmail DB error: ' . $e->getMessage()); + $this->send($chatId, "⚠️ Error de conexión. Escribe tu correo de nuevo:"); + return; + } if (! $user) { $this->setState($chatId, [ @@ -356,16 +362,22 @@ class TelegramBotService return; } - $contact = ChatContact::where('canal', 'telegram')->where('canal_id', $chatId)->first(); - if ($contact) { - $contact->update(['user_id' => $user->id, 'nombre' => $user->name]); - } else { - $contact = ChatContact::create([ - 'canal' => 'telegram', - 'canal_id' => $chatId, - 'nombre' => $user->name, - 'user_id' => $user->id, - ]); + try { + $contact = ChatContact::where('canal', 'telegram')->where('canal_id', (string) $chatId)->first(); + if ($contact) { + $contact->update(['user_id' => $user->id, 'nombre' => $user->name]); + } else { + $contact = ChatContact::create([ + 'canal' => 'telegram', + 'canal_id' => (string) $chatId, + 'nombre' => $user->name, + 'user_id' => $user->id, + ]); + } + } catch (\Throwable $e) { + Log::error('[TelegramBot] processEmail contact error: ' . $e->getMessage()); + $this->send($chatId, "⚠️ Error interno. Escribe tu correo de nuevo:"); + return; } $this->sendOtp($chatId, $contact, $user); @@ -376,9 +388,11 @@ class TelegramBotService $codigo = (string) random_int(100000, 999999); Cache::put("chat_otp_{$contact->id}", $codigo, now()->addMinutes(10)); + $mailEnviado = true; try { Mail::to($user->email)->send(new ChatOtpMail($codigo, $user->name)); } catch (\Throwable $e) { + $mailEnviado = false; Log::warning('[TelegramOTP] Error enviando correo: ' . $e->getMessage()); } @@ -389,7 +403,16 @@ class TelegramBotService ]); $masked = $this->maskEmail($user->email); - $this->send($chatId, "✉️ Te enviamos un código a *{$masked}*.\n\nEscribe el código de 6 dígitos:"); + + if ($mailEnviado) { + $this->send($chatId, "✉️ Te enviamos un código a *{$masked}*.\n\nEscribe el código de 6 dígitos:"); + } else { + $this->send($chatId, + "✉️ Código generado para *{$masked}*.\n\n" . + "⚠️ Hubo un problema enviando el correo. Revisa tu bandeja o escribe /cancelar para intentar de nuevo.\n\n" . + "Si lo recibiste, escribe el código de 6 dígitos:" + ); + } } private function processOtp(string $chatId, string $text): void