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, "AYUDANTES DISPONIBLES:\n\n" . implode("\n", $ayudantes)); } elseif ($textLower === '/pdf' || $textLower === 'pdf' || $textLower === 'mi pdf') { $this->bot->sendPDFGeneral($chatId); } else { // Buscar por nombre - verificar si existe el usuario $config = require __DIR__ . '/../config/config.php'; try { $pdo = new PDO( "mysql:host={$config['db']['host']};port={$config['db']['port']};dbname={$config['db']['database']};charset=utf8mb4", $config['db']['username'], $config['db']['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ); } catch (Exception $e) { $this->bot->sendMessage($chatId, "Error de conexion."); return; } $stmt = $pdo->prepare("SELECT * FROM users WHERE (nombre LIKE ? OR username LIKE ?) AND rol = 'ayudante' AND activo = 1 LIMIT 1"); $stmt->execute(["%$text%", "%$text%"]); $user = $stmt->fetch(); if ($user) { $this->bot->sendMessage($chatId, "Generando PDF de turnos..."); $this->bot->sendPDF($chatId, $user['id']); } else { $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 'mi_pdf': $this->bot->answerCallback($callbackId, 'Generando PDF...'); $this->bot->sendPDFGeneral($chatId); break; case 'buscar_nombre': $this->bot->answerCallback($callbackId, ''); $this->bot->deleteMessage($chatId, $messageId); $this->bot->sendMessage($chatId, "🔍 Buscar por Nombre\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 = "BOT DE TURNOS - CONTENEDOR IBIZA\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 .= "Mi PDF - Descargar horarios en PDF\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."; }