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 <noreply@anthropic.com>
This commit is contained in:
Lizandro
2026-07-14 18:26:39 +00:00
co-authored by Claude Sonnet 4.6
parent e43fe4043f
commit 50bbd159c6
@@ -3,6 +3,7 @@
namespace App\Http\Livewire\Chat; namespace App\Http\Livewire\Chat;
use App\Models\ChatConfig; use App\Models\ChatConfig;
use Illuminate\Support\Facades\Http;
use Jantinnerezo\LivewireAlert\LivewireAlert; use Jantinnerezo\LivewireAlert\LivewireAlert;
use Livewire\Component; use Livewire\Component;
@@ -82,35 +83,29 @@ class ShowConfiguracionChat extends Component
$token = trim($this->telegram_token); $token = trim($this->telegram_token);
if (! $token) { if (! $token) {
$this->webhookResult = 'El token de Telegram esta vacio.'; $this->webhookResult = '⚠️ El token de Telegram está vacío.';
return; return;
} }
ChatConfig::set('telegram_token', $token); ChatConfig::set('telegram_token', $token);
$webhookUrl = url('/chat/webhook/telegram'); $webhookUrl = url('/chat/webhook/telegram');
$apiUrl = "https://api.telegram.org/bot{$token}/setWebhook";
$context = stream_context_create([ try {
'http' => [ $response = Http::timeout(15)
'method' => 'POST', ->post("https://api.telegram.org/bot{$token}/setWebhook", [
'header' => "Content-Type: application/x-www-form-urlencoded\r\n", 'url' => $webhookUrl,
'content' => http_build_query(['url' => $webhookUrl]), ]);
'timeout' => 10,
],
]);
$result = @file_get_contents($apiUrl, false, $context); $data = $response->json();
if ($result === false) { $this->webhookResult = ($data['ok'] ?? false)
$this->webhookResult = 'Error de conexion con la API de Telegram.'; ? '✅ Webhook registrado correctamente.'
return; : '❌ 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() public function render()