Feat: 发送图文ark卡片 (#355)

* feat(onebot): implement send tuwen ark

* test: add unit tests for send_ark action

* test(onebot): update canonical action docs count expectation to 191

* refactor(protocol,onebot): update 0xdc2_34 ark implementation and test coverage

- Rename action send_ark -> send_tuwen_ark to distinguish from structmsg and contact-share arks.
- Clarify SSO command mapping (OidbSvcTrpcTcp.0xdc2_34) and default param behavior in comments/docs.
- Adjust peerType, field3, and previewUrl to pb_optional to preserve 0/empty values during proton encoding.
- Update OneBot handler, tests, and auto-generated MCP tool catalog specs.

* style(protocol): fix indentation in rich-body-decoder tests
This commit is contained in:
H3CoF6 authored and GitHub committed 2026-08-20 12:12:39 +08:00
1 parent 62256ee816
commit 8fbddf67bf
11 files changed
+734 -137

No files matched your search

@@ -0,0 +1,67 @@
// 0xdc2_34 — send a custom 图文 (URL-share) ark card to a C2C peer or group.
// RE'd from QQ Android 9.3.25 captures. Default SSO: OidbSvcTrpcTcp.0xdc2_34.
// Fixed fields: appId = 100446242, field2 = 1, field3 = 0, field5 = {1:1}
// targetId appears at AppInfo[11] and Meta[2]; Meta.peerType: 0=C2C, 1=group.
// peerType, field3, and previewUrl are pb_optional — proton preserves 0/empty on wire.
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
Oidb0xdc2_34Req,
Oidb0xdc2_34Resp,
} from '@snowluma/proto-defs/oidb-actions/contact-ark';
import { invokeOidb, type OidbSender } from '../../oidb-service';
const TUWEN_ARK_APPID = 100446242;
export type SendTuwenArkParams = {
targetId: number;
peerType: 0 | 1;
title: string;
desc: string;
summary: string;
jumpUrl: string;
previewUrl: string;
};
export namespace SendTuwenArk {
export const command = 0xDC2;
export const subCommand = 34;
export const uinForm = false;
export type Params = SendTuwenArkParams;
export type Deps = OidbSender;
export const serialize = (_ctx: Deps, p: Params): Oidb0xdc2_34Req => ({
appInfo: {
appId: TUWEN_ARK_APPID,
field2: 1,
field3: 0,
field5: { field1: 1 },
targetId: p.targetId,
content: {
flag: 1,
title: p.title,
desc: p.desc,
summary: p.summary,
jumpUrl: p.jumpUrl,
previewUrl: p.previewUrl,
},
},
meta: {
peerType: p.peerType,
targetId: p.targetId,
},
});
export const deserialize = (_ctx: Deps, _body: Oidb0xdc2_34Resp): void => undefined;
export const encode = (env: OidbBase<Oidb0xdc2_34Req>): Uint8Array =>
protobuf_encode<OidbBase<Oidb0xdc2_34Req>>(env);
export const decode = (bytes: Uint8Array): OidbBase<Oidb0xdc2_34Resp> =>
protobuf_decode<OidbBase<Oidb0xdc2_34Resp>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<void> =>
invokeOidb(deps, SendTuwenArk, params);
}
@@ -923,153 +923,153 @@ describe('decodeRichBody / unknown wire element observability', () => {
}));
});
function ntImageElem(fileName: string): Elem {
const msgInfo: MsgInfo = {
msgInfoBody: [{
index: {
info: {
fileSize: 10711,
fileHash: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
fileSha1: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
fileName,
width: 1080,
height: 1080,
type: { picFormat: 1001 },
function ntImageElem(fileName: string): Elem {
const msgInfo: MsgInfo = {
msgInfoBody: [{
index: {
info: {
fileSize: 10711,
fileHash: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
fileSha1: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
fileName,
width: 1080,
height: 1080,
type: { picFormat: 1001 },
},
},
picture: {
urlPath: '/download?appid=1407&fileid=EhRgood',
domain: 'multimedia.nt.qq.com.cn',
ext: { originalParameter: '&rkey=good' },
},
}],
extBizInfo: { pic: { bizType: 0, textSummary: '[图片]' } },
};
return {
commonElem: {
serviceType: 48,
businessType: 20,
pbElem: protobuf_encode<MsgInfo>(msgInfo),
},
picture: {
urlPath: '/download?appid=1407&fileid=EhRgood',
domain: 'multimedia.nt.qq.com.cn',
ext: { originalParameter: '&rkey=good' },
};
}
function customFaceElem(filePath: string, origUrl: string, md5: Uint8Array): Elem {
return {
customFace: {
filePath,
origUrl,
md5,
size: 10711,
width: 1080,
height: 1080,
},
}],
extBizInfo: { pic: { bizType: 0, textSummary: '[图片]' } },
};
return {
commonElem: {
serviceType: 48,
businessType: 20,
pbElem: protobuf_encode<MsgInfo>(msgInfo),
},
};
}
};
}
function customFaceElem(filePath: string, origUrl: string, md5: Uint8Array): Elem {
return {
customFace: {
filePath,
origUrl,
md5,
size: 10711,
width: 1080,
height: 1080,
},
};
}
describe('decodeRichBody / NT image compatibility siblings (#389)', () => {
const fileName = 'bcc7bbcc76a1a9d940c57346a5d223e8.png';
const otherMd5 = Uint8Array.from({ length: 16 }, (_, i) => i + 1);
describe('decodeRichBody / NT image compatibility siblings (#389)', () => {
const fileName = 'bcc7bbcc76a1a9d940c57346a5d223e8.png';
const otherMd5 = Uint8Array.from({ length: 16 }, (_, i) => i + 1);
it('keeps the NT image and drops the query-only customFace sibling', () => {
const out = decodeRichBody({
richText: {
elems: [
ntImageElem(fileName),
customFaceElem(fileName, '&rkey=xxx&spec=0', otherMd5),
],
},
}, true);
it('keeps the NT image and drops the query-only customFace sibling', () => {
const out = decodeRichBody({
richText: {
elems: [
ntImageElem(fileName),
customFaceElem(fileName, '&rkey=xxx&spec=0', otherMd5),
],
},
}, true);
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({
type: 'image',
fileId: fileName,
imageUrl: 'https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=EhRgood&rkey=good',
picFormat: 1001,
});
});
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({
type: 'image',
fileId: fileName,
imageUrl: 'https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=EhRgood&rkey=good',
picFormat: 1001,
it('drops a same-file customFace even when its legacy URL is well-formed', () => {
const out = decodeRichBody({
richText: {
elems: [
ntImageElem(fileName),
customFaceElem(fileName, '/gchatpic_new/0/0-0-AABB/0', otherMd5),
],
},
}, true);
expect(out).toEqual([
expect.objectContaining({ type: 'image', fileId: fileName, picFormat: 1001 }),
]);
});
it('keeps a standalone customFace with a usable download path', () => {
const out = decodeRichBody({
richText: {
elems: [customFaceElem(fileName, '/gchatpic_new/0/0-0-AABB/0', otherMd5)],
},
}, true);
expect(out).toEqual([{
type: 'image',
imageUrl: 'http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0',
fileId: fileName,
fileSize: 10711,
width: 1080,
height: 1080,
subType: 0,
summary: '[图片]',
md5Hex: '0102030405060708090A0B0C0D0E0F10',
}]);
});
it('drops a standalone customFace whose URL cannot be fetched', () => {
const out = decodeRichBody({
richText: {
elems: [customFaceElem(fileName, '&rkey=xxx&spec=0', otherMd5)],
},
}, true);
expect(out).toEqual([]);
});
it('keeps two distinct pictures', () => {
const second = 'other.png';
const out = decodeRichBody({
richText: {
elems: [
ntImageElem(fileName),
customFaceElem(second, '/gchatpic_new/0/0-0-CCDD/0', otherMd5),
],
},
}, true);
expect(out.map((el) => el.type === 'image' ? el.fileId : el.type)).toEqual([
fileName,
second,
]);
});
});
it('drops a same-file customFace even when its legacy URL is well-formed', () => {
const out = decodeRichBody({
richText: {
elems: [
ntImageElem(fileName),
customFaceElem(fileName, '/gchatpic_new/0/0-0-AABB/0', otherMd5),
],
},
}, true);
describe('makeImageUrl', () => {
it('prefixes NT paths that already include a download path', () => {
expect(makeImageUrl('/download?appid=1407&fileid=abc&rkey=x'))
.toBe('https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=abc&rkey=x');
});
expect(out).toEqual([
expect.objectContaining({ type: 'image', fileId: fileName, picFormat: 1001 }),
]);
it('does not glue the NT host onto a query fragment (#389)', () => {
expect(makeImageUrl('&rkey=xxx&spec=0')).toBe('');
expect(makeImageUrl('rkey=xxx&spec=0')).toBe('');
});
it('prefixes legacy gchat paths', () => {
expect(makeImageUrl('/gchatpic_new/0/0-0-AABB/0'))
.toBe('http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0');
});
});
it('keeps a standalone customFace with a usable download path', () => {
const out = decodeRichBody({
richText: {
elems: [customFaceElem(fileName, '/gchatpic_new/0/0-0-AABB/0', otherMd5)],
},
}, true);
expect(out).toEqual([{
type: 'image',
imageUrl: 'http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0',
fileId: fileName,
fileSize: 10711,
width: 1080,
height: 1080,
subType: 0,
summary: '[图片]',
md5Hex: '0102030405060708090A0B0C0D0E0F10',
}]);
});
it('drops a standalone customFace whose URL cannot be fetched', () => {
const out = decodeRichBody({
richText: {
elems: [customFaceElem(fileName, '&rkey=xxx&spec=0', otherMd5)],
},
}, true);
expect(out).toEqual([]);
});
it('keeps two distinct pictures', () => {
const second = 'other.png';
const out = decodeRichBody({
richText: {
elems: [
ntImageElem(fileName),
customFaceElem(second, '/gchatpic_new/0/0-0-CCDD/0', otherMd5),
],
},
}, true);
expect(out.map((el) => el.type === 'image' ? el.fileId : el.type)).toEqual([
fileName,
second,
]);
});
});
describe('makeImageUrl', () => {
it('prefixes NT paths that already include a download path', () => {
expect(makeImageUrl('/download?appid=1407&fileid=abc&rkey=x'))
.toBe('https://multimedia.nt.qq.com.cn/download?appid=1407&fileid=abc&rkey=x');
});
it('does not glue the NT host onto a query fragment (#389)', () => {
expect(makeImageUrl('&rkey=xxx&spec=0')).toBe('');
expect(makeImageUrl('rkey=xxx&spec=0')).toBe('');
});
it('prefixes legacy gchat paths', () => {
expect(makeImageUrl('/gchatpic_new/0/0-0-AABB/0'))
.toBe('http://gchat.qpic.cn/gchatpic_new/0/0-0-AABB/0');
});
});
it('bounds retained card output across the entire message', () => {
const previousLevel = getLogLevel();
const captured: Array<{ scope: string; message: string }> = [];
@@ -3,7 +3,6 @@ import {
protobuf_encode,
type pb,
type pb_repeated,
type string,
type uint_32,
} from '@snowluma/proton';
import { describe, expect, it } from 'vitest';
@@ -0,0 +1,130 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { Oidb0xdc2_34Resp } from '@snowluma/proto-defs/oidb-actions/contact-ark';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { SendTuwenArk } from '../../../src/oidb-services/contacts/send-tuwen-ark';
import { env, v, s, m } from '../_pb-oracle';
function makeSender() {
const respEnv: OidbBase<Oidb0xdc2_34Resp> = { command: 0xDC2, subCommand: 34, body: {} };
const r: SendPacketResult = {
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
responseData: Buffer.from(protobuf_encode<OidbBase<Oidb0xdc2_34Resp>>(respEnv)),
};
return { sendRawPacket: vi.fn(async () => r) };
}
describe('SendTuwenArk namespace', () => {
it('declares command 0xdc2 sub 34, SSO: OidbSvcTrpcTcp.0xdc2_34', () => {
expect(SendTuwenArk.command).toBe(0xDC2);
expect(SendTuwenArk.subCommand).toBe(34);
expect(SendTuwenArk.uinForm).toBe(false);
});
it('serializes fixed appId=100446242, field2=1, field3=0, field5={1:1}', () => {
const out = SendTuwenArk.serialize({} as any, {
targetId: 12345,
peerType: 1,
title: 'T',
desc: 'D',
summary: 'S',
jumpUrl: 'https://example.com',
previewUrl: '',
});
expect(out.appInfo).toMatchObject({
appId: 100446242,
field2: 1,
field3: 0,
field5: { field1: 1 },
targetId: 12345,
});
});
it('byte-oracle: C2C (peerType=0) — peerType omitted on wire, field3=0 present, previewUrl present', async () => {
const sender = makeSender();
await SendTuwenArk.invoke(sender, {
targetId: 2863253201,
peerType: 0,
title: 'QQ开放平台',
desc: 'QQ小程序是连接广轻用户的新方式,覆盖8亿新生代活跃网民。轻便快捷的开发模式,将能在QQ内被轻松获取和传播',
summary: '[分享] QQ开放平台',
jumpUrl: 'https://q.qq.com/r',
previewUrl: 'https://tangram-1251316161.file.myqcloud.com/files/20210721/e50a8e37e08f29bf1ffc7466e1950690.png',
});
const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(cmd).toBe('OidbSvcTrpcTcp.0xdc2_34');
// AppInfo embedding: field5={1:1}, content={1:1, 10:title, 11:desc, 12:summary, 13:jumpUrl, 14:previewUrl}, then appId/field2/field3/targetId
const field5 = m(5, [...v(1, 1)]);
const content = m(12, [
...v(1, 1),
...s(10, 'QQ开放平台'),
...s(11, 'QQ小程序是连接广轻用户的新方式,覆盖8亿新生代活跃网民。轻便快捷的开发模式,将能在QQ内被轻松获取和传播'),
...s(12, '[分享] QQ开放平台'),
...s(13, 'https://q.qq.com/r'),
...s(14, 'https://tangram-1251316161.file.myqcloud.com/files/20210721/e50a8e37e08f29bf1ffc7466e1950690.png'),
]);
const appInfo = m(1, [
...v(1, 100446242),
...v(2, 1),
...v(3, 0), // field3=0 is present in wire (pb_optional preserves zero as expected)
...field5,
...v(11, 2863253201),
...content,
]);
// Meta: peerType=0 is present in wire (pb_optional preserves zero as expected)
const meta = m(2, [
...v(1, 0),
...v(2, 2863253201),
]);
const body = [...appInfo, ...meta];
expect(Buffer.from(bytes).toString('hex')).toBe(env(0xDC2, 34, body, false));
});
it('byte-oracle: group (peerType=1) — peerType present, field3=0 present, empty previewUrl present', async () => {
const sender = makeSender();
await SendTuwenArk.invoke(sender, {
targetId: 123456789,
peerType: 1,
title: '测试标题',
desc: '测试描述',
summary: '测试摘要',
jumpUrl: 'https://example.com/jump',
previewUrl: '',
});
const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(cmd).toBe('OidbSvcTrpcTcp.0xdc2_34');
const field5 = m(5, [...v(1, 1)]);
const content = m(12, [
...v(1, 1),
...s(10, '测试标题'),
...s(11, '测试描述'),
...s(12, '测试摘要'),
...s(13, 'https://example.com/jump'),
...s(14, ''), // previewUrl='' is present in wire (pb_optional preserves empty string as expected)
]);
const appInfo = m(1, [
...v(1, 100446242),
...v(2, 1),
...v(3, 0), // field3=0 is present
...field5,
...v(11, 123456789),
...content,
]);
// Meta: peerType=1 present
const meta = m(2, [
...v(1, 1),
...v(2, 123456789),
]);
const body = [...appInfo, ...meta];
expect(Buffer.from(bytes).toString('hex')).toBe(env(0xDC2, 34, body, false));
});
it('deserialize returns undefined (void response)', () => {
expect(SendTuwenArk.deserialize({} as any, {})).toBeUndefined();
});
});