(function () { const chatHTML = `
Bot Avatar
Hi! I’m Claybot
`; async function loadLibrary(src) { return new Promise((resolve, reject) => { const script = document.createElement("script"); script.src = src; script.onload = resolve; script.onerror = reject; document.head.appendChild(script); }); } console.log("Injecting script"); // const container = document.createElement("div"); // container.innerHTML = chatHTML; // document.body.appendChild(container); document.addEventListener("DOMContentLoaded", function () { const container = document.createElement("div"); container.innerHTML = chatHTML; document.body.appendChild(container); const projectKey = "b4ce27ba-46ff-4b27-814a-ee2303cc0b55"; // References to elements const chatToggleBtn = document.getElementById("chatToggleBtn"); const chatWindow = document.getElementById("chatWindow"); const chatMessages = document.getElementById("chatMessages"); const chatInput = document.getElementById("chatInput"); const micBtn1 = document.getElementById("micBtn1"); const micBtn2 = document.getElementById("micBtn2"); // const chatInput2 = document.getElementById("chatInput2"); const sendIcon = document.querySelector(".sendIcon"); const minimizeBtn = document.getElementById("minimizeBtn"); const maximizeBtnL = document.getElementById("maximizeBtnL"); const moveAsideBtn = document.getElementById("moveAsideBtn"); const minimizeBtnR = document.getElementById("minimizeBtnR"); const maximizeBtnR = document.getElementById("maximizeBtnR"); const chatArea = document.getElementById("chatArea"); const chatInitR = document.getElementById("chatInitR"); // const chatbotInitContainer = document.getElementById( // "chatbotInitContainer" // ); const chatTitle = document.getElementById("chatTitle"); const suggestion = document.getElementById("suggestion"); const suggestionInside = document.getElementById("suggestion-inside"); // chatbotInitContainer.classList.add("open"); chatToggleBtn.classList.toggle("open"); function submitSuggestion(suggestionText) { suggestionInside.style.display = "none"; // Hide suggestion buttons suggestionInside.remove(); // Remove the suggestion element from the DOM chatInput.value = suggestionText; // Set the input value to the suggestion sendFirstMessage(); // Call the function to send the message } document.querySelectorAll(".suggestbtn").forEach((button) => { button.addEventListener("click", function () { submitSuggestion(this.innerText); // Or use this.textContent }); }); // We'll also keep a reference to the SVG icon // const micIcon2 = micBtn2.querySelector("svg"); // Toggle chat window chatToggleBtn.addEventListener("click", () => { chatWindow.classList.toggle("open"); chatToggleBtn.classList.toggle("open"); // chatToggleBtn.classList.remove("open"); }); function openChatModal() { // chatbotInitContainer.style.height = "450px"; // chatbotInitContainer.style.padding = "10px 64px"; chatArea.style.background = "white"; chatArea.style.border = "1px solid #ccc"; chatArea.style.borderRadius = "24px"; chatArea.style.boxShadow = "0 4px 12px rgba(0,0,0,0.1)"; chatArea.style.padding = "10px"; chatMessages.style.display = "flex"; minimizeBtn.style.display = "block"; chatTitle.style.display = "block"; } function minimizeChatModal() { chatArea.style.background = "transparent"; chatArea.style.border = "none"; chatArea.style.borderRadius = "0px"; chatArea.style.boxShadow = "none"; chatArea.style.padding = "0px"; chatMessages.style.display = "none"; minimizeBtn.style.display = "none"; chatTitle.style.display = "none"; maximizeBtnL.style.display = "block"; } function sendFirstMessage() { const question = chatInput.value.trim(); if (question) { sendMessage(question); } } sendIcon.addEventListener("click", () => { // chatWindow.classList.toggle('open'); sendFirstMessage(); }); chatInitR.addEventListener("click", () => { const question = chatInput.value.trim(); if (question) { sendMessage(question); } }); minimizeBtnR.addEventListener("click", () => { chatWindow.classList.remove("open"); // chatbotInitContainer.classList.remove("open"); chatToggleBtn.classList.add("open"); }); function createBubble(text, sender) { const bubble = document.createElement("div"); bubble.classList.add("bubble"); // Create a container for avatar and message const container = document.createElement("div"); container.classList.add("bubbleContainer"); if (sender === "user") { bubble.classList.add("userBubble"); } else { bubble.classList.add("botBubble"); // Add bot avatar const botAvatar = document.createElement("img"); botAvatar.src = "https://bot.claybox.ai/static/ab.png"; // Replace with your bot avatar image URL botAvatar.alt = "Bot Avatar"; botAvatar.classList.add("avatar", "botAvatar"); // container.appendChild(botAvatar); bubble.appendChild(botAvatar); } // Add the message text const message = document.createElement("div"); message.classList.add("bubbleText"); message.innerHTML = text; // container.appendChild(message); bubble.appendChild(message); return bubble; } // // Helper: create message bubble // function createBubble(text, sender) { // const bubble = document.createElement("div"); // bubble.classList.add("bubble"); // if (sender === "user") { // bubble.classList.add("userBubble"); // } else { // bubble.classList.add("botBubble"); // } // // bubble.textContent = text; // bubble.innerHTML = text; // return bubble; // } // Scroll chat to bottom function scrollToBottom() { chatMessages.scrollTop = chatMessages.scrollHeight; } // ========== Text-to-Speech (Voice Output) ========== function speakText(text) { if ("speechSynthesis" in window) { const utterance = new SpeechSynthesisUtterance(text); // You can customize the voice, language, pitch, rate, etc. // utterance.lang = 'en-US'; // utterance.pitch = 1.0; // utterance.rate = 1.0; window.speechSynthesis.speak(utterance); } } // ========== Speech-to-Text (Voice Input) ========== let recognition; let isListening = false; if ("webkitSpeechRecognition" in window) { recognition = new webkitSpeechRecognition(); recognition.continuous = false; recognition.interimResults = false; recognition.lang = "en-US"; recognition.onstart = () => { isListening = true; // Animate mic icon micIcon1.classList.add("listening"); micIcon2.classList.add("listening"); }; recognition.onend = () => { isListening = false; micIcon1.classList.remove("listening"); micIcon2.classList.remove("listening"); }; recognition.onresult = (event) => { const transcript = event.results[0][0].transcript; chatInput.value = transcript; // put recognized text in input // Optionally, immediately send message: // sendMessage(transcript); chatInput2.value = transcript; // put recognized text in input }; } else { // If not supported, disable mic button micBtn1.disabled = true; micBtn1.title = "Speech recognition not supported in this browser"; micBtn2.disabled = true; micBtn2.title = "Speech recognition not supported in this browser"; } const params = new URLSearchParams(window.location.search); const msg = params.get('msg'); if (msg === 'feedback') { console.log("matched"); chatToggleBtn.classList.toggle("open"); chatWindow.classList.toggle("open"); sendMessage("Hey Claybot! I want to provide feedback for the workshop");} loadLibrary("https://cdn.jsdelivr.net/npm/marked/marked.min.js"); // Load marked.js dynamically // ========== Sending Message to Server ========== async function sendMessage(question) { // Append user's message const userBubble = createBubble(question, "user"); chatMessages.appendChild(userBubble); scrollToBottom(); // Clear input chatInput.value = ""; // Show a 'typing...' bubble const typingBubble = createBubble("...", "bot"); chatMessages.appendChild(typingBubble); scrollToBottom(); // Generate a random UUID function generateUUID() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace( /[xy]/g, function (c) { const r = (Math.random() * 16) | 0, v = c === "x" ? r : (r & 0x3) | 0x8; return v.toString(16); } ); } // Save UUID in session storage let sessionUUID = sessionStorage.getItem("sessionUUID"); if (!sessionUUID) { sessionUUID = generateUUID(); sessionStorage.setItem("sessionUUID", sessionUUID); } const chatId = "824bdc3e-6c92-495f-9dd6-2821079b0852"; // Add UUID to the request body const requestBody = { query: question, project_id: projectKey, id: chatId || sessionUUID, }; const response = await fetch("https://bot.claybox.ai/claybox", { method: "POST", headers: { "Content-Type": "application/json", Authorization: "Basic " + btoa("ayush:Ecosystm"), }, body: JSON.stringify(requestBody), }); const reader = response.body.getReader(); const decoder = new TextDecoder("utf-8"); let botAnswer = ""; // Format the bot's response as markdown function formatMarkdown(text) { const markdownContainer = document.createElement("div"); markdownContainer.innerHTML = marked(text); return markdownContainer; } while (true) { const { done, value } = await reader.read(); if (done) break; botAnswer += decoder.decode(value, { stream: true }); typingBubble.innerHTML = marked.parse(botAnswer); scrollToBottom(); } typingBubble.remove(); const botBubble = createBubble(marked.parse(botAnswer), "bot"); // const botBubble = createBubble(botAnswer, 'bot'); chatMessages.appendChild(botBubble); scrollToBottom(); } // Press Enter => send message chatInput.addEventListener("keypress", (e) => { if (e.key === "Enter") { e.preventDefault(); const question = chatInput.value.trim(); if (question) { sendMessage(question); } } }); }); })();