refactor: Centralizar y corregir la comunicación con la API de Telegram
Se refactoriza toda la comunicación con la API de Telegram para solucionar un problema de latencia severa en el entorno Docker. El problema era causado por un retraso en la resolución de red. - Se mejora la función en para forzar el uso de IPv4, añadir timeouts y soportar métodos GET/POST. - Se centraliza la lógica de la API en la clase , añadiendo los métodos , y . - Se modifica para que utilice los nuevos métodos centralizados, eliminando código cURL duplicado y aplicando la solución de red. - Se mantiene la instrumentación en para futuros diagnósticos, según lo solicitado.
This commit is contained in:
@@ -17,7 +17,7 @@ $botInfo = null;
|
||||
|
||||
// Obtener información del bot
|
||||
$botMe = $bot->getMe();
|
||||
if ($botMe && isset($botMe['ok']) && $botMe['ok']) {
|
||||
if ($botMe && $botMe['ok']) { // Simplificado
|
||||
$botInfo = $botMe['result'];
|
||||
}
|
||||
|
||||
@@ -26,14 +26,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
if ($action === 'verificar') {
|
||||
$url = "https://api.telegram.org/bot{$config['telegram_bot_token']}/getWebhookInfo";
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
$result = json_decode($response, true);
|
||||
$result = $bot->getWebhookInfo(); // Usar el método centralizado
|
||||
|
||||
if ($result && isset($result['ok'])) {
|
||||
if ($result && $result['ok']) { // Simplificado
|
||||
$webhookInfo = $result;
|
||||
$message = 'Información del webhook obtenida';
|
||||
$messageType = 'success';
|
||||
@@ -42,17 +37,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$messageType = 'danger';
|
||||
}
|
||||
} elseif ($action === 'borrar') {
|
||||
$url = "https://api.telegram.org/bot{$config['telegram_bot_token']}/deleteWebhook";
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([]));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
$result = json_decode($response, true);
|
||||
$result = $bot->deleteWebhook(); // Usar el método centralizado
|
||||
|
||||
if ($result && isset($result['ok']) && $result['ok']) {
|
||||
if ($result && $result['ok']) { // Simplificado
|
||||
$message = 'Webhook eliminado correctamente';
|
||||
$messageType = 'success';
|
||||
$webhookInfo = null;
|
||||
@@ -70,20 +57,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$message = 'La URL ingresada no es válida';
|
||||
$messageType = 'danger';
|
||||
} else {
|
||||
$url = "https://api.telegram.org/bot{$config['telegram_bot_token']}/setWebhook";
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
||||
'url' => $webhookUrl,
|
||||
'allowed_updates' => ['message', 'callback_query']
|
||||
]));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
$result = json_decode($response, true);
|
||||
$result = $bot->setWebhook($webhookUrl); // Usar el método centralizado
|
||||
|
||||
if ($result && isset($result['ok']) && $result['ok']) {
|
||||
if ($result && $result['ok']) { // Simplificado
|
||||
$message = "Webhook configurado correctamente en:\n" . htmlspecialchars($webhookUrl);
|
||||
$messageType = 'success';
|
||||
} else {
|
||||
@@ -94,13 +70,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
}
|
||||
|
||||
// Obtener estado actual del webhook
|
||||
$url = "https://api.telegram.org/bot{$config['telegram_bot_token']}/getWebhookInfo";
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
$webhookInfo = json_decode($response, true);
|
||||
// Obtener estado actual del webhook al cargar la página
|
||||
$webhookInfo = $bot->getWebhookInfo(); // Usar el método centralizado
|
||||
|
||||
$currentPage = 'webhook';
|
||||
$pageTitle = 'Administración del Bot de Telegram';
|
||||
|
||||
Reference in New Issue
Block a user