Files
sistema_funcionando_lastwar/check_webhook.php

81 lines
2.9 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/includes/db.php'; // Necesario para la instancia de PDO
require_once __DIR__ . '/src/TelegramSender.php';
$botToken = $_ENV['TELEGRAM_BOT_TOKEN'];
$webhookUrl = 'https://' . $_SERVER['HTTP_HOST'] . '/telegram_bot_webhook.php?auth_token=' . urlencode($_ENV['TELEGRAM_WEBHOOK_TOKEN']);
// Instanciar TelegramSender con la conexión PDO
$telegramSender = new TelegramSender($botToken, $pdo); // $pdo debe estar disponible desde includes/db.php
// Verificar el webhook actual
$apiUrl = "https://api.telegram.org/bot{$botToken}/getWebhookInfo";
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
header('Content-Type: text/plain');
echo "=== Estado actual del webhook ===\n";
if ($httpCode === 200) {
$result = json_decode($response, true);
echo "URL actual: " . ($result['result']['url'] ?? 'No configurado') . "\n";
echo "Tiene certificado: " . ($result['result']['has_custom_certificate'] ? 'Sí' : 'No') . "\n";
echo "Último error: " . ($result['result']['last_error_message'] ?? 'Ninguno') . "\n";
echo "\n";
} else {
echo "Error al obtener información del webhook: " . $error . "\n";
}
// Configurar el webhook
echo "\n=== Configurando webhook ===\n";
$apiUrl = "https://api.telegram.org/bot{$botToken}/setWebhook";
$postData = [
'url' => $webhookUrl,
'max_connections' => 40,
'allowed_updates' => ['message', 'callback_query']
];
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($httpCode === 200) {
$result = json_decode($response, true);
if ($result['ok'] === true) {
echo "✅ Webhook configurado correctamente en: " . $webhookUrl . "\n";
} else {
echo "❌ Error al configurar el webhook: " . ($result['description'] ?? 'Error desconocido') . "\n";
}
} else {
echo "❌ Error HTTP al configurar el webhook: " . $httpCode . " - " . $error . "\n";
if ($response) {
echo "Respuesta: " . $response . "\n";
}
}
echo "\n=== Prueba de envío de mensaje ===\n";
$chatId = 'YOUR_CHAT_ID'; // Reemplaza con tu ID de chat de Telegram
$message = "✅ Webhook configurado correctamente en: " . $webhookUrl;
// Enviar el mensaje usando TelegramSender
$response = $telegramSender->sendMessage($chatId, $message);
if ($response) {
echo "✅ Mensaje de prueba enviado correctamente a tu chat de Telegram.\n";
} else {
echo "❌ No se pudo enviar el mensaje de prueba. Asegúrate de que el bot te haya enviado un mensaje primero.\n";
if ($error) {
echo "Error: " . $error . "\n";
}
if ($response) {
echo "Respuesta: " . json_encode($response) . "\n";
}
}