Files
sirpremiumv2/app/Http/Controllers/TelegramWebhookController.php
T
LizandroandClaude Sonnet 4.6 77c3353213 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>
2026-07-15 04:01:53 +00:00

76 lines
2.7 KiB
PHP

<?php
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();
// Inline keyboard button taps
if (isset($update['callback_query'])) {
$cq = $update['callback_query'];
$chatId = (string) $cq['message']['chat']['id'];
$bot->handleCallback($chatId, $cq['id'], $cq['data'] ?? '');
return response()->json(['ok' => true]);
}
// Photos (payment receipts)
if (isset($update['message']['photo'])) {
$chatId = (string) $update['message']['chat']['id'];
$photos = $update['message']['photo'];
$fileId = end($photos)['file_id'];
$bot->handlePhoto($chatId, $fileId);
return response()->json(['ok' => true]);
}
// Voice / audio messages
if (isset($update['message']['voice']) || isset($update['message']['audio'])) {
$chatId = (string) $update['message']['chat']['id'];
$fileId = $update['message']['voice']['file_id'] ?? $update['message']['audio']['file_id'];
$duration = $update['message']['voice']['duration'] ?? $update['message']['audio']['duration'] ?? null;
$nombre = trim(
($update['message']['from']['first_name'] ?? '') . ' ' .
($update['message']['from']['last_name'] ?? '')
);
$bot->handleVoice($chatId, $fileId, $nombre, (int) $duration);
return response()->json(['ok' => true]);
}
// Text messages
if (isset($update['message']['text'])) {
$chatId = (string) $update['message']['chat']['id'];
$text = $update['message']['text'];
$nombre = trim(
($update['message']['from']['first_name'] ?? '') . ' ' .
($update['message']['from']['last_name'] ?? '')
);
$bot->handleText($chatId, $text, $nombre);
return response()->json(['ok' => true]);
}
return response()->json(['ok' => true]);
}
}