338 lines
15 KiB
PHP
Executable File
338 lines
15 KiB
PHP
Executable File
<?php
|
|
require_once __DIR__ . '/includes/session_check.php';
|
|
require_once __DIR__ . '/includes/db.php';
|
|
|
|
// Crear las tablas necesarias si no existen
|
|
try {
|
|
$pdo->exec("
|
|
-- Tabla para registrar interacciones con el bot
|
|
CREATE TABLE IF NOT EXISTS telegram_bot_interactions (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id BIGINT NOT NULL,
|
|
username VARCHAR(255),
|
|
first_name VARCHAR(255),
|
|
last_name VARCHAR(255),
|
|
interaction_type VARCHAR(50) NOT NULL,
|
|
interaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE KEY unique_interaction (user_id, interaction_type)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
-- Tabla para la configuración del mensaje del bot
|
|
CREATE TABLE IF NOT EXISTS telegram_bot_messages (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
message_text TEXT NOT NULL,
|
|
button_text VARCHAR(50) DEFAULT '¡Únete a nuestro grupo!',
|
|
group_invite_link VARCHAR(255) DEFAULT 'https://t.me/+Hzh0G-1-BY41ZDNj',
|
|
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
|
register_users BOOLEAN NOT NULL DEFAULT TRUE,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
-- Insertar configuración por defecto si no existe
|
|
INSERT IGNORE INTO telegram_bot_messages
|
|
(message_text, button_text, group_invite_link, is_active, register_users)
|
|
VALUES (
|
|
'¡Hola {user_name}! 👋\n\nGracias por interactuar con nuestro bot. Únete a nuestro grupo principal para mantenerte actualizado con nuestras novedades.',
|
|
'¡Únete a nuestro grupo!',
|
|
'https://t.me/+Hzh0G-1-BY41ZDNj',
|
|
TRUE,
|
|
TRUE
|
|
);
|
|
");
|
|
} catch (PDOException $e) {
|
|
die("Error al crear las tablas: " . $e->getMessage());
|
|
}
|
|
|
|
// Manejar el envío del formulario
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$messageText = $_POST['message_text'] ?? '';
|
|
$buttonText = $_POST['button_text'] ?? '¡Únete a nuestro grupo!';
|
|
$groupInviteLink = $_POST['group_invite_link'] ?? 'https://t.me/+Hzh0G-1-BY41ZDNj';
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
$registerUsers = isset($_POST['register_users']) ? 1 : 0;
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Actualizar la configuración
|
|
$stmt = $pdo->prepare("
|
|
UPDATE telegram_bot_messages
|
|
SET message_text = ?,
|
|
button_text = ?,
|
|
group_invite_link = ?,
|
|
is_active = ?,
|
|
register_users = ?
|
|
WHERE id = 1
|
|
");
|
|
|
|
$stmt->execute([
|
|
$messageText,
|
|
$buttonText,
|
|
$groupInviteLink,
|
|
$isActive,
|
|
$registerUsers
|
|
]);
|
|
|
|
$pdo->commit();
|
|
$successMessage = "¡Configuración guardada con éxito!";
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
$errorMessage = "Error al guardar la configuración: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// Obtener la configuración actual
|
|
try {
|
|
$stmt = $pdo->query("SELECT * FROM telegram_bot_messages WHERE id = 1");
|
|
$config = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$config) {
|
|
throw new Exception("No se encontró la configuración del mensaje del bot");
|
|
}
|
|
} catch (Exception $e) {
|
|
$errorMessage = "Error al cargar la configuración: " . $e->getMessage();
|
|
$config = [
|
|
'message_text' => '¡Hola {user_name}! 👋\n\nGracias por interactuar con nuestro bot. Únete a nuestro grupo principal para mantenerte actualizado con nuestras novedades.',
|
|
'button_text' => '¡Únete a nuestro grupo!',
|
|
'group_invite_link' => 'https://t.me/+Hzh0G-1-BY41ZDNj',
|
|
'is_active' => true,
|
|
'register_users' => true
|
|
];
|
|
}
|
|
|
|
// Obtener estadísticas de interacciones
|
|
try {
|
|
$statsStmt = $pdo->query("
|
|
SELECT
|
|
COUNT(*) as total_interactions,
|
|
COUNT(DISTINCT user_id) as unique_users
|
|
FROM telegram_bot_interactions
|
|
");
|
|
$stats = $statsStmt->fetch(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$errorMessage = "Error al cargar las estadísticas: " . $e->getMessage();
|
|
$stats = [];
|
|
}
|
|
|
|
$pageTitle = 'Interacciones del Bot de Telegram';
|
|
require_once __DIR__ . '/templates/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<h1 class="mt-4" data-translate="true">Interacciones del Bot de Telegram</h1>
|
|
<p class="text-muted" data-translate="true">Configura el mensaje que se enviará cuando los usuarios interactúen con tu bot.</p>
|
|
|
|
<?php if (isset($successMessage)): ?>
|
|
<div class="alert alert-success" data-translate="true"><?= htmlspecialchars($successMessage) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($errorMessage)): ?>
|
|
<div class="alert alert-danger" data-translate="true"><?= htmlspecialchars($errorMessage) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header">
|
|
<h5 class="mb-0" data-translate="true">Configuración del Mensaje del Bot</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="message_text" class="form-label" data-translate="true">Mensaje de Respuesta</label>
|
|
<textarea class="form-control" id="message_text" name="message_text" rows="5" required><?= htmlspecialchars($config['message_text'] ?? '') ?></textarea>
|
|
<div class="form-text" data-translate="true">
|
|
Puedes usar los siguientes placeholders que serán reemplazados automáticamente:
|
|
<ul class="mb-0">
|
|
<li><code>{user_name}</code> - El nombre del usuario que interactúa con el bot.</li>
|
|
<li><code>{group_link}</code> - El enlace al grupo principal.</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row mb-3">
|
|
<div class="col-md-6">
|
|
<label for="button_text" class="form-label" data-translate="true">Texto del Botón</label>
|
|
<input type="text" class="form-control" id="button_text" name="button_text"
|
|
value="<?= htmlspecialchars($config['button_text'] ?? '') ?>" required>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label for="group_invite_link" class="form-label" data-translate="true">Enlace de Invitación al Grupo</label>
|
|
<input type="url" class="form-control" id="group_invite_link" name="group_invite_link"
|
|
value="<?= htmlspecialchars($config['group_invite_link'] ?? '') ?>" required>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-check form-switch mb-3">
|
|
<input class="form-check-input" type="checkbox" id="is_active" name="is_active"
|
|
<?= ($config['is_active'] ?? true) ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="is_active" data-translate="true">Activar mensaje de respuesta automática</label>
|
|
</div>
|
|
|
|
<div class="form-check form-switch mb-3">
|
|
<input class="form-check-input" type="checkbox" id="register_users" name="register_users"
|
|
<?= ($config['register_users'] ?? true) ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="register_users" data-translate="true">Registrar usuarios que interactúan con el bot</label>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">
|
|
<i class="bi bi-save-fill me-1"></i> <span data-translate="true">Guardar Cambios</span>
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow-sm">
|
|
<div class="card-header">
|
|
<h5 class="mb-0" data-translate="true">Configuración de Webhook</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="mb-4">
|
|
<h6 data-translate="true">Webhook Unificado</h6>
|
|
<p class="small text-muted" data-translate="true">Este único webhook maneja tanto las interacciones directas con el bot como los mensajes de bienvenida a nuevos miembros en un grupo.</p>
|
|
<div class="input-group mb-2">
|
|
<input type="text" class="form-control" value="<?= rtrim($_ENV['APP_URL'] ?? 'https://pruebaspons.duckdns.org', '/') ?>/telegram_bot_webhook.php?auth_token=<?= urlencode($_ENV['TELEGRAM_WEBHOOK_TOKEN'] ?? '') ?>" id="unifiedWebhookUrl" readonly>
|
|
<button class="btn btn-outline-secondary" type="button" onclick="copyToClipboard(document.getElementById('unifiedWebhookUrl'))">
|
|
<i class="bi bi-clipboard"></i> Copiar
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-4">
|
|
<h6 data-translate="true">Comando de Configuración con cURL</h6>
|
|
<p class="small text-muted" data-translate="true">Usa este comando en tu terminal para configurar el webhook en Telegram. Solo necesitas hacerlo una vez.</p>
|
|
|
|
<div class="mb-3">
|
|
<div class="input-group">
|
|
<input type="text" class="form-control form-control-sm" value="curl -F 'url=<?= rtrim($_ENV['APP_URL'] ?? 'https://pruebaspons.duckdns.org', '/') ?>/telegram_bot_webhook.php?auth_token=<?= urlencode($_ENV['TELEGRAM_WEBHOOK_TOKEN'] ?? '') ?>' https://api.telegram.org/bot<?= $_ENV['TELEGRAM_BOT_TOKEN'] ?? '' ?>/setWebhook" id="curlUnifiedCommand" readonly>
|
|
<button class="btn btn-outline-secondary btn-sm" type="button" onclick="copyToClipboard(document.getElementById('curlUnifiedCommand'))">
|
|
<i class="bi bi-clipboard"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="alert alert-success mt-3 p-2 small">
|
|
<i class="bi bi-check-circle-fill me-1"></i>
|
|
<span data-translate="true">Este es el <strong>único webhook</strong> que necesitas configurar.</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-header">
|
|
<h5 class="mb-0" data-translate="true">Vista Previa</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="telegram-message">
|
|
<div class="telegram-message-avatar">
|
|
<img src="https://cdn4.telegram.org/file/vaG9VXzJ9Us3druVCEMoQv-1wwhBWO1hieJQxGZA9fY/1lZkbjXz6CQ" alt="Bot" class="rounded-circle" width="40">
|
|
</div>
|
|
<div class="telegram-message-content">
|
|
<div class="telegram-message-username">Bot de Interacción</div>
|
|
<div class="telegram-message-text">
|
|
<?= nl2br(htmlspecialchars(str_replace(
|
|
['{user_name}', '{group_link}'],
|
|
['UsuarioEjemplo', $config['group_invite_link'] ?? ''],
|
|
$config['message_text'] ?? ''
|
|
))) ?>
|
|
</div>
|
|
<?php if (!empty($config['group_invite_link'])): ?>
|
|
<div class="mt-2">
|
|
<a href="<?= htmlspecialchars($config['group_invite_link']) ?>"
|
|
class="btn btn-telegram"
|
|
target="_blank">
|
|
<?= htmlspecialchars($config['button_text'] ?? '') ?>
|
|
</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (!empty($stats)): ?>
|
|
<div class="card shadow-sm">
|
|
<div class="card-header">
|
|
<h5 class="mb-0" data-translate="true">Estadísticas</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<span data-translate="true">Usuarios únicos registrados:</span>
|
|
<span class="fw-bold"><?= number_format($stats['unique_users'] ?? 0) ?></span>
|
|
</div>
|
|
<div class="d-flex justify-content-between">
|
|
<span data-translate="true">Total de interacciones:</span>
|
|
<span class="fw-bold"><?= number_format($stats['total_interactions'] ?? 0) ?></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.telegram-message {
|
|
display: flex;
|
|
padding: 8px 16px;
|
|
margin-bottom: 1rem;
|
|
background-color: #182533;
|
|
border-radius: 8px;
|
|
color: #e1e9f2;
|
|
max-width: 100%;
|
|
}
|
|
.telegram-message-avatar {
|
|
margin-right: 12px;
|
|
flex-shrink: 0;
|
|
}
|
|
.telegram-message-avatar img {
|
|
border-radius: 50%;
|
|
}
|
|
.telegram-message-username {
|
|
font-weight: 500;
|
|
color: #6ab3f3;
|
|
margin-bottom: 4px;
|
|
}
|
|
.telegram-message-text {
|
|
margin-top: 2px;
|
|
line-height: 1.4;
|
|
white-space: pre-line;
|
|
}
|
|
.btn-telegram {
|
|
background-color: #2AABEE;
|
|
color: white;
|
|
border: none;
|
|
padding: 6px 12px;
|
|
border-radius: 4px;
|
|
font-size: 14px;
|
|
text-decoration: none;
|
|
display: inline-block;
|
|
}
|
|
.btn-telegram:hover {
|
|
background-color: #229ED9;
|
|
color: white;
|
|
}
|
|
</style>
|
|
|
|
<script>
|
|
function copyToClipboard(button) {
|
|
const input = button.closest('.input-group').querySelector('input');
|
|
input.select();
|
|
document.execCommand('copy');
|
|
|
|
// Cambiar el ícono temporalmente
|
|
const icon = button.querySelector('i');
|
|
const originalClass = icon.className;
|
|
icon.className = 'bi bi-check';
|
|
|
|
// Restaurar después de 2 segundos
|
|
setTimeout(() => {
|
|
icon.className = originalClass;
|
|
}, 2000);
|
|
}
|
|
</script>
|
|
|
|
<?php require_once __DIR__ . '/templates/footer.php'; ?>
|