172 lines
9.6 KiB
JavaScript
172 lines
9.6 KiB
JavaScript
// 合并转发读回的针对性补测:修正此前 8 组参数全部传「内层消息 id」的探测缺陷。
|
||||
|
|
//
|
|||
|
|
// 背景:fwd-record-followup.mjs 的 get_forward_msg 全部用 message_id=497853103,
|
|||
|
|
// 那是被转发条目的 id;NapCat 的错误原文恰好是「消息已过期或者为内层消息」,
|
|||
|
|
// 等于自己承认了传错对象。自己发出的那条转发消息的外层 id 从未被试过。
|
|||
|
|
//
|
|||
|
|
// 本脚本只读:先回读 history 找出所有 com.tencent.multimsg 外层消息,
|
|||
|
|
// 再用外层 message_id / resid / uniseq 的多种组合调 get_forward_msg,
|
|||
|
|
// 判定被转发条目到底能否还原、以及能从哪一路还原。不发送任何消息。
|
|||
|
|
//
|
|||
|
|
// 凭据只从环境变量读取。
|
|||
|
|
|
|||
|
|
import fs from 'node:fs';
|
|||
|
|
import path from 'node:path';
|
|||
|
|
|
|||
|
|
const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099';
|
|||
|
|
const TOKEN = process.env.NAPCAT_WS_TOKEN;
|
|||
|
|
const GROUP = Number(process.env.PROBE_GROUP || 933280401);
|
|||
|
|
const FRIEND = Number(process.env.PROBE_FRIEND || 2161656029);
|
|||
|
|
const OUT_DIR = process.env.PROBE_OUT || 'local';
|
|||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
|
|||
|
|
|
|||
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|||
|
|
fs.mkdirSync(OUT_DIR, { recursive: true });
|
|||
|
|
const OUT = path.join(OUT_DIR, `forward-readback-${stamp}.jsonl`);
|
|||
|
|
const say = (...a) => console.log(...a);
|
|||
|
|
const rec = (o) => fs.appendFileSync(OUT, JSON.stringify(o) + '\n');
|
|||
|
|
|
|||
|
|
const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
|
|||
|
|
const pending = new Map();
|
|||
|
|
let opened = false, authed = false;
|
|||
|
|
ws.onopen = () => { opened = true; };
|
|||
|
|
ws.onmessage = (ev) => {
|
|||
|
|
let o; try { o = JSON.parse(String(ev.data)); } catch { return; }
|
|||
|
|
if (o.echo && pending.has(o.echo)) { pending.get(o.echo).resolve(o); pending.delete(o.echo); return; }
|
|||
|
|
if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') authed = true;
|
|||
|
|
};
|
|||
|
|
for (let i = 0; i < 100 && !opened; i++) await new Promise((r) => setTimeout(r, 100));
|
|||
|
|
if (!opened) { console.error('连接超时'); process.exit(1); }
|
|||
|
|
for (let i = 0; i < 100 && !authed; i++) await new Promise((r) => setTimeout(r, 100));
|
|||
|
|
say('认证=' + authed);
|
|||
|
|
|
|||
|
|
const api = (action, params, ms = 40000) => new Promise((resolve) => {
|
|||
|
|
const echo = `fr-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
|
|||
|
|
const to = setTimeout(() => { pending.delete(echo); resolve({ __timeout: true }); }, ms);
|
|||
|
|
pending.set(echo, { resolve: (o) => { clearTimeout(to); resolve(o); }, to });
|
|||
|
|
ws.send(JSON.stringify({ action, params, echo }));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 1) 回读群 + 私聊 history,挑出所有 multimsg 外层消息,抽 resid/uniseq/tsum
|
|||
|
|
const multimsg = [];
|
|||
|
|
const collect = (msgs, scope) => {
|
|||
|
|
for (const m of msgs ?? []) {
|
|||
|
|
for (const s of m.message ?? []) {
|
|||
|
|
if (s.type !== 'json') continue;
|
|||
|
|
let d; try { d = JSON.parse(s.data.data); } catch { continue; }
|
|||
|
|
if (d.app !== 'com.tencent.multimsg') continue;
|
|||
|
|
const det = d.meta?.detail ?? {};
|
|||
|
|
multimsg.push({
|
|||
|
|
scope, seq: m.real_seq, message_id: m.message_id,
|
|||
|
|
resid: det.resid ?? null, uniseq: det.uniseq ?? null,
|
|||
|
|
filename: d.extra?.filename ?? null, tsum: d.extra?.tsum ?? null,
|
|||
|
|
news_len: (det.news ?? []).length,
|
|||
|
|
news0: (det.news ?? [])[0]?.text ?? null,
|
|||
|
|
view: d.view ?? null,
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
const hg = await api('get_group_msg_history', { group_id: GROUP, count: 100 });
|
|||
|
|
collect(hg?.data?.messages, 'group');
|
|||
|
|
const hp = await api('get_friend_msg_history', { user_id: FRIEND, count: 60 });
|
|||
|
|
collect(hp?.data?.messages, 'private');
|
|||
|
|
say(`history 中 multimsg 外层消息 ${multimsg.length} 条`);
|
|||
|
|
for (const m of multimsg) {
|
|||
|
|
rec({ kind: 'multimsg_outer', ...m });
|
|||
|
|
say(` [${m.scope}] seq=${m.seq} mid=${m.message_id} tsum=${m.tsum} news=${m.news_len} view=${m.view} uniseq=${String(m.uniseq).slice(0, 12)}…`);
|
|||
|
|
}
|
|||
|
|
if (!multimsg.length) { say('history 窗口内没有 multimsg 消息,无法继续(不是能力缺口,是窗口不足)'); try { ws.close(); } catch {} process.exit(3); }
|
|||
|
|
|
|||
|
|
// 2) news[].text 能否还原为段数组 —— 「不依赖任何读回 API」的唯一路径。
|
|||
|
|
// 逐条打印全部 news 原文,因为不同发送形态(content 传段数组 vs 传字符串)落到的
|
|||
|
|
// news 文本形态不同,必须逐条看清才能给「能否还原」下定论。
|
|||
|
|
say('\n--- 还原路径 A:解析 json 段 meta.detail.news[].text ---');
|
|||
|
|
for (const m of multimsg) {
|
|||
|
|
const g = await api('get_msg', { message_id: Number(m.message_id) });
|
|||
|
|
const gm = g?.data;
|
|||
|
|
const jsonSeg = (gm?.message ?? []).find((s) => s.type === 'json');
|
|||
|
|
let detail = null;
|
|||
|
|
try { detail = JSON.parse(jsonSeg.data.data).meta?.detail ?? null; } catch {}
|
|||
|
|
const news = detail?.news ?? [];
|
|||
|
|
const items = news.map((n) => {
|
|||
|
|
const t = String(n.text ?? '');
|
|||
|
|
const colon = t.indexOf(': ');
|
|||
|
|
const body = colon >= 0 ? t.slice(colon + 2) : t;
|
|||
|
|
let parsed = null, err = null;
|
|||
|
|
try { parsed = JSON.parse(body); } catch (e) { err = e.message; }
|
|||
|
|
return { text: t, nick_prefix: colon >= 0 ? t.slice(0, colon) : null,
|
|||
|
|
parse_ok: Array.isArray(parsed), seg_types: Array.isArray(parsed) ? parsed.map((s) => s.type).join('+') : null,
|
|||
|
|
parse_err: err };
|
|||
|
|
});
|
|||
|
|
const okAll = items.length > 0 && items.every((i) => i.parse_ok);
|
|||
|
|
rec({ kind: 'news_recover', scope: m.scope, seq: m.seq, message_id: m.message_id,
|
|||
|
|
get_msg_retcode: g?.retcode ?? null, get_msg_segs: (gm?.message ?? []).map((s) => s.type).join('+') || null,
|
|||
|
|
detail_keys: detail ? Object.keys(detail) : null, tsum: m.tsum, news_len: items.length,
|
|||
|
|
all_parse_ok: okAll, items, resid: m.resid, uniseq: m.uniseq });
|
|||
|
|
say(` [${m.scope}] seq=${String(m.seq).padEnd(5)} get_msg=${g?.retcode} news=${items.length} 全可解析=${okAll ? '★是' : '否'}`);
|
|||
|
|
for (const it of items) {
|
|||
|
|
say(` ${it.parse_ok ? '段数组 ' + it.seg_types : '纯文本 '} | ${it.text.slice(0, 90)}`);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3) get_forward_msg:对**每一条**外层 multimsg 消息都试外层 id,
|
|||
|
|
// 避免把「单条消息过期」误判成「接口能力缺口」。
|
|||
|
|
say('\n--- 还原路径 B:get_forward_msg(外层 message_id,逐条外层消息重试)---');
|
|||
|
|
const outer = [...multimsg].sort((a, b) => Number(b.seq) - Number(a.seq))
|
|||
|
|
.find((m) => m.scope === 'group' && Number(m.tsum) >= 2) ?? multimsg[0];
|
|||
|
|
const outerIds = multimsg.filter((m) => m.scope === 'group');
|
|||
|
|
for (const m of outerIds) {
|
|||
|
|
const r = await api('get_forward_msg', { message_id: Number(m.message_id) });
|
|||
|
|
const nodes = r?.data?.messages ?? r?.data?.nodes ?? null;
|
|||
|
|
rec({ kind: 'get_forward_msg', id: `outer_mid_only:seq${m.seq}`, params: { message_id: Number(m.message_id) },
|
|||
|
|
retcode: r.retcode ?? null, message: r.message ?? r.wording ?? null, timeout: !!r.__timeout,
|
|||
|
|
node_count: Array.isArray(nodes) ? nodes.length : null, resp: r });
|
|||
|
|
say(` 外层 seq=${String(m.seq).padEnd(5)} mid=${String(m.message_id).padEnd(11)} tsum=${m.tsum} retcode=${r.retcode} ${(r.retcode === 0 ? '★成功 ' + JSON.stringify(r.data).slice(0, 140) : (r.message || '').slice(0, 46))}`);
|
|||
|
|
}
|
|||
|
|
const variants = [
|
|||
|
|
{ label: 'outer_mid', params: { message_id: Number(outer.message_id) } },
|
|||
|
|
{ label: 'outer_mid_str', params: { message_id: String(outer.message_id) } },
|
|||
|
|
{ label: 'outer_mid_group', params: { message_id: Number(outer.message_id), group_id: GROUP } },
|
|||
|
|
{ label: 'outer_mid_resid_uniseq', params: { message_id: Number(outer.message_id), resid: outer.resid, uniseq: outer.uniseq } },
|
|||
|
|
{ label: 'outer_mid_resid', params: { message_id: Number(outer.message_id), resid: outer.resid } },
|
|||
|
|
{ label: 'forward_id', params: { forward_id: outer.uniseq } },
|
|||
|
|
{ label: 'id_uniseq', params: { id: outer.uniseq } },
|
|||
|
|
{ label: 'uniseq_only', params: { uniseq: outer.uniseq } },
|
|||
|
|
{ label: 'resid_only', params: { resid: outer.resid } },
|
|||
|
|
{ label: 'filename', params: { filename: outer.filename } },
|
|||
|
|
{ label: 'outer_mid_resid_filename', params: { message_id: Number(outer.message_id), resid: outer.resid, filename: outer.filename } },
|
|||
|
|
];
|
|||
|
|
for (const v of variants) {
|
|||
|
|
const r = await api('get_forward_msg', v.params);
|
|||
|
|
const data = r?.data;
|
|||
|
|
const nodes = data?.messages ?? data?.nodes ?? null;
|
|||
|
|
const ok = r.retcode === 0;
|
|||
|
|
rec({ kind: 'get_forward_msg', id: `outer:${v.label}`, params: v.params, retcode: r.retcode ?? null,
|
|||
|
|
message: r.message ?? r.wording ?? null, timeout: !!r.__timeout,
|
|||
|
|
data_keys: data && typeof data === 'object' ? Object.keys(data) : null,
|
|||
|
|
node_count: Array.isArray(nodes) ? nodes.length : null,
|
|||
|
|
node_segs: Array.isArray(nodes) ? nodes.map((n) => (n.message ?? []).map((s) => s.type).join('+')).join(',') : null,
|
|||
|
|
resp: r });
|
|||
|
|
say(` ${v.label.padEnd(26)} retcode=${r.retcode} ${ok ? '★成功 data=' + JSON.stringify(data).slice(0, 160) : (r.message || '').slice(0, 50)}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4) 其它可能读回转发内容的只读接口
|
|||
|
|
say('\n--- 还原路径 C:其它只读接口 ---');
|
|||
|
|
const others = [
|
|||
|
|
{ label: 'get_msg', action: 'get_msg', params: { message_id: Number(outer.message_id) } },
|
|||
|
|
{ label: 'get_group_msg', action: 'get_group_msg', params: { message_id: Number(outer.message_id) } },
|
|||
|
|
{ label: 'get_forward_msg_no_param', action: 'get_forward_msg', params: {} },
|
|||
|
|
];
|
|||
|
|
for (const o of others) {
|
|||
|
|
const r = await api(o.action, o.params);
|
|||
|
|
rec({ kind: o.action, id: `outer:${o.label}`, action: o.action, params: o.params,
|
|||
|
|
retcode: r.retcode ?? null, message: r.message ?? r.wording ?? null, timeout: !!r.__timeout, resp: r });
|
|||
|
|
const d = JSON.stringify(r?.data ?? null);
|
|||
|
|
say(` ${o.label.padEnd(26)} retcode=${r.retcode} ${(r.retcode === 0 ? 'data=' + d.slice(0, 200) : (r.message || '').slice(0, 60))}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
say('\n结果 -> ' + OUT);
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
process.exit(0);
|