beistelltisch_noa_schwarz_nero_marquina_transparent
Skip to content
beistelltisch_noa_schwarz_nero_marquina_transparentapollon2018-08-15T21:26:06+02:00
Page load link
';
document.body.appendChild(box);
var input = document.getElementById('mz-chat-input');
var sendBtn = document.getElementById('mz-chat-send');
var msgsDiv = document.getElementById('mz-chat-msgs');
var closeBtn = document.getElementById('mz-chat-close');
closeBtn.onclick = function() { box.classList.remove('open'); isOpen = false; };
sendBtn.onclick = sendMessage;
input.onkeydown = function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } };
function toggleChat() {
isOpen = !isOpen;
if (isOpen) { box.classList.add('open'); if (messages.length === 0) addBotMessage('Welcome to MAZANLI. How can I help you?'); input.focus(); }
else { box.classList.remove('open'); }
}
function addBotMessage(text) {
messages.push({ role: 'bot', text: text });
var div = document.createElement('div'); div.className = 'mz-msg mz-msg-bot';
div.innerHTML = '' + escapeHtml(text) + '
';
msgsDiv.appendChild(div); msgsDiv.scrollTop = msgsDiv.scrollHeight;
}
function addUserMessage(text) {
messages.push({ role: 'user', text: text });
var div = document.createElement('div'); div.className = 'mz-msg mz-msg-user';
div.innerHTML = '' + escapeHtml(text) + '
';
msgsDiv.appendChild(div); msgsDiv.scrollTop = msgsDiv.scrollHeight;
}
function showTyping() {
var div = document.createElement('div'); div.className = 'mz-msg mz-msg-bot'; div.id = 'mz-typing';
div.innerHTML = '
';
msgsDiv.appendChild(div); msgsDiv.scrollTop = msgsDiv.scrollHeight;
}
function hideTyping() { var el = document.getElementById('mz-typing'); if (el) el.remove(); }
function showWeiterleitung() {
if (document.querySelector('.mz-weiterleitung')) return;
var wrap = document.getElementById('mz-chat-input-wrap');
var div = document.createElement('div'); div.className = 'mz-weiterleitung';
div.innerHTML = '';
wrap.parentNode.insertBefore(div, wrap);
}
window._mzWeiterleiten = function() {
var name = document.getElementById('mz-wl-name').value;
var email = document.getElementById('mz-wl-email').value;
var msg = document.getElementById('mz-wl-msg').value;
if (!email) { alert('Please provide email'); return; }
fetch(API + '/weiterleiten', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ conversation_id: convId, name: name, email: email, nachricht: msg }) })
.then(function(r) { return r.json(); })
.then(function(d) { var el = document.querySelector('.mz-weiterleitung'); if (el) el.remove(); addBotMessage(d.message || 'Message has been forwarded. We will contact you.'); })
.catch(function() { addBotMessage('Forwarding failed. Please contact us directly at info@mazan.li'); });
};
function sendMessage() {
var text = input.value.trim(); if (!text) return;
input.value = ''; addUserMessage(text); sendBtn.disabled = true; showTyping();
fetch(API + '/message', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: text, conversation_id: convId }) })
.then(function(r) { return r.json(); })
.then(function(d) {
hideTyping(); sendBtn.disabled = false;
if (d.error) { addBotMessage('Sorry, an error has occurred.'); return; }
convId = d.conversation_id; addBotMessage(d.antwort);
if (d.antwort.toLowerCase().indexOf('forward') > -1 || d.antwort.toLowerCase().indexOf('team') > -1 || d.antwort.toLowerCase().indexOf('info@mazan.li') > -1) { showWeiterleitung(); }
input.focus();
})
.catch(function() { hideTyping(); sendBtn.disabled = false; addBotMessage('Connection failed. Please try again.'); });
}
function escapeHtml(str) { return str.replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"').replace(/\n/g,'
'); }
})();