fix: catch all exceptions in Telegram email flow + webhook global handler
- processEmail: wrap DB lookup and ChatContact create/update in try-catch so any exception sends an error message to the user instead of silently failing and leaving state stuck at await_email - sendOtp: inform user if mail sending fails so they know to wait/retry - TelegramWebhookController: top-level try-catch returns 200 always so Telegram never retries on server errors (retries caused duplicate state resets) - ChatContact canal_id cast to string to avoid type mismatch on insert Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
eb6bb8e7f3
commit
77c3353213
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user