feat(onebot): decode bot markdown and keyboards

Fixes #337
This commit is contained in:
DrSmoothl committed 2026-08-09 13:06:35 +08:00
1 parent 1e0c91e74d
commit 4aba81ec6b
12 files changed
+536 -46

No files matched your search

@@ -492,7 +492,12 @@ export const ELEMENT_CODECS = {
},
markdown: {
// 收侧(S)无对应,仅发·解存在。
async toSegment(element) {
return {
type: 'markdown',
data: { content: element.text },
};
},
async fromSegment(data) {
return {
type: 'markdown',
@@ -500,6 +505,36 @@ export const ELEMENT_CODECS = {
};
},
},
inline_keyboard: {
async toSegment(element) {
return {
type: 'inline_keyboard',
data: {
bot_appid: element.botAppid,
rows: element.rows.map((row) => ({
buttons: row.buttons.map((button) => ({
id: button.id,
label: button.label,
visited_label: button.visitedLabel,
style: button.style,
type: button.type,
click_limit: button.clickLimit,
unsupport_tips: button.unsupportedTips,
data: button.data,
at_bot_show_channel_list: button.atBotShowChannelList,
permission_type: button.permissionType,
specify_role_ids: button.specifyRoleIds,
specify_user_ids: button.specifyUserIds,
is_reply: button.isReply,
enter: button.enter,
anchor: button.anchor,
})),
})),
},
};
},
},
} satisfies ElementCodecMap;
/**
@@ -1791,6 +1791,7 @@ function elementPreview(element: MessageElement): string {
case 'json': return '[JSON消息]';
case 'xml': return '[XML消息]';
case 'markdown': return '[Markdown]';
case 'inline_keyboard': return '[交互按钮]';
case 'forward': return '[聊天记录]';
case 'poke': return '[窗口抖动]';
default: return '';
+57 -1
View File
@@ -365,7 +365,7 @@ describe('convertEvent — request kinds', () => {
});
});
describe('convertEvent — message elements (13 segment types)', () => {
describe('convertEvent — message elements', () => {
// Round-trips one element through elementsToOneBotSegments and
// asserts the OneBot segment shape. Resolvers are wired only when
// the element type needs them. JsonArray's index type is JsonValue
@@ -523,6 +523,62 @@ describe('convertEvent — message elements (13 segment types)', () => {
expect((seg2.data as Record<string, unknown>).resid).toBe(50);
});
it('markdown: exposes the original content on receive', async () => {
const seg = await segment({ type: 'markdown', text: '# Bot answer' });
expect(seg).toEqual({ type: 'markdown', data: { content: '# Bot answer' } });
});
it('inline keyboard: exposes rows and actionable button metadata', async () => {
const seg = await segment({
type: 'inline_keyboard',
botAppid: '9007199254740993',
rows: [{
buttons: [{
id: 'btn-1',
label: 'Run',
visitedLabel: 'Done',
style: 1,
type: 2,
clickLimit: 3,
unsupportedTips: 'Admins only',
data: 'callback-data',
atBotShowChannelList: true,
permissionType: 2,
specifyRoleIds: ['admin'],
specifyUserIds: ['u_10001'],
isReply: false,
enter: true,
anchor: 9,
}],
}],
});
expect(seg).toEqual({
type: 'inline_keyboard',
data: {
bot_appid: '9007199254740993',
rows: [{
buttons: [{
id: 'btn-1',
label: 'Run',
visited_label: 'Done',
style: 1,
type: 2,
click_limit: 3,
unsupport_tips: 'Admins only',
data: 'callback-data',
at_bot_show_channel_list: true,
permission_type: 2,
specify_role_ids: ['admin'],
specify_user_ids: ['u_10001'],
is_reply: false,
enter: true,
anchor: 9,
}],
}],
},
});
});
it('file: canonical file/file_id/file_size + legacy name/size/id + url + file_hash', async () => {
const seg = await segment(
{ type: 'file', fileName: 'doc.pdf', fileSize: 7, fileId: 'fid', fileHash: 'h' },