// 24/7 assistant widget: floating bubble, slide-up panel, /api/chat.
// Hand-off (help desk, 2026-10-16): when the answer cannot help, or they ask for a person, the widget offers
// "Send this to support", which files the last few turns as a ticket on the shared desk (members: their Help
// screen; signed out: their email, behind the same invisible walls as sign-up).
(function () {
const root = document.createElement('div');
root.id = 'iapChat';
root.innerHTML = 'đź’¬ '
+ '
'
+ '
Ask anything Real answers, around the clock '
+ 'Ă—
'
+ '
'
+ '
Hey. Ask me how the payments work, what the packages buy, or anything else. Straight answers only, no income hype. Need a person instead? Tap
Send this to support below, open
Help , or use the
support page .
'
+ '
'
+ '
'
+ 'Send
'
+ '
'
+ '
';
document.body.appendChild(root);
const $ = id => document.getElementById(id);
const msgs = $('iapChatMsgs');
const input = $('iapChatIn');
let history = [];
const turns = []; // { who: 'me' | 'bot', text } for the support hand-off
const add = (text, who) => {
const d = document.createElement('div');
d.className = 'ch-m ' + who;
// linkify plain URLs
d.innerHTML = String(text).replace(/[&<>]/g, c => ({ '&': '&', '<': '<', '>': '>' }[c]))
.replace(/(https?:\/\/[^\s]+)/g, '$1 ');
msgs.appendChild(d);
msgs.scrollTop = msgs.scrollHeight;
return d;
};
function offerHandoff() {
if (msgs.querySelector('.ch-handoff')) return;
const d = document.createElement('div'); d.className = 'ch-m bot ch-handoff';
d.innerHTML = 'Send this to support ';
d.querySelector('button').addEventListener('click', handoff);
msgs.appendChild(d); msgs.scrollTop = msgs.scrollHeight;
}
async function send() {
const q = input.value.trim();
if (!q) return;
input.value = '';
add(q, 'me'); turns.push({ who: 'me', text: q });
const wait = add('…', 'bot');
try {
const r = await (await fetch('/api/chat', { method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: q, history: history.slice(-4).join(' | ') }) })).json();
wait.remove();
add(r.reply || r.error || 'No answer came back. Try again.', 'bot');
if (r.reply) turns.push({ who: 'bot', text: r.reply });
history.push('Q: ' + q, 'A: ' + (r.reply || ''));
if (r.handoff) offerHandoff();
} catch (e) {
wait.remove();
add('Connection hiccup. Try that again.', 'bot');
offerHandoff();
}
}
async function post(body) {
const g = window.IAP && IAP.guard;
if (g) await g.formSettled();
return (await fetch('/api/support/chatbot', { method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(Object.assign({ transcript: turns.slice(-8), fts: g ? g.FORM_TS : 0 }, body)) })).json();
}
function done(r) {
const old = msgs.querySelector('.ch-handoff'); if (old) old.remove();
const f = msgs.querySelector('.ch-hform'); if (f) f.remove();
add(r.member ? 'Sent to support as ticket ' + r.ref + '. Marty will reply in Help in your Control Room and by email: ' + location.origin + r.link
: 'Sent to support as ticket ' + r.ref + '. We emailed you a private link to follow it, and Marty will reply there: ' + location.origin + r.link, 'bot');
}
async function handoff(e) {
if (e && e.preventDefault) e.preventDefault();
if (msgs.querySelector('.ch-hform')) return;
let me = null; try { me = await (await fetch('/api/me')).json(); } catch (err) {}
if (me && me.signedIn && me.email) {
const ok = window.IAP && IAP.confirmBox ? await IAP.confirmBox('Send this chat to support as a ticket? Marty will reply in Help in your Control Room and by email.', { ok: 'Send it', cancel: 'Not now' }) : true;
if (!ok) return;
try { const r = await post({}); if (r.error) throw new Error(r.error); done(r); } catch (err) { add(err.message || 'Could not send it. Try Help in your Control Room.', 'bot'); }
return;
}
// signed out: an email so Marty can reply, plus the same invisible walls as sign-up
const d = document.createElement('div'); d.className = 'ch-m bot ch-hform';
d.innerHTML = 'Where should Marty reply?'
+ ' '
+ ' '
+ ' '
+ '
'
+ 'Send to support ';
msgs.appendChild(d); msgs.scrollTop = msgs.scrollHeight;
$('iapChatEmail').focus();
$('iapChatGo').addEventListener('click', async () => {
const btn = $('iapChatGo'); if (btn.disabled) return; btn.disabled = true; $('iapChatErr').textContent = '';
try {
let pick = null, r = null;
for (let i = 0; i < 4; i++) {
r = await post({ email: $('iapChatEmail').value.trim(), note: $('iapChatNote').value.trim(), hp_field_x9: window.IAP && IAP.guard ? IAP.guard.hpValue($('iapChatHp')) : '', pick });
if (r.challenge && window.IAP && IAP.guard) { pick = await IAP.guard.iconCheck($('iapChatCheck'), r.challenge, r.error); continue; }
break;
}
if (!r || r.error) throw new Error((r && r.error) || 'Could not send it.');
done(r);
} catch (err) { $('iapChatErr').textContent = err.message; btn.disabled = false; }
});
}
$('iapChatBtn').addEventListener('click', () => {
const p = $('iapChatPanel');
p.hidden = !p.hidden;
if (!p.hidden) input.focus();
});
$('iapChatClose').addEventListener('click', () => { $('iapChatPanel').hidden = true; });
$('iapChatSend').addEventListener('click', send);
$('iapChatHuman').addEventListener('click', handoff);
input.addEventListener('keydown', e => { if (e.key === 'Enter') send(); });
})();