9be58bd5ef
- tickets.js: one shared desk (platform iap now, linkspin later), dual-mode store (MySQL tables tickets + ticket_messages, else support.json); refs IAP-<1000+id> / LS-; private no-login link /support/t/<ref>.<HMAC> signed with DATA_DIR/support.key (or SUPPORT_SECRET); ingestEmail() ready for the support@ intake step (threads by Message-ID, then [ref] in the subject from the same sender) - Member Help pane (/my#help): list with status + unread badge, new ticket (category, subject, message, up to 2 screenshots via /api/my/upload), thread + reply (a reply reopens a closed ticket) - Public /support: name, email, category, subject, message behind the sign-up walls (honeypot, form age, per-IP limits with the icon check, global cap) plus a per-email cap; confirmation email with the ref and the private link - Chatbot: CANNED + systemPrompt know the help desk; answers that cannot help (or "talk to a human") offer "Send this to support", which files the last turns as a ticket (members: Help pane; signed out: email + the same guard) - Admin Support pane: open / waiting / closed / all, platform, category, search; open-count badge; ticket view with requester account info, internal notes, status/priority/category, saved replies (site setting supportTemplates); deep link /admin#support?t=<ref> - Notifications: new ticket / member reply -> Telegram to Marty's private chat only (supportTelegramChat, default 1289244227, site bot then Hermes fallback, never a group or the Discord relay) + email to ADMIN_EMAIL, max one per ticket per 10 min; Marty's reply -> email "[IAP-1042] subject" + a 'notice' inbox message for members, bursts batched - support* settings are kept out of /api/config; Support link in the site footer, home footer, member page foot, terms and privacy; "Contact support" errors point at /support - QA walk: member Help ticket flow, admin Support pane + deep link, internal-note leak check, public /support + private link + tampered token + form-age refusal - Cache tags: common.js 20261016a, chat.js 20261016a, my.js 20261016r, admin.js 20261016sp, support.js 20261016a Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
6.8 KiB
JavaScript
120 lines
6.8 KiB
JavaScript
// 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.</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 => ({ '&': '&', '<': '<', '>': '>' }[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(); });
|
||
})();
|