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