(function() { // ====== CONFIG ====== const API_URL = "https://snugly.cloud/services/aiexpert/api/"; //const AGENT_ID = "AGENT_ID_HERE"; // Replace or inject dynamically const currentScript = document.currentScript; // Read the custom attribute const AGENT_ID = currentScript.getAttribute('data-agent-id'); // ====== SESSION HANDLING ====== let sessionId = localStorage.getItem("myai_expert_session"); if (!sessionId) { sessionId = (crypto.randomUUID && crypto.randomUUID()) || 'sess-' + Math.random().toString(36).substr(2, 9); localStorage.setItem("myai_expert_session", sessionId); } var currentcol = currentScript.getAttribute('data-color'); var currentpos = currentScript.getAttribute('data-pos'); if (!currentcol) currentcol="#4A90E2"; if (!currentpos) currentpos="br"; var poscode=''; if (currentpos=='br') { poscode='bottom: 20px; right: 20px;'; } if (currentpos=='bl') { poscode='bottom: 20px; left: 20px;'; } if (currentpos=='bm') { poscode='bottom: 20px; left: 50%;'; } // ====== STYLES ====== const style = document.createElement("style"); style.innerHTML = ` .myai-messages { background-color: #eee!important; color: black!important; } #myai-chat-button { position: fixed; ${poscode}; background: #${currentcol}; color: white; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 4px 8px rgba(0,0,0,0.2); font-size: 28px; z-index: 999999; } #myai-chat-popup { position: fixed; bottom: 90px; right: 20px; width: 320px; height: 420px; background: white; border-radius: 10px; box-shadow: 0 4px 12px rgba(0,0,0,0.3); display: none; flex-direction: column; z-index: 999999; } #myai-chat-header { background: #${currentcol}; color: white; padding: 10px; border-top-left-radius: 10px; border-top-right-radius: 10px; font-weight: bold; font-size: 16px; } #myai-chat-description { font-size: 12px; padding: 6px 10px; border-bottom: 1px solid #eee; color: #555; } #myai-chat-messages { flex: 1; overflow-y: auto; padding: 10px; font-size: 14px; display: flex; flex-direction: column; position: relative; } .myai-msg { margin-bottom: 10px; padding: 8px; border-radius: 6px; max-width: 85%; word-break: break-word; color: black!important; } .myai-msg.user { background: #DCF8C6; align-self: flex-end; } .myai-msg.bot { background: #EEE; align-self: flex-start; } #myai-chat-input { display: flex; border-top: 1px solid #ddd; } #myai-chat-input input { flex: 1; border: none; padding: 8px; font-size: 14px; } #myai-chat-input button { background: #${currentcol}; border: none; color: white; padding: 0 12px; cursor: pointer; } `; document.head.appendChild(style); // ====== HTML ELEMENTS ====== const button = document.createElement("div"); button.id = "myai-chat-button"; button.innerHTML = ' ';//"💬"; const popup = document.createElement("div"); popup.id = "myai-chat-popup"; popup.innerHTML = `
MyAI Expert
`; document.body.appendChild(button); document.body.appendChild(popup); const headerEl = popup.querySelector("#myai-chat-header"); const descEl = popup.querySelector("#myai-chat-description"); const messagesEl = popup.querySelector("#myai-chat-messages"); const inputEl = popup.querySelector("input"); const sendBtn = popup.querySelector("button"); const loadingicon = popup.querySelector("#loadingicon"); let infoLoaded = false; // ====== EVENTS ====== button.addEventListener("click", () => { if (!infoLoaded) { loadAgentInfo(); } popup.style.display = popup.style.display === "flex" ? "none" : "flex"; }); sendBtn.addEventListener("click", sendMessage); inputEl.addEventListener("keypress", e => { if (e.key === "Enter") sendMessage(); }); function addMessage(text, sender,iserror='no') { const msg = document.createElement("div"); msg.className = `myai-msg ${sender}`; if (iserror=='rederror') { msg.style.color='darkred'; } msg.textContent = text; messagesEl.appendChild(msg); messagesEl.scrollTop = messagesEl.scrollHeight; } async function loadAgentInfo() { try { let options = { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer " + AGENT_ID }, body: JSON.stringify({ action: "getinfo", sessionid: sessionId }) }; options.headers['Authorization'] = 'Bearer ' + AGENT_ID; console.log(options); const res = await fetch(API_URL, options); const data = await res.json(); if (data.agent_name) headerEl.textContent = data.agent_name; if (data.agent_description) descEl.textContent = data.agent_description; messagesEl.innerHTML = ""; if (data.chat_history && data.chat_history.length > 0) { data.chat_history.forEach(m => { addMessage(m.text, m.sender); // m.sender should be "user" or "bot" }); } else if (data.welcome_message) { addMessage(data.welcome_message, "bot"); } infoLoaded = true; } catch (err) { console.error("Error loading agent info:", err); addMessage("[Error loading agent info]", "bot"); } } async function sendMessage() { const text = inputEl.value.trim(); if (!text) return; addMessage(text, "user"); loadingicon.style.display='block'; inputEl.value = ""; let options = { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sessionid: sessionId, message: text }) }; options.headers['Authorization'] = 'Bearer ' + AGENT_ID; try { const res = await fetch(API_URL, options); const data = await res.json(); if (data.reply_error=='yes') { addMessage(data.reply || "[No reply]", "bot","rederror"); } else { addMessage(data.reply || "[No reply]", "bot"); } loadingicon.style.display='none'; } catch (err) { addMessage("[Error contacting server]", "bot"); } } })();