fix(onebot): accept an empty group_id on private send_msg

Some clients fill unused group_id with an empty string when sending a
private message through send_msg. Treat that empty string as absent so
the send is a normal private chat.

Fixes #426
This commit is contained in:
DrSmoothl committed 2026-08-29 19:11:10 +08:00
1 parent d05c516ed6
commit 9e27910885
2 files changed
+52 -2

No files matched your search

+14 -2
View File
@@ -1,9 +1,21 @@
import { createLogger } from '@snowluma/common/logger';
import type { JsonObject, JsonValue } from '../types';
import type { ApiActionContext } from '../api-handler';
import { defineAction, groupAction, f } from '../action-kit';
import { defineAction, groupAction, f, type Field } from '../action-kit';
import { RETCODE, failedResponse, okResponse } from '../types';
/** send_msg only (#426). Some clients send `group_id: ""` on private chats.
* Treat that exact empty string as absent. Do not change `f.groupId()`. */
function sendMsgOptionalGroupId(): Field<number | undefined> {
const inner = f.groupId().optional();
return Object.assign(Object.create(inner) as Field<number | undefined>, {
coerce(raw: Parameters<Field<number | undefined>['coerce']>[0], field: string) {
if (raw === '') return { ok: true as const, value: undefined };
return inner.coerce(raw, field);
},
});
}
const log = createLogger('OneBot');
/**
@@ -47,7 +59,7 @@ export const actions = [
params: {
message: f.message(),
message_type: f.string().optional(),
group_id: f.groupId().optional(),
group_id: sendMsgOptionalGroupId(),
user_id: f.userId().optional(),
auto_escape: f.bool().default(false),
},
@@ -84,6 +84,24 @@ describe('send_msg parse', () => {
reason: 'must be >= 1',
});
});
it('treats an empty group_id string as absent', () => {
expect(sendMsg.parse({
message_type: 'private',
user_id: 1316864423,
group_id: '',
message: 'hi',
})).toEqual({
ok: true,
value: {
message: 'hi',
message_type: 'private',
group_id: undefined,
user_id: 1316864423,
auto_escape: false,
},
});
});
});
describe('send_msg run', () => {
@@ -120,6 +138,26 @@ describe('send_msg run', () => {
expect(sendPrivateMessage).not.toHaveBeenCalled();
});
it('sends a private message when group_id is an empty string', async () => {
const sendGroupMessage = vi.fn(async () => ({ messageId: 0 }));
const sendPrivateMessage = vi.fn(async () => ({ messageId: 9006 }));
const response = await sendMsg.toHandler(asCtx({ sendGroupMessage, sendPrivateMessage }))({
message_type: 'private',
user_id: 1316864423,
group_id: '',
message: [{ type: 'image', data: { file: 'base64://x' } }],
});
expect(response).toEqual({ status: 'ok', retcode: 0, data: { message_id: 9006 } });
expect(sendPrivateMessage.mock.calls).toEqual([[
1316864423,
[{ type: 'image', data: { file: 'base64://x' } }],
false,
undefined,
]]);
expect(sendGroupMessage).not.toHaveBeenCalled();
});
it('sends a private message when only user_id is present', async () => {
const sendGroupMessage = vi.fn(async () => ({ messageId: 0 }));
const sendPrivateMessage = vi.fn(async () => ({ messageId: 9003 }));