Files
contenedor_ibiza/bot/webhook.php
2026-01-19 15:20:36 -06:00

128 lines
4.7 KiB
PHP
Executable File

<?php
// Configurar logging de errores
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/../logs/bot_error.log');
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/TelegramBot.php';
class TurnoBot {
private $bot;
private $config;
public function __construct() {
$this->config = require __DIR__ . '/../config/config.php';
$this->bot = new TelegramBot();
}
public function handleUpdate($update) {
try {
// Manejar callback de botones inline
if (isset($update['callback_query'])) {
$this->handleCallback($update['callback_query']);
return;
}
// Manejar mensajes normales
if (!isset($update['message'])) {
return;
}
$message = $update['message'];
$chatId = $message['chat']['id'];
$text = trim($message['text'] ?? '');
if (empty($text)) {
return;
}
$textLower = mb_strtolower($text, 'UTF-8');
// Comandos
if ($textLower === '/start' || $textLower === '/menu' || $textLower === 'menu') {
$this->sendMenu($chatId);
} elseif ($textLower === '/turnos' || $textLower === 'turnos') {
$this->bot->sendMessage($chatId, $this->bot->getTablaTurnos(8));
} elseif ($textLower === '/semana' || $textLower === 'semana' || $textLower === 'hoy') {
$this->bot->sendMessage($chatId, $this->bot->getSemanaActual());
} elseif ($textLower === '/ayudantes' || $textLower === 'ayudantes') {
$ayudantes = $this->bot->getListaAyudantesParaBusqueda();
$this->bot->sendMessage($chatId, "<b>AYUDANTES DISPONIBLES:</b>\n\n" . implode("\n", $ayudantes));
} else {
// Buscar por nombre
$this->bot->sendMessage($chatId, $this->bot->getTurnosAyudante($text));
}
} catch (Exception $e) {
error_log("Error en handleUpdate: " . $e->getMessage());
if (isset($update['message']['chat']['id'])) {
$this->bot->sendMessage($update['message']['chat']['id'], "Error: " . $e->getMessage());
}
}
}
private function handleCallback($callback) {
try {
$callbackId = $callback['id'];
$data = $callback['data'];
$message = $callback['message'];
$chatId = $message['chat']['id'];
$messageId = $message['message_id'];
switch ($data) {
case 'ver_turnos':
$this->bot->answerCallback($callbackId, 'Cargando turnos...');
$this->bot->editMessage($chatId, $messageId, $this->bot->getTablaTurnos(8));
break;
case 'semana_actual':
$this->bot->answerCallback($callbackId, 'Cargando semana actual...');
$this->bot->editMessage($chatId, $messageId, $this->bot->getSemanaActual());
break;
case 'buscar_nombre':
$this->bot->answerCallback($callbackId, '');
$this->bot->deleteMessage($chatId, $messageId);
$this->bot->sendMessage($chatId, "🔍 <b>Buscar por Nombre</b>\n\nEscribe el nombre del ayudante que buscas:");
break;
case 'mi_turno':
$this->bot->answerCallback($callbackId, 'Enviando tu turno...');
$this->bot->editMessage($chatId, $messageId, "Por favor ingresa tu nombre para ver tu turno:");
break;
default:
$this->bot->answerCallback($callbackId, 'Opcion no reconocida');
}
} catch (Exception $e) {
error_log("Error en handleCallback: " . $e->getMessage());
}
}
private function sendMenu($chatId) {
$mensaje = "<b>BOT DE TURNOS - CONTENEDOR IBIZA</b>\n\n";
$mensaje .= "Selecciona una opcion del menu:\n\n";
$mensaje .= "Ver Turnos - Tabla completa de asignaciones\n";
$mensaje .= "Semana Actual - Quien tiene turno esta semana\n";
$mensaje .= "Buscar por Nombre - Consultar un ayudante especifico\n";
$mensaje .= "Mi Turno - Ver tu proximo turno";
$this->bot->sendKeyboard($chatId, $mensaje);
}
}
// Recibir actualización
$update = json_decode(file_get_contents('php://input'), true);
// Log para debugging
error_log("Webhook recibido: " . json_encode($update));
if ($update) {
$bot = new TurnoBot();
$bot->handleUpdate($update);
} else {
http_response_code(200);
echo "Webhook activo. Usa /start para ver el menu.";
}