From 04a9016a7d23b2477e50708cd4c3e52310293a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=B8=E6=89=8B=E5=A5=BD=E9=97=B2=E7=9A=84=E8=A8=80?= =?UTF-8?q?=E6=9F=92?= <68868379+tt-P607@users.noreply.github.com> Date: Fri, 26 Jun 2026 21:08:01 +0800 Subject: [PATCH] fix(onebot): strip at segments from private messages (#151) * fix(onebot): support inline file url/path in send_group/private_msg * test(onebot): update file segment parser test for inline url/path support * fix(onebot): strip at segments from private messages --- .../onebot/src/modules/message-actions.ts | 16 ++ .../tests/send-private-strip-at.test.ts | 157 ++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 packages/onebot/tests/send-private-strip-at.test.ts diff --git a/packages/onebot/src/modules/message-actions.ts b/packages/onebot/src/modules/message-actions.ts index 9fd7ec2d..ed998a73 100644 --- a/packages/onebot/src/modules/message-actions.ts +++ b/packages/onebot/src/modules/message-actions.ts @@ -504,6 +504,22 @@ export async function sendPrivateMessage( }); if (elements.length === 0) throw new Error('message is empty'); + // Private chats cannot @-mention anyone — QQ simply ignores the mention + // element in c2c messages. Many OneBot callers blindly append an `at` + // segment right after `reply` (the group-chat convention), which ends up + // producing a broken wire message. Strip `at` elements so they never + // reach the protocol layer. + const hasReply = elements.some(e => e.type === 'reply'); + for (let i = elements.length - 1; i >= 0; i--) { + if (elements[i].type === 'at') { + if (!hasReply) { + log.warn('[OneBot] at segment in private message is unsupported — stripped'); + } + elements.splice(i, 1); + } + } + if (elements.length === 0) throw new Error('message is empty'); + // C2C `{type:'file'}` segments can't ride on the elems[] pipeline — // c2c files live on `RichText.notOnlineFile`, parallel to elems // (see `@snowluma/proto-defs/message:notOnlineFile`). The element-builder diff --git a/packages/onebot/tests/send-private-strip-at.test.ts b/packages/onebot/tests/send-private-strip-at.test.ts new file mode 100644 index 00000000..97115e53 --- /dev/null +++ b/packages/onebot/tests/send-private-strip-at.test.ts @@ -0,0 +1,157 @@ +// Regression coverage for the private-chat-at bug. +// +// Symptom in the wild: bot called `send_private_msg` with a message +// like `[reply, at, text]` — the group-chat convention of quoting + +// @-mentioning the reply target. QQ ignores mention elements in c2c +// messages so the `at` segment is pointless; worse, it produces a +// broken-looking wire message (the recipient sees a phantom "@QQ号" +// that can't be clicked). +// +// Fix: `sendPrivateMessage` now strips all `at` elements before +// handing the array to the protocol layer. When the message also +// contains a `reply` segment the strip is silent (it's the normal +// OneBot convention and the caller didn't do anything wrong); without +// a `reply` the strip still happens but a warn is logged. + +import { describe, expect, it, vi } from 'vitest'; +import type { BridgeInterface } from '../../src/bridge/bridge-interface'; +import type { OneBotInstanceContext } from '../src/instance-context'; +import { sendPrivateMessage } from '../src/modules/message-actions'; + +function fakeBridge(overrides: Partial = {}): BridgeInterface { + return new Proxy(overrides as BridgeInterface, { + get(target, prop) { + if (prop in target) return (target as any)[prop]; + throw new Error(`fakeBridge: '${String(prop)}' was not stubbed for this test`); + }, + }); +} + +const goodReceipt = { + messageId: 1, sequence: 100, clientSequence: 0, random: 1, timestamp: 1700000000, +}; + +function makeCtx(bridge: BridgeInterface): OneBotInstanceContext { + return { + uin: '10001', + selfId: 10001, + bridge, + messageStore: { + findEvent: () => null, + findMeta: () => null, + resolveReplySequence: () => 42, + } as any, + cacheMessageMeta: vi.fn(), + mediaStore: {} as any, + musicSignUrl: '', + } as unknown as OneBotInstanceContext; +} + +describe('send_private_msg strips at segments', () => { + it('reply + at + text → only reply + text reach the wire', async () => { + const sendPrivate = vi.fn(async (_uin: number, elements: any[]) => { + // Verify no `at` element reaches the bridge + const types = elements.map((e: any) => e.type); + expect(types).not.toContain('at'); + return goodReceipt; + }); + const bridge = fakeBridge({ + apis: { message: { sendPrivate } } as any, + resolveUserUid: vi.fn(async () => 'u_peer'), + } as any); + const ctx = makeCtx(bridge); + + await sendPrivateMessage(ctx, 67890, [ + { type: 'reply', data: { id: '-2050237785' } }, + { type: 'at', data: { qq: '67890' } }, + { type: 'text', data: { text: ' ' } }, + { type: 'text', data: { text: 'quoted reply text' } }, + ] as any, false); + + expect(sendPrivate).toHaveBeenCalledOnce(); + const sentElements = sendPrivate.mock.calls[0]![1] as any[]; + const sentTypes = sentElements.map(e => e.type); + expect(sentTypes).toEqual(['reply', 'text', 'text']); + }); + + it('at-only message (no reply) is also stripped, throws "message is empty"', async () => { + const sendPrivate = vi.fn(); + const bridge = fakeBridge({ + apis: { message: { sendPrivate } } as any, + resolveUserUid: vi.fn(async () => 'u_peer'), + } as any); + const ctx = makeCtx(bridge); + + await expect(sendPrivateMessage(ctx, 67890, [ + { type: 'at', data: { qq: '67890' } }, + ] as any, false)).rejects.toThrow('message is empty'); + + expect(sendPrivate).not.toHaveBeenCalled(); + }); + + it('text + at (without reply) strips at and sends remaining text', async () => { + const sendPrivate = vi.fn(async (_uin: number, _elements: any[]) => goodReceipt); + const bridge = fakeBridge({ + apis: { message: { sendPrivate } } as any, + resolveUserUid: vi.fn(async () => 'u_peer'), + } as any); + const ctx = makeCtx(bridge); + + await sendPrivateMessage(ctx, 67890, [ + { type: 'text', data: { text: 'hello' } }, + { type: 'at', data: { qq: '67890' } }, + ] as any, false); + + expect(sendPrivate).toHaveBeenCalledOnce(); + const sentElements = sendPrivate.mock.calls[0]![1] as any[]; + expect(sentElements).toEqual([{ type: 'text', text: 'hello' }]); + }); + + it('multiple at segments are all stripped', async () => { + const sendPrivate = vi.fn(async (_uin: number, _elements: any[]) => goodReceipt); + const bridge = fakeBridge({ + apis: { message: { sendPrivate } } as any, + resolveUserUid: vi.fn(async () => 'u_peer'), + } as any); + const ctx = makeCtx(bridge); + + await sendPrivateMessage(ctx, 67890, [ + { type: 'reply', data: { id: '123' } }, + { type: 'at', data: { qq: '67890' } }, + { type: 'at', data: { qq: '12345' } }, + { type: 'text', data: { text: 'hi' } }, + ] as any, false); + + expect(sendPrivate).toHaveBeenCalledOnce(); + const sentElements = sendPrivate.mock.calls[0]![1] as any[]; + const sentTypes = sentElements.map((e: any) => e.type); + expect(sentTypes).not.toContain('at'); + expect(sentTypes).toContain('reply'); + expect(sentTypes).toContain('text'); + }); + + it('group messages are NOT affected (at segments preserved)', async () => { + // Sanity: import sendGroupMessage and verify at segments pass through + const { sendGroupMessage } = await import('../src/modules/message-actions'); + + const sendGroup = vi.fn(async (_groupId: number, elements: any[]) => { + return goodReceipt; + }); + const bridge = fakeBridge({ + apis: { message: { sendGroup } } as any, + resolveUserUid: vi.fn(async () => 'u_peer'), + } as any); + const ctx = makeCtx(bridge); + + await sendGroupMessage(ctx, 99999, [ + { type: 'reply', data: { id: '123' } }, + { type: 'at', data: { qq: '67890' } }, + { type: 'text', data: { text: 'hi' } }, + ] as any, false); + + expect(sendGroup).toHaveBeenCalledOnce(); + const sentElements = sendGroup.mock.calls[0]![1] as any[]; + const sentTypes = sentElements.map((e: any) => e.type); + expect(sentTypes).toContain('at'); + }); +});