KOS COMPANY • Chat
KOS COMPANY
Ciao, cosa posso fare per te?
Hai intenzione di sviluppare un Sample, uno Stock o sei interessato ai nostri servizi di Marketing e Branding?
`; kosMsgs.appendChild(holder); kosMsgs.scrollTop = kosMsgs.scrollHeight; } function hideTypingKos(){ const el=document.getElementById("kosTyping"); if(el) el.remove(); } /* ===== iOS keyboard fix: usa visualViewport per dimensionare il modal ===== */ (function mountIOSKeyboardFix(){ if (!window.visualViewport) return; const inputWrap = document.querySelector('.kos-inputwrap'); function applyModalHeight(){ const vh = window.visualViewport.height; const margin = 16; kosModal.style.maxHeight = Math.max(360, Math.floor(vh - margin)) + 'px'; const keyboardLikelyOpen = vh { if (window.__applyKosModalHeight) window.__applyKosModalHeight(); kosInput.focus(); },50); } function closeModal(){ kosMask.style.display="none"; kosModal.style.display="none"; } /* ===== Send (frontend -> apre popup) ===== */ function sendFromFrontend(){ const txt = input.value.trim(); if(!txt) return; ensureChatId(); input.value=""; HISTORY.push({role:'user', text:txt}); openModal(); renderKosModal(); showTypingKos(); fetch(webhookUrl,{ method:"POST", headers:{ "Content-Type":"application/json" }, body: JSON.stringify({ question: txt, chat_id: CHAT_ID }) }) .then(r=>r.json()) .then(data=>{ hideTypingKos(); const payload = Array.isArray(data) ? (data[0]||{}) : (data||{}); const reply = payload.reply || {}; const raw = reply.details || payload.data || reply.text || ""; const html = /{ hideTypingKos(); HISTORY.push({role:'bot', html:"

⚠️ Errore di connessione.

"}); renderKosModal(); }); } /* ===== Send (dal popup) ===== */ function sendFromPopup(){ const txt = kosInput.value.trim(); if(!txt) return; ensureChatId(); kosInput.value=""; HISTORY.push({role:'user', text:txt}); renderKosModal(); showTypingKos(); fetch(webhookUrl,{ method:"POST", headers:{ "Content-Type":"application/json" }, body: JSON.stringify({ question: txt, chat_id: CHAT_ID }) }) .then(r=>r.json()) .then(data=>{ hideTypingKos(); const payload = Array.isArray(data) ? (data[0]||{}) : (data||{}); const reply = payload.reply || {}; const raw = reply.details || payload.data || reply.text || ""; const html = /{ hideTypingKos(); HISTORY.push({role:'bot', html:"

⚠️ Errore di connessione.

"}); renderKosModal(); }); } /* ===== Events ===== */ openFullChatIcon.addEventListener("click", openModal); document.getElementById("kosClose").addEventListener("click", closeModal); miniSendBtn.addEventListener("click", sendFromFrontend); input.addEventListener("keypress", e=>{ if(e.key==="Enter") sendFromFrontend(); }); kosSend.addEventListener("click", sendFromPopup); kosInput.addEventListener("keypress", e=>{ if(e.key==="Enter") sendFromPopup(); }); /* ===== CESTINO: cancella subito senza popup nero ===== */ kosTrash.addEventListener("click", ()=>{ HISTORY = []; renderKosModal(); }); // aggancio bottoni rapidi document.addEventListener("click", function(e){ if(e.target.classList.contains("quick-btn")){ const msg = e.target.dataset.text; const kosInput = document.getElementById("kosInput"); kosInput.value = msg; // inserisce il testo preimpostato nella barra input kosInput.focus(); } });