feat: bot WhatsApp Business API — Palmas360 inicial
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../config/env.php';
|
||||
require_once __DIR__ . '/../config/db.php';
|
||||
require_once __DIR__ . '/../admin/v1/WpWebhook.php';
|
||||
require_once __DIR__ . '/../middleware/SessionAuth.php';
|
||||
require_once __DIR__ . '/../admin/LoginController.php';
|
||||
require_once __DIR__ . '/../admin/DashboardController.php';
|
||||
|
||||
// ─── Helper de respuesta JSON ────────────────────────────────────────────────
|
||||
function jsonResponse(int $status, array $body): void
|
||||
{
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($body, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
function serveHtml(string $filename): void
|
||||
{
|
||||
$file = __DIR__ . '/' . $filename;
|
||||
if (!file_exists($file)) {
|
||||
jsonResponse(404, ['error' => 'Página no encontrada']);
|
||||
}
|
||||
http_response_code(200);
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
readfile($file);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ─── Router ──────────────────────────────────────────────────────────────────
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
// Normalizar: quitar trailing slash
|
||||
$path = rtrim($path, '/') ?: '/';
|
||||
|
||||
// ─── Tabla de rutas ──────────────────────────────────────────────────────────
|
||||
$routes = [
|
||||
['GET', '/health', fn() => jsonResponse(200, ['status' => 'ok', 'service' => 'bot-palmas360'])],
|
||||
|
||||
// ─── WhatsApp webhook ────────────────────────────────────────────────────
|
||||
// Seguridad: HMAC-SHA256 (X-Hub-Signature-256) verificado dentro del controlador
|
||||
['GET', '/admin/v1/wp-webhook', fn() => WpWebhook::verify()],
|
||||
['POST', '/admin/v1/wp-webhook', fn() => WpWebhook::receive()],
|
||||
|
||||
// ─── Login / Logout ──────────────────────────────────────────────────────
|
||||
['GET', '/login', fn() => LoginController::showForm()],
|
||||
['POST', '/login', fn() => LoginController::authenticate()],
|
||||
['GET', '/logout', fn() => LoginController::logout()],
|
||||
|
||||
// ─── Dashboard protegido ─────────────────────────────────────────────────
|
||||
['GET', '/admin/dashboard', fn() => DashboardController::index()],
|
||||
['GET', '/admin/live', fn() => DashboardController::live()],
|
||||
['GET', '/admin/webhook/stream', fn() => DashboardController::stream()],
|
||||
['GET', '/admin/webhook/raw', fn() => DashboardController::getRaw()],
|
||||
|
||||
// ─── Páginas legales (requeridas por Meta/WhatsApp Business) ────────────
|
||||
['GET', '/politicas', fn() => serveHtml('politicas.html')],
|
||||
['GET', '/eliminacion-datos-usuario', fn() => serveHtml('eliminacion-datos-usuario.html')],
|
||||
['GET', '/condiciones-servicio', fn() => serveHtml('condiciones-servicio.html')],
|
||||
];
|
||||
|
||||
// ─── Despacho ────────────────────────────────────────────────────────────────
|
||||
foreach ($routes as [$routeMethod, $routePath, $handler]) {
|
||||
if ($method === $routeMethod && $path === $routePath) {
|
||||
$handler();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
jsonResponse(404, ['error' => 'Ruta no encontrada', 'path' => $path]);
|
||||
Reference in New Issue
Block a user