top of page
bottom of page
console.log("✅ Running Chatbot Script in an iframe...");
// Ensure the script only runs in the browser
if (typeof window !== "undefined" && typeof document !== "undefined") {
console.log("✅ Confirmed: Running in the browser...");
setTimeout(() => {
console.log("✅ Attempting to add chatbot UI...");
// Create chatbot UI
const chatbot = document.createElement("div");
chatbot.innerHTML = `
`;
document.body.appendChild(chatbot);
console.log("✅ Chatbot added to the page");
// Button to toggle chat
const chatButton = document.getElementById("chatbot-btn");
const chatWindow = document.getElementById("chat-window");
const chatInput = document.getElementById("chat-input");
const chatBody = document.getElementById("chat-body");
chatButton.addEventListener("click", function () {
chatWindow.style.display = chatWindow.style.display === "none" ? "block" : "none";
});
chatInput.addEventListener("keypress", async function (e) {
if (e.key === "Enter" && chatInput.value.trim() !== "") {
const userMessage = chatInput.value;
chatInput.value = "";
displayMessage("You", userMessage);
try {
console.log("✅ Sending message to backend...");
const response = await fetch("https://www.logic3consulting.com/_functions/getChatResponse", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ message: userMessage })
});
const data = await response.json();
console.log("✅ Received response:", data);
let botMessage = data.reply || "Sorry, I couldn't understand that.";
displayMessage("Bot", botMessage);
} catch (error) {
console.error("❌ Error fetching response:", error);
displayMessage("Bot", "Oops! Something went wrong.");
}
}
});
function displayMessage(sender, message) {
const messageDiv = document.createElement("div");
messageDiv.innerHTML = `${sender}: ${message}`;
chatBody.appendChild(messageDiv);
chatBody.scrollTop = chatBody.scrollHeight;
}
}, 3000);
} else {
console.error("❌ Wix is running this in a backend environment where `document` does not exist.");
}