Files
instantadpay/public/assets/chat.js
T

120 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 (members: their Help pane; signed out: their email).
(function () {
const root = document.createElement('div');
root.id = 'iapChat';
root.innerHTML = '<button id="iapChatBtn" aria-label="Chat with us" type="button">💬</button>'
+ '<div id="iapChatPanel" hidden>'
+ '<div class="ch-head"><b>Ask anything</b><span class="ch-sub">Real answers, around the clock</span>'
+ '<button id="iapChatClose" aria-label="Close chat" type="button">×</button></div>'
+ '<div class="ch-msgs" id="iapChatMsgs">'
+ '<div class="ch-m bot">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 <b>Send this to support</b> below, or use the <a href="/support" style="color:inherit;text-decoration:underline">support page</a>.</div>'
+ '</div>'
+ '<div class="ch-input"><input id="iapChatIn" placeholder="Type your question…" maxlength="600">'
+ '<button id="iapChatSend" type="button">Send</button></div>'
+ '<div style="text-align:center;font-size:12px;padding:4px 0 8px"><a href="#" id="iapChatHuman" style="color:inherit;opacity:.75">Need a person? Send this to support</a></div>'
+ '</div>';
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 => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' }[c]))
.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_blank" rel="noopener">$1</a>');
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 = '<button type="button" class="btn small" style="margin:0">Send this to support</button>';
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, fage: g ? Date.now() - 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 dashboard 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 dashboard 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 the Help tab in your dashboard.', '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?'
+ '<input class="hp-field" type="text" tabindex="-1" aria-hidden="true" autocomplete="off" id="iapChatHp">'
+ '<input id="iapChatEmail" type="email" placeholder="your email" maxlength="190" autocomplete="email" style="width:100%;margin-top:6px;box-sizing:border-box">'
+ '<input id="iapChatNote" placeholder="anything to add (optional)" maxlength="600" style="width:100%;margin-top:6px;box-sizing:border-box">'
+ '<div id="iapChatCheck" hidden style="margin-top:6px"></div>'
+ '<button type="button" class="btn small" id="iapChatGo" style="margin-top:8px">Send to support</button> <span class="small" id="iapChatErr"></span>';
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(); });
})();