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;
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()