diff --git a/supportbot.js b/supportbot.js
index e5369fe..72f0033 100644
--- a/supportbot.js
+++ b/supportbot.js
@@ -28,6 +28,13 @@ const adminUrl = ref => (deps.site || 'https://instantadpay.com') + '/admin#supp
const keyboard = ref => ({ inline_keyboard: [
[{ text: 'Close', callback_data: 'tk|' + ref + '|closed' }, { text: 'Waiting', callback_data: 'tk|' + ref + '|waiting_on_member' }, { text: 'High', callback_data: 'tk|' + ref + '|high' }],
[{ text: 'Open in admin', url: adminUrl(ref) }]] });
+// the persistent button keyboard under the chat (the "menu" Marty works from) + the command list behind the Menu button
+const MENU = { keyboard: [[{ text: '๐ Open tickets' }, { text: 'โณ Waiting on member' }], [{ text: '๐ฅ High priority' }, { text: 'โ
Recently closed' }], [{ text: 'โ Help' }]],
+ resize_keyboard: true, is_persistent: true };
+const COMMANDS = [{ command: 'tickets', description: 'Open tickets' }, { command: 'waiting', description: 'Waiting on member' },
+ { command: 'high', description: 'High priority' }, { command: 'closed', description: 'Recently closed' },
+ { command: 't', description: 'Show a ticket: /t IAP-1042' }, { command: 'menu', description: 'Show the button menu' }, { command: 'help', description: 'How the desk works' }];
+const HELP = 'Tickets land here as they arrive.\n\nโข Reply to a ticket message to answer it (the member gets it by email, and in the dashboard).\nโข Start the reply with /note to keep it internal.\nโข Buttons under a ticket: Close, Waiting, High, Open in admin.\nโข The menu below lists tickets; tap one to open its thread.';
const say = (text, extra) => api('sendMessage', Object.assign({ chat_id: deps.chatId(), text, parse_mode: 'HTML', disable_web_page_preview: true }, extra || {}));
// the ticket ping: same text the desk builds, plus buttons and a hint; true when Telegram accepted it
@@ -40,13 +47,21 @@ async function ping(text, ref) {
const REF_RE = /\b((?:IAP|LS)-\d{4,9})\b/;
const STATUS_WORD = { open: 'open', waiting_on_member: 'waiting on member', closed: 'closed' };
-async function listOpen() {
- const out = [];
- for (const st of ['open', 'waiting_on_member']) {
- const r = await deps.tickets.adminList({ status: st });
- for (const t of (r.tickets || []).slice(0, 15)) out.push('' + t.ref + ' ยท ' + STATUS_WORD[t.status] + (t.priority === 'high' ? ' ยท high' : '') + '\n' + escH(t.subject));
- }
- return out.length ? out.join('\n\n') : 'No open tickets.';
+async function fetchList(which) {
+ const get = async st => ((await deps.tickets.adminList({ status: st })).tickets || []);
+ if (which === 'waiting') return { title: 'โณ Waiting on member', rows: await get('waiting_on_member') };
+ if (which === 'closed') return { title: 'โ
Recently closed', rows: (await get('closed')).slice(0, 10) };
+ const live = (await get('open')).concat(await get('waiting_on_member'));
+ if (which === 'high') return { title: '๐ฅ High priority', rows: live.filter(t => t.priority === 'high') };
+ return { title: '๐ Open tickets', rows: await get('open') };
+}
+// a list = one tappable button per ticket; tapping opens the thread (callback tv|[)
+async function sendList(which) {
+ const { title, rows } = await fetchList(which);
+ if (!rows.length) return say(title + ': none right now.', { reply_markup: MENU });
+ const shown = rows.slice(0, 20);
+ const buttons = shown.map(t => [{ text: (t.priority === 'high' ? '๐ฅ ' : '') + t.ref + ' ยท ' + String(t.subject).slice(0, 40), callback_data: 'tv|' + t.ref }]);
+ return say('' + title + ' (' + rows.length + ')' + (rows.length > shown.length ? ', newest 20 shown' : '') + '\nTap one to open it.', { reply_markup: { inline_keyboard: buttons } });
}
async function showThread(ref) {
const t = await deps.tickets.byRef(ref); if (!t) return say('No ticket ' + escH(ref) + '.');
@@ -63,6 +78,7 @@ async function handle(req, update) {
if (cq) {
if (String(cq.from && cq.from.id) !== me) return 'ignored';
const [kind, ref, action] = String(cq.data || '').split('|');
+ if (kind === 'tv' && REF_RE.test(ref || '')) { await api('answerCallbackQuery', { callback_query_id: cq.id }); await showThread(ref); return 'view'; }
let note = 'Done';
if (kind === 'tk' && REF_RE.test(ref || '')) {
const t = await deps.tickets.byRef(ref);
@@ -78,13 +94,17 @@ async function handle(req, update) {
const msg = update.message;
if (!msg || String(msg.chat && msg.chat.id) !== me || String(msg.from && msg.from.id) !== me) return 'ignored';
const text = String(msg.text || '').trim(); if (!text) return 'empty';
- if (/^\/start\b/.test(text)) return say('Support desk connected. New tickets and member replies land here.\nReply to a ticket message to answer it, start with /note for an internal note.\n/tickets lists what is open, /t IAP-1042 shows a thread.');
- if (/^\/tickets\b/.test(text)) return say(await listOpen());
+ if (/^\/(start|menu)\b/.test(text)) return say('Support desk connected. Use the buttons below, or reply to any ticket message to answer it.', { reply_markup: MENU });
+ if (/^\/help\b/.test(text) || text === 'โ Help') return say(HELP, { reply_markup: MENU });
+ if (/^\/tickets\b/.test(text) || text === '๐ Open tickets') return sendList('open');
+ if (/^\/waiting\b/.test(text) || text === 'โณ Waiting on member') return sendList('waiting');
+ if (/^\/high\b/.test(text) || text === '๐ฅ High priority') return sendList('high');
+ if (/^\/closed\b/.test(text) || text === 'โ
Recently closed') return sendList('closed');
const tm = /^\/t\s+((?:IAP|LS)-\d{4,9})/i.exec(text);
if (tm) return showThread(tm[1].toUpperCase());
const replied = msg.reply_to_message && (msg.reply_to_message.text || msg.reply_to_message.caption || '');
const rm = replied && REF_RE.exec(replied);
- if (!rm) return say('Reply to a ticket message to answer it, or use /tickets.');
+ if (!rm) return say('To answer a ticket, reply to its message. Or pick a list below.', { reply_markup: MENU });
const t = await deps.tickets.byRef(rm[1]); if (!t) return say('No ticket ' + escH(rm[1]) + '.');
const internal = /^\/note\b/i.test(text);
const body = internal ? text.replace(/^\/note\b\s*/i, '') : text;
@@ -97,6 +117,8 @@ async function handle(req, update) {
// point Telegram at our webhook (run once after deploy, and again if the URL or secret changes)
async function register(url) {
if (!enabled() || !secret()) return { ok: false, error: 'SUPPORT_BOT_TOKEN and SUPPORT_TG_SECRET must both be set' };
- return api('setWebhook', { url, secret_token: secret(), allowed_updates: ['message', 'callback_query'], drop_pending_updates: true });
+ const hook = await api('setWebhook', { url, secret_token: secret(), allowed_updates: ['message', 'callback_query'], drop_pending_updates: true });
+ const cmds = await api('setMyCommands', { commands: COMMANDS, scope: { type: 'chat', chat_id: Number(deps.chatId()) } });
+ return { ok: !!(hook.ok && cmds.ok), webhook: hook.description || hook.ok, commands: !!cmds.ok };
}
module.exports = { init, enabled, ping, handle, register };
]