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:
@@ -14,14 +14,39 @@ class TelegramBot {
|
||||
$this->apiUrl = "https://api.telegram.org/bot{$this->token}";
|
||||
}
|
||||
|
||||
private function request($method, $data = []) {
|
||||
private function request($method, $data = [], $httpMethod = 'POST') {
|
||||
$url = "{$this->apiUrl}/{$method}";
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
// --- MEJORAS ---
|
||||
// 1. Forzar el uso de IPv4. Un problema común en entornos Docker
|
||||
// es un timeout al intentar resolver AAAA (IPv6) antes de usar A (IPv4).
|
||||
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
||||
|
||||
// 2. Añadir timeouts para evitar que el script se cuelgue indefinidamente.
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 5 segundos para conectar
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 segundos para la transferencia total
|
||||
|
||||
if ($httpMethod === 'POST') {
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
} else { // GET request
|
||||
$url .= empty($data) ? '' : '?' . http_build_query($data);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Añadir manejo de errores de cURL
|
||||
if (curl_errno($ch)) {
|
||||
error_log("cURL Error en {$method}: " . curl_error($ch));
|
||||
curl_close($ch);
|
||||
return null;
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
return json_decode($response, true);
|
||||
}
|
||||
@@ -39,13 +64,30 @@ class TelegramBot {
|
||||
}
|
||||
|
||||
public function getMe() {
|
||||
return $this->request('getMe');
|
||||
return $this->request('getMe', [], 'GET');
|
||||
}
|
||||
|
||||
public function getUpdates($offset = 0) {
|
||||
return $this->request('getUpdates', ['offset' => $offset, 'timeout' => 60]);
|
||||
return $this->request('getUpdates', ['offset' => $offset, 'timeout' => 60], 'GET');
|
||||
}
|
||||
|
||||
// --- NUEVOS MÉTODOS PARA CENTRALIZAR COMUNICACIONES ---
|
||||
public function getWebhookInfo() {
|
||||
return $this->request('getWebhookInfo', [], 'GET');
|
||||
}
|
||||
|
||||
public function deleteWebhook() {
|
||||
return $this->request('deleteWebhook', []);
|
||||
}
|
||||
|
||||
public function setWebhook($webhookUrl, $allowedUpdates = ['message', 'callback_query']) {
|
||||
return $this->request('setWebhook', [
|
||||
'url' => $webhookUrl,
|
||||
'allowed_updates' => $allowedUpdates
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function sendKeyboard($chatId, $text) {
|
||||
$keyboard = [
|
||||
'inline_keyboard' => [
|
||||
|
||||
Reference in New Issue
Block a user