CONFIG.rateWindow
) {
rateStart =
ahora;
messageCount =
0;
}
if (
messageCount >=
CONFIG.maxMessages
) {
return false;
}
messageCount++;
return true;
}
/* ==========================================================
MENSAJE DEL BOT
========================================================== */
function bot(texto) {
const messages =
get("romani-messages");
const row =
document.createElement(
"div"
);
row.className =
"romani-message bot";
const avatar =
document.createElement(
"div"
);
avatar.className =
"romani-bot-avatar";
const img =
document.createElement(
"img"
);
img.src =
CONFIG.image;
img.alt =
"Romani";
img.draggable =
false;
avatar.appendChild(
img
);
const bubble =
document.createElement(
"div"
);
bubble.className =
"romani-bubble";
/*
* IMPORTANTE:
* textContent evita que un texto
* se interprete como HTML.
*/
bubble.textContent =
texto;
row.appendChild(
avatar
);
row.appendChild(
bubble
);
messages.appendChild(
row
);
scrollBottom();
}
/* ==========================================================
MENSAJE USUARIO
========================================================== */
function user(texto) {
const messages =
get("romani-messages");
const row =
document.createElement(
"div"
);
row.className =
"romani-message user";
const bubble =
document.createElement(
"div"
);
bubble.className =
"romani-bubble";
bubble.textContent =
texto;
row.appendChild(
bubble
);
messages.appendChild(
row
);
scrollBottom();
}
/* ==========================================================
CREAR OPCIONES
========================================================== */
function options(items) {
const messages =
get("romani-messages");
const box =
document.createElement(
"div"
);
box.className =
"romani-options";
items.forEach(
function (item) {
const button =
document.createElement(
"button"
);
button.type =
"button";
button.className =
"romani-option " +
(
item.className ||
""
);
/* ICONO */
const icon =
document.createElement(
"span"
);
icon.className =
"romani-option-icon";
/*
* SVG internos controlados.
*/
icon.innerHTML =
item.icon || "";
/* CONTENIDO */
const content =
document.createElement(
"span"
);
content.className =
"romani-option-content";
const title =
document.createElement(
"span"
);
title.className =
"romani-option-title";
title.textContent =
item.text;
content.appendChild(
title
);
if (
item.description
) {
const description =
document.createElement(
"span"
);
description.className =
"romani-option-description";
description.textContent =
item.description;
content.appendChild(
description
);
}
/* FLECHA */
const arrow =
document.createElement(
"span"
);
arrow.className =
"romani-option-arrow";
arrow.textContent =
"→";
button.appendChild(
icon
);
button.appendChild(
content
);
button.appendChild(
arrow
);
button.addEventListener(
"click",
item.action
);
box.appendChild(
button
);
}
);
messages.appendChild(
box
);
scrollBottom();
}
/* ==========================================================
CONVERSACIÓN INICIAL
========================================================== */
function iniciarConversacion() {
bot(
"Hola, soy Romani."
);
setTimeout(
function () {
bot(
"Estoy aquí para orientarte. ¿Qué estás buscando?"
);
options([
{
text:
"Servicios",
description:
"Conoce nuestras soluciones",
icon:
ICONS.services,
action:
mostrarServicios
},
{
text:
"Capacitaciones",
description:
"Formación especializada",
icon:
ICONS.training,
action:
mostrarCapacitaciones
},
{
text:
"WhatsApp",
description:
"Escríbenos directamente",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp();
}
},
{
text:
"Contacto",
description:
"Hablemos de tu proyecto",
icon:
ICONS.contact,
action:
abrirContacto
}
]);
},
350
);
}
/* ==========================================================
SERVICIOS
========================================================== */
function mostrarServicios() {
user(
"Quiero conocer sus servicios."
);
bot(
"Claro. Selecciona el servicio que deseas conocer."
);
options([
{
text:
"Auditoría Trinorma",
description:
"Auditorías y sistemas de gestión",
icon:
ICONS.services,
action:
function () {
mostrarServicio(
"Auditoría Trinorma",
"Te orientamos sobre auditorías relacionadas con sistemas de gestión."
);
}
},
{
text:
"Sistemas de Gestión",
description:
"Implementación y acompañamiento",
icon:
ICONS.services,
action:
function () {
mostrarServicio(
"Sistemas de Gestión",
"Soluciones de implementación y acompañamiento para sistemas de gestión."
);
}
},
{
text:
"Monitoreos Ocupacionales",
description:
"Evaluación de agentes ocupacionales",
icon:
ICONS.services,
action:
function () {
mostrarServicio(
"Monitoreos Ocupacionales",
"Evaluación de agentes ocupacionales según las necesidades de la organización."
);
}
},
{
text:
"Matriz IPERC",
description:
"Identificación y evaluación de riesgos",
icon:
ICONS.services,
action:
function () {
mostrarServicio(
"Matriz IPERC",
"Apoyo en la identificación de peligros y evaluación de riesgos."
);
}
},
{
text:
"ITSE",
description:
"Seguridad en edificaciones",
icon:
ICONS.services,
action:
function () {
mostrarServicio(
"ITSE",
"Orientación relacionada con Inspecciones Técnicas de Seguridad en Edificaciones."
);
}
},
{
text:
"Volver",
description:
"Regresar al menú principal",
icon:
ICONS.back,
action:
iniciarConversacion
}
]);
}
/* ==========================================================
DETALLE SERVICIO
========================================================== */
function mostrarServicio(
titulo,
descripcion
) {
user(
titulo
);
bot(
titulo +
"\n\n" +
descripcion
);
options([
{
text:
"Ver información",
description:
"Conocer más sobre este servicio",
className:
"gold",
icon:
ICONS.services,
action:
function () {
navegarSeguro(
CONFIG.servicios
);
}
},
{
text:
"Consultar por WhatsApp",
description:
"Hablar directamente con nosotros",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp(
"Hola Romani, estoy interesado(a) en información sobre " +
titulo +
"."
);
}
},
{
text:
"Ver otros servicios",
description:
"Regresar al listado",
icon:
ICONS.back,
action:
mostrarServicios
}
]);
}
/* ==========================================================
CAPACITACIONES
========================================================== */
function mostrarCapacitaciones() {
user(
"Quiero conocer sus capacitaciones."
);
bot(
"Perfecto. ¿Qué tipo de capacitación estás buscando?"
);
options([
{
text:
"Seguridad y Salud en el Trabajo",
description:
"Formación para equipos de trabajo",
icon:
ICONS.training,
action:
function () {
mostrarCapacitacion(
"Seguridad y Salud en el Trabajo"
);
}
},
{
text:
"Sistemas de Gestión",
description:
"Formación especializada",
icon:
ICONS.training,
action:
function () {
mostrarCapacitacion(
"Sistemas de Gestión"
);
}
},
{
text:
"Salud Ocupacional",
description:
"Capacitación especializada",
icon:
ICONS.training,
action:
function () {
mostrarCapacitacion(
"Salud Ocupacional"
);
}
},
{
text:
"Ver todas las capacitaciones",
description:
"Explorar nuestra oferta",
className:
"gold",
icon:
ICONS.training,
action:
function () {
navegarSeguro(
CONFIG.capacitaciones
);
}
},
{
text:
"Volver",
description:
"Regresar al menú principal",
icon:
ICONS.back,
action:
iniciarConversacion
}
]);
}
/* ==========================================================
DETALLE CAPACITACIÓN
========================================================== */
function mostrarCapacitacion(
titulo
) {
user(
titulo
);
bot(
titulo +
"\n\n" +
"Contamos con formación especializada orientada a fortalecer los conocimientos y competencias de los equipos de trabajo."
);
options([
{
text:
"Ver capacitaciones",
description:
"Conocer nuestra oferta",
className:
"gold",
icon:
ICONS.training,
action:
function () {
navegarSeguro(
CONFIG.capacitaciones
);
}
},
{
text:
"Consultar por WhatsApp",
description:
"Solicitar información",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp(
"Hola Romani, estoy interesado(a) en la capacitación de " +
titulo +
"."
);
}
},
{
text:
"Ver otras capacitaciones",
description:
"Regresar al listado",
icon:
ICONS.back,
action:
mostrarCapacitaciones
}
]);
}
/* ==========================================================
WHATSAPP
========================================================== */
function abrirWhatsApp(
mensaje
) {
if (
typeof mensaje !==
"string"
) {
mensaje =
"Hola Romani Consultoría, quisiera recibir información.";
}
mensaje =
mensaje.substring(
0,
300
);
user(
"Quiero comunicarme por WhatsApp."
);
bot(
"Claro. Te llevaré directamente a nuestro WhatsApp."
);
const url =
"https://wa.me/" +
CONFIG.whatsapp +
"?text=" +
encodeURIComponent(
mensaje
);
setTimeout(
function () {
window.open(
url,
"_blank",
"noopener,noreferrer"
);
},
450
);
}
/* ==========================================================
CONTACTO
========================================================== */
function abrirContacto() {
user(
"Quiero contactar con Romani."
);
bot(
"Perfecto. Puedes visitar nuestra sección de contacto para encontrar nuestros canales de comunicación."
);
options([
{
text:
"Ir a Contacto",
description:
"Ver nuestros canales de comunicación",
className:
"gold",
icon:
ICONS.contact,
action:
function () {
navegarSeguro(
CONFIG.contacto
);
}
},
{
text:
"Escribir por WhatsApp",
description:
"Hablar directamente con nosotros",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp(
"Hola Romani Consultoría, quisiera comunicarme con su equipo."
);
}
}
]);
}
/* ==========================================================
PREGUNTA ESCRITA
========================================================== */
function enviarPregunta() {
const input =
get("romani-input");
let pregunta =
input.value.trim();
if (!pregunta) {
return;
}
if (
pregunta.length >
CONFIG.maxMessageLength
) {
bot(
"Tu consulta es demasiado larga. Por favor, escribe una consulta más corta."
);
input.value = "";
return;
}
if (
pareceCodigo(
pregunta
)
) {
user(
"[Consulta bloqueada]"
);
bot(
"Por seguridad, solo puedo procesar consultas relacionadas con la información de Romani Consultoría."
);
input.value = "";
return;
}
if (
!permitido()
) {
bot(
"Has realizado varias consultas seguidas. Espera unos segundos y vuelve a intentarlo."
);
return;
}
user(
pregunta
);
input.value = "";
mostrarEscribiendo(
function () {
responder(
pregunta
);
}
);
}
/* ==========================================================
RESPUESTAS
========================================================== */
function responder(
pregunta
) {
const texto =
normalizar(
pregunta
);
/* SERVICIOS */
if (
texto.includes(
"servicio"
) ||
texto.includes(
"servicios"
) ||
texto.includes(
"que hacen"
) ||
texto.includes(
"que ofrecen"
)
) {
bot(
"Claro. Puedo ayudarte a conocer nuestros servicios."
);
options([
{
text:
"Ver servicios",
description:
"Conoce nuestras soluciones",
icon:
ICONS.services,
action:
mostrarServicios
}
]);
return;
}
/* CAPACITACIONES */
if (
texto.includes(
"capacit"
) ||
texto.includes(
"curso"
) ||
texto.includes(
"cursos"
)
) {
bot(
"Claro. Puedo ayudarte a encontrar la capacitación que estás buscando."
);
options([
{
text:
"Ver capacitaciones",
description:
"Explorar nuestra oferta",
icon:
ICONS.training,
action:
mostrarCapacitaciones
}
]);
return;
}
/* WHATSAPP */
if (
texto.includes(
"whatsapp"
) ||
texto.includes(
"wsp"
) ||
texto.includes(
"numero"
) ||
texto.includes(
"celular"
)
) {
bot(
"Puedes comunicarte directamente con nuestro equipo por WhatsApp."
);
options([
{
text:
"Abrir WhatsApp",
description:
"Hablar directamente con nosotros",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp();
}
}
]);
return;
}
/* CONTACTO */
if (
texto.includes(
"contacto"
) ||
texto.includes(
"correo"
) ||
texto.includes(
"telefono"
) ||
texto.includes(
"comunicar"
)
) {
bot(
"Puedes encontrar nuestros canales de comunicación en la sección Contacto."
);
options([
{
text:
"Ir a Contacto",
description:
"Ver nuestros canales",
className:
"gold",
icon:
ICONS.contact,
action:
abrirContacto
}
]);
return;
}
/* MONITOREOS */
if (
texto.includes(
"monitoreo"
)
) {
mostrarServicio(
"Monitoreos Ocupacionales",
"Evaluación de agentes ocupacionales según las necesidades de la organización."
);
return;
}
/* IPERC */
if (
texto.includes(
"iperc"
) ||
texto.includes(
"matriz"
)
) {
mostrarServicio(
"Matriz IPERC",
"Apoyo en la identificación de peligros y evaluación de riesgos."
);
return;
}
/* ITSE */
if (
texto.includes(
"itse"
) ||
texto.includes(
"inspeccion"
)
) {
mostrarServicio(
"ITSE",
"Orientación relacionada con Inspecciones Técnicas de Seguridad en Edificaciones."
);
return;
}
/* AUDITORÍA */
if (
texto.includes(
"auditoria"
) ||
texto.includes(
"trinorma"
)
) {
mostrarServicio(
"Auditoría Trinorma",
"Te orientamos sobre auditorías relacionadas con sistemas de gestión."
);
return;
}
/* SISTEMAS DE GESTIÓN */
if (
texto.includes(
"sistema de gestion"
) ||
texto.includes(
"sistemas de gestion"
) ||
texto.includes(
"iso"
)
) {
mostrarServicio(
"Sistemas de Gestión",
"Soluciones de implementación y acompañamiento para sistemas de gestión."
);
return;
}
/* PRECIOS */
if (
texto.includes(
"precio"
) ||
texto.includes(
"costo"
) ||
texto.includes(
"cuanto"
) ||
texto.includes(
"cotizacion"
)
) {
bot(
"Para brindarte una propuesta adecuada necesitamos conocer las características de tu requerimiento."
);
options([
{
text:
"Consultar por WhatsApp",
description:
"Solicitar una propuesta",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp(
"Hola Romani Consultoría, quisiera solicitar información sobre una propuesta."
);
}
},
{
text:
"Ir a Contacto",
description:
"Hablemos de tu proyecto",
className:
"gold",
icon:
ICONS.contact,
action:
abrirContacto
}
]);
return;
}
/* RESPUESTA POR DEFECTO */
bot(
"Puedo ayudarte con información sobre nuestros servicios, capacitaciones, WhatsApp y Contacto."
);
options([
{
text:
"Servicios",
description:
"Conoce nuestras soluciones",
icon:
ICONS.services,
action:
mostrarServicios
},
{
text:
"Capacitaciones",
description:
"Formación especializada",
icon:
ICONS.training,
action:
mostrarCapacitaciones
},
{
text:
"WhatsApp",
description:
"Escríbenos directamente",
className:
"whatsapp",
icon:
ICONS.whatsapp,
action:
function () {
abrirWhatsApp();
}
},
{
text:
"Contacto",
description:
"Hablemos de tu proyecto",
icon:
ICONS.contact,
action:
abrirContacto
}
]);
}
/* ==========================================================
ANIMACIÓN "ESCRIBIENDO"
========================================================== */
function mostrarEscribiendo(
callback
) {
const messages =
get("romani-messages");
const typing =
document.createElement(
"div"
);
typing.className =
"romani-typing";
for (
let i = 0;
i < 3;
i++
) {
const dot =
document.createElement(
"span"
);
typing.appendChild(
dot
);
}
messages.appendChild(
typing
);
scrollBottom();
setTimeout(
function () {
if (
typing &&
typing.parentNode
) {
typing.remove();
}
callback();
},
600
);
}
/* ==========================================================
SCROLL
========================================================== */
function scrollBottom() {
const messages =
get("romani-messages");
setTimeout(
function () {
messages.scrollTop =
messages.scrollHeight;
},
30
);
}
/* ==========================================================
NAVEGACIÓN CONTROLADA
========================================================== */
function navegarSeguro(
url
) {
const permitidas = [
CONFIG.servicios,
CONFIG.capacitaciones,
CONFIG.contacto
];
if (
!permitidas.includes(
url
)
) {
return;
}
window.location.href =
url;
}
})();