118 lines
4.4 KiB
PHP
118 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* API - Obtener respuestas automáticas
|
|
* Fecha: 12 de enero de 2026
|
|
*/
|
|
|
|
require_once '../config/config.php';
|
|
|
|
// Verificar autenticación
|
|
requireAuthentication();
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
try {
|
|
$db = Database::getInstance();
|
|
|
|
// Verificar si la tabla existe, si no crearla
|
|
$tablesExist = $db->fetchAll("SHOW TABLES LIKE 'autoresponses'");
|
|
|
|
if (empty($tablesExist)) {
|
|
error_log("Creando tabla autoresponses...");
|
|
|
|
$createAutoResponsesSQL = "CREATE TABLE IF NOT EXISTS autoresponses (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
trigger_type ENUM('keyword', 'contains', 'exact', 'welcome', 'default') NOT NULL DEFAULT 'keyword',
|
|
trigger_value TEXT,
|
|
response_text TEXT NOT NULL,
|
|
response_type ENUM('text', 'template', 'menu') NOT NULL DEFAULT 'text',
|
|
template_name VARCHAR(255),
|
|
menu_id INT,
|
|
priority INT DEFAULT 0,
|
|
is_active BOOLEAN DEFAULT true,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
INDEX idx_trigger_type (trigger_type),
|
|
INDEX idx_is_active (is_active),
|
|
INDEX idx_priority (priority)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
|
|
|
$db->execute($createAutoResponsesSQL);
|
|
|
|
// Crear respuestas automáticas de ejemplo
|
|
$exampleResponses = [
|
|
[
|
|
'trigger_type' => 'welcome',
|
|
'trigger_value' => null,
|
|
'response_text' => '¡Hola! 👋 Bienvenido a nuestro servicio. ¿En qué puedo ayudarte?',
|
|
'response_type' => 'text',
|
|
'priority' => 10,
|
|
'is_active' => 1
|
|
],
|
|
[
|
|
'trigger_type' => 'keyword',
|
|
'trigger_value' => 'hola,buenos días,buenas tardes,buenas noches,hi,hello',
|
|
'response_text' => '¡Hola! 😊 Gracias por contactarnos. ¿En qué podemos ayudarte hoy?',
|
|
'response_type' => 'text',
|
|
'priority' => 5,
|
|
'is_active' => 1
|
|
],
|
|
[
|
|
'trigger_type' => 'keyword',
|
|
'trigger_value' => 'precio,precios,costo,costos,cuanto cuesta,tarifa',
|
|
'response_text' => '💰 Para información sobre precios, por favor visita nuestro sitio web o contacta directamente con nuestro equipo de ventas.',
|
|
'response_type' => 'text',
|
|
'priority' => 3,
|
|
'is_active' => 1
|
|
],
|
|
[
|
|
'trigger_type' => 'keyword',
|
|
'trigger_value' => 'horario,horarios,abierto,cerrado,cuando abren',
|
|
'response_text' => '🕒 Nuestros horarios de atención son:\nLunes a Viernes: 9:00 AM - 6:00 PM\nSábados: 9:00 AM - 2:00 PM\nDomingos: Cerrado',
|
|
'response_type' => 'text',
|
|
'priority' => 3,
|
|
'is_active' => 1
|
|
],
|
|
[
|
|
'trigger_type' => 'default',
|
|
'trigger_value' => null,
|
|
'response_text' => 'Gracias por tu mensaje. Un representante se pondrá en contacto contigo pronto. 🤝',
|
|
'response_type' => 'text',
|
|
'priority' => 1,
|
|
'is_active' => 1
|
|
]
|
|
];
|
|
|
|
foreach ($exampleResponses as $response) {
|
|
$db->insert('autoresponses', $response);
|
|
}
|
|
|
|
error_log("Tabla autoresponses creada con datos de ejemplo");
|
|
}
|
|
|
|
// Obtener todas las respuestas automáticas
|
|
$autoresponses = $db->fetchAll(
|
|
"SELECT * FROM autoresponses ORDER BY priority DESC, created_at DESC"
|
|
);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => $autoresponses,
|
|
'total' => count($autoresponses),
|
|
'message' => 'Respuestas automáticas cargadas correctamente'
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error en get_autoresponses.php: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Error interno del servidor',
|
|
'debug' => $e->getMessage(),
|
|
'data' => []
|
|
]);
|
|
}
|
|
?>
|