328 lines
14 KiB
PHP
Executable File
328 lines
14 KiB
PHP
Executable File
<?php
|
|
if (!defined('BASE_PATH')) {
|
|
define('BASE_PATH', dirname(__DIR__, 2));
|
|
}
|
|
$config = require BASE_PATH . '/config/config.php';
|
|
require_once BASE_PATH . '/src/Auth.php';
|
|
require_once BASE_PATH . '/bot/TelegramBot.php';
|
|
|
|
$auth = new Auth();
|
|
$auth->requireAdmin();
|
|
|
|
$bot = new TelegramBot();
|
|
$message = '';
|
|
$messageType = '';
|
|
$webhookInfo = null;
|
|
$botInfo = null;
|
|
|
|
// Obtener información del bot
|
|
$botMe = $bot->getMe();
|
|
if ($botMe && isset($botMe['ok']) && $botMe['ok']) {
|
|
$botInfo = $botMe['result'];
|
|
}
|
|
|
|
// Verificar estado del webhook
|
|
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);
|
|
|
|
if ($result && isset($result['ok'])) {
|
|
$webhookInfo = $result;
|
|
$message = 'Información del webhook obtenida';
|
|
$messageType = 'success';
|
|
} else {
|
|
$message = 'Error al obtener información del webhook';
|
|
$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);
|
|
|
|
if ($result && isset($result['ok']) && $result['ok']) {
|
|
$message = 'Webhook eliminado correctamente';
|
|
$messageType = 'success';
|
|
$webhookInfo = null;
|
|
} else {
|
|
$message = 'Error al eliminar webhook: ' . ($result['description'] ?? 'Desconocido');
|
|
$messageType = 'danger';
|
|
}
|
|
} elseif ($action === 'configurar') {
|
|
$webhookUrl = trim($_POST['webhook_url'] ?? '');
|
|
|
|
if (empty($webhookUrl)) {
|
|
$message = 'Debes ingresar la URL del webhook';
|
|
$messageType = 'danger';
|
|
} elseif (!filter_var($webhookUrl, FILTER_VALIDATE_URL)) {
|
|
$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);
|
|
|
|
if ($result && isset($result['ok']) && $result['ok']) {
|
|
$message = "Webhook configurado correctamente en:\n" . htmlspecialchars($webhookUrl);
|
|
$messageType = 'success';
|
|
} else {
|
|
$message = 'Error al configurar webhook: ' . ($result['description'] ?? 'Desconocido');
|
|
$messageType = 'danger';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
|
|
$currentPage = 'webhook';
|
|
$pageTitle = 'Administración del Bot de Telegram';
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="es">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= $pageTitle ?> - Contenedor Ibiza</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
</head>
|
|
<body>
|
|
<?php include BASE_PATH . '/public/partials/navbar.php'; ?>
|
|
|
|
<div class="container mt-4">
|
|
<h2 class="mb-4">🤖 Administración del Bot de Telegram</h2>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-<?= $messageType ?>"><?= nl2br(htmlspecialchars($message)) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Información del Bot -->
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header bg-primary text-white">
|
|
<h5 class="mb-0">Información del Bot</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if ($botInfo): ?>
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<p><strong>Nombre:</strong> <?= htmlspecialchars($botInfo['first_name']) ?></p>
|
|
<p><strong>Username:</strong> @<?= htmlspecialchars($botInfo['username']) ?></p>
|
|
<p><strong>ID:</strong> <?= $botInfo['id'] ?></p>
|
|
<p><strong>Estado:</strong>
|
|
<span class="badge bg-success">Conectado</span>
|
|
</p>
|
|
</div>
|
|
<div class="col-md-6 text-end">
|
|
<span class="text-muted">Token configurado correctamente</span>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="alert alert-warning">
|
|
<strong>Error:</strong> No se pudo conectar con el bot.
|
|
Verifica que el TELEGRAM_BOT_TOKEN esté configurado correctamente en .env
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Estado del Webhook -->
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header bg-info text-white">
|
|
<h5 class="mb-0">Estado del Webhook</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if ($webhookInfo && isset($webhookInfo['ok']) && $webhookInfo['ok']): ?>
|
|
<?php if (!empty($webhookInfo['result']['url'])): ?>
|
|
<div class="alert alert-success">
|
|
<strong>✅ Webhook activo</strong>
|
|
<p class="mb-0 mt-2">
|
|
<strong>URL:</strong> <?= htmlspecialchars($webhookInfo['result']['url']) ?>
|
|
</p>
|
|
</div>
|
|
<ul class="list-group mb-3">
|
|
<li class="list-group-item d-flex justify-content-between">
|
|
<span>Última actualización:</span>
|
|
<span><?= date('d/m/Y H:i:s', $webhookInfo['result']['last_synchronization_unix_time'] ?? 0) ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between">
|
|
<span>IP permitida:</span>
|
|
<span><?= $webhookInfo['result']['ip_address'] ?? 'No disponible' ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between">
|
|
<span>Errores acumulados:</span>
|
|
<span><?= $webhookInfo['result']['last_error_date'] ?? 0 ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between">
|
|
<span>Actualizaciones pendientes:</span>
|
|
<span><?= $webhookInfo['result']['pending_update_count'] ?? 0 ?></span>
|
|
</li>
|
|
</ul>
|
|
<?php else: ?>
|
|
<div class="alert alert-warning">
|
|
<strong>⚠️ Webhook no configurado</strong>
|
|
<p class="mb-0 mt-2">No hay webhook configurado para este bot.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<div class="alert alert-danger">
|
|
<strong>Error:</strong> No se pudo obtener información del webhook
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Acciones -->
|
|
<div class="d-flex gap-2 mt-3">
|
|
<form method="POST" class="d-inline">
|
|
<input type="hidden" name="action" value="verificar">
|
|
<button type="submit" class="btn btn-outline-primary">
|
|
🔄 Verificar Estado
|
|
</button>
|
|
</form>
|
|
|
|
<?php if ($webhookInfo && isset($webhookInfo['result']['url']) && !empty($webhookInfo['result']['url'])): ?>
|
|
<form method="POST" class="d-inline" onsubmit="return confirm('¿Estás seguro de eliminar el webhook?');">
|
|
<input type="hidden" name="action" value="borrar">
|
|
<button type="submit" class="btn btn-outline-danger">
|
|
🗑️ Eliminar Webhook
|
|
</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Configurar Webhook -->
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header bg-success text-white">
|
|
<h5 class="mb-0">⚙️ Configurar Webhook</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php
|
|
// Construir URL sugerida usando SITE_URL del .env
|
|
$urlSugerida = ($config['site_url'] ?? '') . '/bot/webhook.php';
|
|
$urlActual = $webhookInfo['result']['url'] ?? '';
|
|
$urlParaInput = $urlActual ?: $urlSugerida;
|
|
?>
|
|
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="configurar">
|
|
|
|
<div class="mb-3">
|
|
<label for="webhook_url" class="form-label">URL del Webhook:</label>
|
|
<input type="url" class="form-control" id="webhook_url" name="webhook_url"
|
|
value="<?= htmlspecialchars($urlParaInput) ?>"
|
|
placeholder="https://tu-dominio.com/bot/webhook.php" required>
|
|
<?php if (!empty($config['site_url'])): ?>
|
|
<div class="form-text">
|
|
URL sugerida basada en SITE_URL: <code><?= htmlspecialchars($config['site_url']) ?></code>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="alert alert-info mb-3">
|
|
<strong>📝 Instrucciones:</strong>
|
|
<ol class="mb-0">
|
|
<li>Asegúrate de que la URL sea accesible públicamente (no localhost)</li>
|
|
<li>El dominio debe tener certificado SSL (HTTPS)</li>
|
|
<li>Ejemplo de URL: <code>https://contenedor-test.local:82/bot/webhook.php</code></li>
|
|
</ol>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-success">
|
|
✅ Configurar Webhook
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Comandos del Bot -->
|
|
<div class="card shadow-sm">
|
|
<div class="card-header bg-secondary text-white">
|
|
<h5 class="mb-0">📋 Comandos Disponibles</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Comando</th>
|
|
<th>Descripción</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td><code>/start</code> o <code>/menu</code></td>
|
|
<td>Muestra el menú interactivo con botones</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/turnos</code></td>
|
|
<td>Muestra la tabla completa de asignaciones</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/semana</code> o <code>hoy</code></td>
|
|
<td>Muestra quién tiene turno esta semana</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/ayudantes</code></td>
|
|
<td>Lista de todos los ayudantes</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>[Nombre]</code></td>
|
|
<td>Busca los turnos de un ayudante específico</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- URLs de Referencia -->
|
|
<div class="card shadow-sm mt-4">
|
|
<div class="card-header bg-dark text-white">
|
|
<h5 class="mb-0">🔗 URLs de Referencia</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<strong>Webhook:</strong><br>
|
|
<code class="text-primary"><?= htmlspecialchars($config['site_url'] ?? 'https://tu-dominio.com') ?>/bot/webhook.php</code>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<strong>Test del bot:</strong><br>
|
|
<code class="text-primary"><?= $site_url ?? 'https://tu-dominio.com' ?>/bot/test.php</code>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|