From 50bbd159c6ccfb8105f6213e918596cd2a153464 Mon Sep 17 00:00:00 2001 From: Lizandro Date: Tue, 14 Jul 2026 18:26:39 +0000 Subject: [PATCH] Fix Telegram webhook registration: replace file_get_contents with Http client file_get_contents fails on many hosts due to allow_url_fopen or SSL restrictions. Http:: (Guzzle) is more reliable and shows a clearer error message on failure. Co-Authored-By: Claude Sonnet 4.6 --- .../Livewire/Chat/ShowConfiguracionChat.php | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/app/Http/Livewire/Chat/ShowConfiguracionChat.php b/app/Http/Livewire/Chat/ShowConfiguracionChat.php index 3227ab5..30d85b6 100644 --- a/app/Http/Livewire/Chat/ShowConfiguracionChat.php +++ b/app/Http/Livewire/Chat/ShowConfiguracionChat.php @@ -3,6 +3,7 @@ namespace App\Http\Livewire\Chat; use App\Models\ChatConfig; +use Illuminate\Support\Facades\Http; use Jantinnerezo\LivewireAlert\LivewireAlert; use Livewire\Component; @@ -82,35 +83,29 @@ class ShowConfiguracionChat extends Component $token = trim($this->telegram_token); if (! $token) { - $this->webhookResult = 'El token de Telegram esta vacio.'; + $this->webhookResult = '⚠️ El token de Telegram está vacío.'; return; } ChatConfig::set('telegram_token', $token); $webhookUrl = url('/chat/webhook/telegram'); - $apiUrl = "https://api.telegram.org/bot{$token}/setWebhook"; - $context = stream_context_create([ - 'http' => [ - 'method' => 'POST', - 'header' => "Content-Type: application/x-www-form-urlencoded\r\n", - 'content' => http_build_query(['url' => $webhookUrl]), - 'timeout' => 10, - ], - ]); + try { + $response = Http::timeout(15) + ->post("https://api.telegram.org/bot{$token}/setWebhook", [ + 'url' => $webhookUrl, + ]); - $result = @file_get_contents($apiUrl, false, $context); + $data = $response->json(); - if ($result === false) { - $this->webhookResult = 'Error de conexion con la API de Telegram.'; - return; + $this->webhookResult = ($data['ok'] ?? false) + ? '✅ Webhook registrado correctamente.' + : '❌ Error de Telegram: ' . ($data['description'] ?? 'respuesta desconocida'); + + } catch (\Throwable $e) { + $this->webhookResult = '❌ No se pudo conectar con Telegram: ' . $e->getMessage(); } - - $data = json_decode($result, true); - $this->webhookResult = ($data['ok'] ?? false) - ? 'Webhook registrado: ' . ($data['description'] ?? 'OK') - : 'Error: ' . ($data['description'] ?? 'respuesta desconocida'); } public function render()