Vai direttamente al contenuto
(function() {
// === CONFIG ===
const PROXY_URL = "/apps/ai-chat"; // Rotta App Proxy Shopify
const CHAT_TITLE = "Assistenza AI";
const AUTO_OPEN_DELAY = 5000; // 5 secondi
// === HTML DEL WIDGET ===
const widgetHTML = `
💬 Chat AI
`;
document.body.insertAdjacentHTML("beforeend", widgetHTML);
// === ELEMENTI ===
const chatBtn = document.getElementById("aiChatButton");
const chatWin = document.getElementById("aiChatWindow");
const chatClose = document.getElementById("aiChatClose");
const chatMessages = document.getElementById("aiChatMessages");
const chatInput = document.getElementById("aiChatInput");
const chatSend = document.getElementById("aiChatSend");
chatBtn.onclick = () => openChat();
chatClose.onclick = () => closeChat();
function openChat() {
chatWin.style.display = "block";
chatBtn.style.display = "none";
}
function closeChat() {
chatWin.style.display = "none";
chatBtn.style.display = "block";
}
// === MESSAGGI ===
function addMessage(role, text) {
const div = document.createElement("div");
div.style.margin = "8px 0";
div.style.textAlign = role === "user" ? "right" : "left";
div.innerHTML =
role === "user"
? `${text}
`
: `${text}
`;
chatMessages.appendChild(div);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// === INVIO MESSAGGIO ===
async function sendMessage() {
const msg = chatInput.value.trim();
if (!msg) return;
addMessage("user", msg);
chatInput.value = "";
const loading = document.createElement("div");
loading.innerHTML = `...
`;
chatMessages.appendChild(loading);
chatMessages.scrollTop = chatMessages.scrollHeight;
try {
const response = await fetch(PROXY_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: msg }),
});
const text = await response.text();
loading.remove();
// Tenta parsing JSON
let data;
try {
data = JSON.parse(text);
} catch {
addMessage("ai", "⚠️ Errore: risposta non valida dal server.");
console.warn("Server response:", text);
return;
}
if (data.ok && data.title) {
// Mostra prodotto singolo
addMessage(
"ai",
`${data.title}
Prezzo: ${data.price}
${data.description || ""}
Vedi prodotto`
);
} else if (Array.isArray(data.products)) {
// Mostra elenco prodotti
let html = "Ecco i prodotti trovati:
";
for (const p of data.products) {
html += ``;
}
addMessage("ai", html);
} else if (data.error) {
addMessage("ai", `⚠️ ${data.error}`);
} else {
addMessage("ai", data.answer || "Nessuna risposta disponibile.");
}
} catch (err) {
loading.remove();
addMessage("ai", "Errore di connessione al server.");
console.error(err);
}
}
chatSend.onclick = sendMessage;
chatInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") sendMessage();
});
// === AUTO-OPEN DOPO 5 SECONDI (solo 1 volta per sessione) ===
if (!sessionStorage.getItem("aiChatOpened")) {
setTimeout(() => {
openChat();
sessionStorage.setItem("aiChatOpened", "true");
}, AUTO_OPEN_DELAY);
}
// === Messaggio iniziale ===
addMessage(
"ai",
"Ciao! Posso aiutarti con prodotti, ordini e politiche del negozio. Scrivi pure la tua domanda."
);
})();