100 lines
4.7 KiB
JavaScript
100 lines
4.7 KiB
JavaScript
// 采集真实收发样本:连接 NapCat 正向 WS → 发送一条测试消息 → 抓取回推事件原始 JSON
|
|||
|
|
// 目的:为消息模型 / 消息段 / 去重键设计提供实测依据(decision-baseline 第 7 章样本采集)。
|
||
|
|
// 凭据只从环境变量读取。发送是真实外部副作用,必须显式 --send。
|
||
|
|
//
|
||
|
|
// 用法:
|
||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/capture-sample.mjs --send private <目标QQ>
|
||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/capture-sample.mjs --send group <群号>
|
||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/capture-sample.mjs # 只监听不发
|
||
|
|
const WS_URL = (process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099').replace(/\/$/, '');
|
||
|
|
const TOKEN = process.env.NAPCAT_WS_TOKEN;
|
||
|
|
const WATCH_MS = Number(process.env.WATCH_MS || 45000);
|
||
|
|
|
||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN'); process.exit(1); }
|
||
|
|
|
||
|
|
const argv = process.argv.slice(2);
|
||
|
|
const doSend = argv[0] === '--send';
|
||
|
|
const kind = doSend ? argv[1] : null;
|
||
|
|
const target = doSend ? Number(argv[2]) : null;
|
||
|
|
|
||
|
|
if (doSend && !['private', 'group'].includes(kind)) {
|
||
|
|
console.error('kind 必须是 private 或 group'); process.exit(1);
|
||
|
|
}
|
||
|
|
if (doSend && !Number.isFinite(target)) {
|
||
|
|
console.error('缺少合法的目标 QQ 号 / 群号'); process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const MARK = `QLINKER-PROBE-${new Date().toISOString().slice(11, 19).replace(/:/g, '')}`;
|
||
|
|
const TEXT = `[QLinker 协议实测] 标记=${MARK} 这条消息由自动化探测脚本发出,用于采集消息结构样本,可忽略。`;
|
||
|
|
|
||
|
|
// SEGMENTS_JSON 允许传入任意消息段数组,用于采集不同段类型的真实结构
|
||
|
|
const SEGMENTS_JSON = process.env.SEGMENTS_JSON;
|
||
|
|
|
||
|
|
const events = [];
|
||
|
|
const ws = new WebSocket(`${WS_URL}/?access_token=${encodeURIComponent(TOKEN)}`);
|
||
|
|
const pending = new Map();
|
||
|
|
|
||
|
|
ws.onmessage = (ev) => {
|
||
|
|
let o;
|
||
|
|
try { o = JSON.parse(String(ev.data)); } catch { events.push({ __unparsed: String(ev.data).slice(0, 500) }); return; }
|
||
|
|
if (o.echo && pending.has(o.echo)) {
|
||
|
|
const p = pending.get(o.echo); pending.delete(o.echo); clearTimeout(p.to); p.resolve(o); return;
|
||
|
|
}
|
||
|
|
events.push(o);
|
||
|
|
const tag = o.post_type + '/' + (o.message_type || o.notice_type || o.meta_event_type || o.request_type || '-');
|
||
|
|
console.log(`[EVENT ${new Date().toISOString().slice(11, 23)}] ${tag}` +
|
||
|
|
(o.message_id ? ` message_id=${o.message_id}` : '') +
|
||
|
|
(o.user_id ? ` user_id=${o.user_id}` : '') +
|
||
|
|
(o.group_id ? ` group_id=${o.group_id}` : ''));
|
||
|
|
};
|
||
|
|
|
||
|
|
await new Promise((res, rej) => {
|
||
|
|
ws.onopen = res;
|
||
|
|
setTimeout(() => rej(new Error('连接超时')), 10000);
|
||
|
|
}).catch((e) => { console.error(e.message); process.exit(1); });
|
||
|
|
|
||
|
|
function call(action, params, ms = 20000) {
|
||
|
|
const echo = `${action}-${Date.now()}`;
|
||
|
|
return new Promise((resolve) => {
|
||
|
|
const to = setTimeout(() => { pending.delete(echo); resolve({ __timeout: true }); }, ms);
|
||
|
|
pending.set(echo, { resolve, to });
|
||
|
|
ws.send(JSON.stringify({ action, params, echo }));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const self = await call('get_login_info', {});
|
||
|
|
console.log(`已连接,登录号 = ${self?.data?.user_id} (${self?.data?.nickname})`);
|
||
|
|
|
||
|
|
if (doSend) {
|
||
|
|
const action = kind === 'private' ? 'send_private_msg' : 'send_group_msg';
|
||
|
|
let message = TEXT;
|
||
|
|
if (SEGMENTS_JSON) {
|
||
|
|
try { message = JSON.parse(SEGMENTS_JSON); } catch (e) { console.error('SEGMENTS_JSON 非法: ' + e.message); process.exit(1); }
|
||
|
|
console.log(`使用自定义消息段: ${JSON.stringify(message).slice(0, 200)}`);
|
||
|
|
}
|
||
|
|
const params = kind === 'private'
|
||
|
|
? { user_id: target, message }
|
||
|
|
: { group_id: target, message };
|
||
|
|
console.log(`\n发送 ${action} → ${target}`);
|
||
|
|
const r = await call(action, params);
|
||
|
|
if (r.__timeout) console.log('发送无响应(20s 超时)→ 需按「结果未知」处理');
|
||
|
|
else console.log('发送响应: ' + JSON.stringify({ status: r.status, retcode: r.retcode, message_id: r.data?.message_id }));
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`\n监听事件 ${WATCH_MS / 1000}s(期间请在测试号上收/发消息)...`);
|
||
|
|
await new Promise(r => setTimeout(r, WATCH_MS));
|
||
|
|
try { ws.close(); } catch {}
|
||
|
|
|
||
|
|
const counts = {};
|
||
|
|
for (const e of events) {
|
||
|
|
const k = e.post_type ? `${e.post_type}/${e.message_type || e.notice_type || e.meta_event_type || e.request_type || '-'}` : 'unparsed';
|
||
|
|
counts[k] = (counts[k] || 0) + 1;
|
||
|
|
}
|
||
|
|
console.log('\n=== 事件类型统计 ===');
|
||
|
|
Object.entries(counts).sort().forEach(([k, v]) => console.log(` ${k}: ${v}`));
|
||
|
|
|
||
|
|
// message_sent 是 NapCat 扩展的 post_type,标准 OneBot11 无此类型,必须一并采集
|
||
|
|
const interesting = events.filter(e => ['message', 'message_sent', 'notice', 'request'].includes(e.post_type));
|
||
|
|
console.log(`\n=== 业务事件原始样本 ${interesting.length} 条 ===`);
|
||
|
|
interesting.forEach((e, i) => console.log(`--- sample[${i}] ---\n${JSON.stringify(e, null, 2)}`));
|