diff --git a/public-api.js b/public-api.js index d1d585b..230b1d0 100644 --- a/public-api.js +++ b/public-api.js @@ -1215,7 +1215,7 @@ ${dialog}`; l.summary_zh ? `> ${l.summary_zh}` : '', [l.name, l.company].filter(Boolean).length ? `> ${[l.name, l.company].filter(Boolean).join(' / ')}` : '', `> 状态:${conv.mode === 'human' ? '你在接待' : 'AI 正在接待'}`, - panel ? `[打开对话](${panel}/?chat=${conv.sid})` : '', + panel ? `[打开对话](${panel}/?chat=${conv.sid}&k=${linkKey(conv.sid)})` : '', ].filter(Boolean).join('\n'); try { const r = await fetch(st.wecomWebhook, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ msgtype: 'markdown', markdown: { content } }) }); @@ -1241,25 +1241,45 @@ ${dialog}`; const list = Object.values(store.convs).filter((c) => c.msgs.some((m) => m.from === 'user')).sort((a, b) => b.updated - a.updated).slice(0, 300).map(summary); res.json({ ver, list }); }); - app.get(`${A}/conv/:sid`, requireAuth, (req, res) => { + app.get(`${A}/conv/:sid`, authConv, (req, res) => { const c = store.convs[req.params.sid]; if (!c) return res.status(404).json({ error: 'not found' }); if (c.unread) { c.unread = 0; bump(c); } const { token, ...rest } = c; res.json({ ver, conv: rest }); }); + // 推送里的链接带一个只对这个对话有效的密钥 k:手机点开不用登录,只能看和回复这一个对话 + function linkKey(sid) { + if (!store.settings.linkSecret) { store.settings.linkSecret = crypto.randomBytes(24).toString('hex'); save(); } + return crypto.createHmac('sha256', store.settings.linkSecret).update(String(sid)).digest('hex').slice(0, 32); + } + function authConv(req, res, next) { + if (req.session && req.session.authed) return next(); + const k = String((req.query && req.query.k) || (req.body && req.body.k) || ''); + if (k && req.params.sid && store.convs[req.params.sid] && crypto.timingSafeEqual(Buffer.from(k.padEnd(32).slice(0, 32)), Buffer.from(linkKey(req.params.sid)))) return next(); + res.status(401).json({ error: 'not authenticated' }); + } + app.get(`${A}/conv/:sid/wait`, authConv, (req, res) => { + const c = store.convs[req.params.sid], since = Number(req.query.ver) || 0; + if ((c.ver || 0) > since) return res.json({ ver: c.ver }); + const set = convWaiters.get(c.sid) || new Set(); convWaiters.set(c.sid, set); + const fn = () => { clearTimeout(t); if (!res.headersSent) res.json({ ver: c.ver || 0 }); }; + const t = setTimeout(() => { set.delete(fn); if (!res.headersSent) res.json({ ver: c.ver || 0 }); }, 25000); + set.add(fn); + req.on('close', () => { clearTimeout(t); set.delete(fn); }); + }); const convOr404 = (req, res) => { const c = store.convs[req.params.sid]; if (!c) res.status(404).json({ error: 'not found' }); return c; }; function join(c) { if (c.mode !== 'human') { c.mode = 'human'; c.typing = false; if (pending.has(c.sid)) { clearTimeout(pending.get(c.sid)); pending.delete(c.sid); } } if (!c.joined) { c.joined = true; const st = S(); addMsg(c, { from: 'sys', text: (JOINED[c.lang] || JOINED.en)(st.managerName, st.managerTitle) }); } bump(c); } - app.post(`${A}/:sid/takeover`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; join(c); res.json({ ok: true }); }); - app.post(`${A}/:sid/release`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; c.mode = 'ai'; bump(c); res.json({ ok: true }); }); - app.post(`${A}/:sid/close`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; c.status = 'closed'; c.typing = false; bump(c); res.json({ ok: true }); }); + app.post(`${A}/:sid/takeover`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; join(c); res.json({ ok: true }); }); + app.post(`${A}/:sid/release`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; c.mode = 'ai'; bump(c); res.json({ ok: true }); }); + app.post(`${A}/:sid/close`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; c.status = 'closed'; c.typing = false; bump(c); res.json({ ok: true }); }); app.post(`${A}/:sid/delete`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; delete store.convs[c.sid]; bump(null); res.json({ ok: true }); }); - app.post(`${A}/:sid/lead`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; c.lead = { ...c.lead, ...(req.body || {}), manual: true }; bump(c); res.json({ ok: true }); }); - app.post(`${A}/:sid/typing`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; if (c.mode === 'human') { c.typing = !!(req.body || {}).on; bump(c); } res.json({ ok: true }); }); + app.post(`${A}/:sid/lead`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; c.lead = { ...c.lead, ...(req.body || {}), manual: true }; bump(c); res.json({ ok: true }); }); + app.post(`${A}/:sid/typing`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; if (c.mode === 'human') { c.typing = !!(req.body || {}).on; bump(c); } res.json({ ok: true }); }); // 你发消息:中文自动翻译成客户语言(可关闭);发了就算接管 async function toCustomerLang(c, text) { const target = c.custLang && c.custLang !== 'zh' ? c.custLang : c.lang; @@ -1269,11 +1289,11 @@ ${dialog}`; const out = await ai.callDeepSeekWithRetry(cfg.apiKey, [{ role: 'user', content: `Translate this message from a sales manager to a customer into ${LANG_NAME[target] || target}. Keep it natural, friendly and professional; keep model names, numbers and links unchanged. Output only the translation.\n\n${text}` }], { maxTokens: 800, temperature: 0.2, timeoutMs: 60000 }); return String(out || '').trim(); } - app.post(`${A}/:sid/translate`, requireAuth, async (req, res) => { + app.post(`${A}/:sid/translate`, authConv, async (req, res) => { const c = convOr404(req, res); if (!c) return; try { res.json({ text: await toCustomerLang(c, String((req.body || {}).text || '')) }); } catch (e) { res.status(500).json({ error: e.message }); } }); - app.post(`${A}/:sid/send`, requireAuth, async (req, res) => { + app.post(`${A}/:sid/send`, authConv, async (req, res) => { const c = convOr404(req, res); if (!c) return; const b = req.body || {}; const raw = String(b.text || '').trim().slice(0, 3000); @@ -1286,7 +1306,7 @@ ${dialog}`; res.json({ ok: true, id: m.id, text }); } catch (e) { res.status(500).json({ error: e.message }); } }); - app.post(`${A}/:sid/note`, requireAuth, (req, res) => { + app.post(`${A}/:sid/note`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; const text = String((req.body || {}).text || '').trim().slice(0, 1000); if (!text) return res.status(400).json({ error: 'empty' }); @@ -1294,7 +1314,7 @@ ${dialog}`; res.json({ ok: true }); }); // 让 AI 按悄悄话马上再回一条 - app.post(`${A}/:sid/nudge`, requireAuth, (req, res) => { const c = convOr404(req, res); if (!c) return; c.mode = 'ai'; bump(c); aiReply(c).catch(() => {}); res.json({ ok: true }); }); + app.post(`${A}/:sid/nudge`, authConv, (req, res) => { const c = convOr404(req, res); if (!c) return; c.mode = 'ai'; bump(c); aiReply(c).catch(() => {}); res.json({ ok: true }); }); // 知识库 / 规则 / 范例 / 待解答 / 设置 app.get(`${A}/knowledge`, requireAuth, (req, res) => res.json({ kb: store.kb || [], rules: store.rules || [], examples: store.examples || [], unanswered: store.unanswered || [] }));