From 11e7584d1c977f16e90e8136c369b23a022dffce Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 22 Jun 2026 15:25:28 +0800 Subject: [PATCH] fix(onebot): approve bot self-invite via private card msgseq (#125) When the bot is invited to a group via a private-chat "qun.invite" ark card, set_group_add_request failed: handleGroupAddRequest re-derived the approval tuple from fetchGroupRequests (0x10c8_2), but for a self-invite the only sequence the server accepts at 0x10c8_1 is the msgseq embedded in the card's mqqapi://group/invite_join jumpUrl, applied with eventType=2 / filtered=false. The MSF invite push (PkgType 87) never carries that msgseq, so approval returned 120161001 'handle async message fail'. - friend-message decoder: parse the private invite card's jumpUrl and stash groupUin -> msgseq on the per-instance IdentityService. - ContactsApi.getGroupInviteCardSequence(groupId) exposes it. - handleGroupAddRequest: for an invite flag, if a card msgseq is cached, approve with setAddRequest(groupId, msgseq, 2, approve, reason, false); otherwise fall back to the existing fetchGroupRequests path. Validated on-target (Docker): private invite card -> set_group_add_request approve -> 0x10c8_1 ok -> bot joined the group (get_group_member_info shows role=member, fresh join_time); no 120161001. --- packages/core/src/bridge/apis/contacts.ts | 7 ++++ .../onebot/src/modules/request-actions.ts | 12 +++++++ packages/protocol/src/identity-service.ts | 15 +++++++++ .../src/msg-push/decoders/friend-message.ts | 32 ++++++++++++++++++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/packages/core/src/bridge/apis/contacts.ts b/packages/core/src/bridge/apis/contacts.ts index 379abc46..24a9618b 100644 --- a/packages/core/src/bridge/apis/contacts.ts +++ b/packages/core/src/bridge/apis/contacts.ts @@ -232,6 +232,13 @@ export class ContactsApi { return requests; } + /** The approval msgseq captured from a private "qun.invite" card for this + * group, or undefined if none was seen. `set_group_add_request` uses it to + * approve a bot self-invite via 0x10c8 (eventType=2). See issue #125. */ + getGroupInviteCardSequence(groupId: number): number | undefined { + return this.ctx.identity.getGroupInviteCardSequence(groupId); + } + async fetchDownloadRKeys(): Promise { const resp = await FetchDownloadRkeys.invoke(this.ctx); diff --git a/packages/onebot/src/modules/request-actions.ts b/packages/onebot/src/modules/request-actions.ts index 29954e2b..573179a1 100644 --- a/packages/onebot/src/modules/request-actions.ts +++ b/packages/onebot/src/modules/request-actions.ts @@ -15,6 +15,18 @@ export async function handleGroupAddRequest( if (!groupId) throw new Error('invalid group_id in flag'); if (!targetUid) throw new Error('invalid request target in flag'); + // Bot self-invited via a private "qun.invite" card: the only sequence the + // server accepts at 0x10c8_1 is that card's jumpUrl msgseq, with eventType=2 + // / filtered=false. The fetchGroupRequests tuple below fails this case with + // 120161001 ("handle async message fail"). See issue #125. + if (requestType === 'invite') { + const cardSequence = bridge.apis.contacts.getGroupInviteCardSequence(groupId); + if (cardSequence) { + await bridge.apis.groupAdmin.setAddRequest(groupId, cardSequence, 2, approve, reason, false); + return; + } + } + const requests = await bridge.apis.contacts.fetchGroupRequests(); const matching = requests.find((r) => { if (r.groupId !== groupId) return false; diff --git a/packages/protocol/src/identity-service.ts b/packages/protocol/src/identity-service.ts index d2e566e2..7e7ee814 100644 --- a/packages/protocol/src/identity-service.ts +++ b/packages/protocol/src/identity-service.ts @@ -105,6 +105,10 @@ export class IdentityService { private readonly userProfiles_ = new Map(); private friends_: FriendInfo[] = []; private readonly groups_ = new Map(); + // groupUin → approval msgseq from a private "qun.invite" card's jumpUrl. + // The 0x10c8 approval for a bot self-invite needs THIS sequence (with + // eventType=2); the MSF invite push never carries it. See issue #125. + private readonly groupInviteCardSeqs_ = new Map(); // ─── Bidirectional UID↔UIN index (O(1), populated by every observation) ─── private readonly uinByUid = new Map(); @@ -381,6 +385,17 @@ export class IdentityService { }))); } + /** Remember the approval msgseq carried by a private "qun.invite" card's + * jumpUrl, keyed by group. `set_group_add_request` reads it back to approve + * a bot self-invite via 0x10c8 (eventType=2). See issue #125. */ + rememberGroupInviteCardSequence(groupUin: number, sequence: number): void { + if (groupUin > 0 && sequence > 0) this.groupInviteCardSeqs_.set(groupUin, sequence); + } + + getGroupInviteCardSequence(groupUin: number): number | undefined { + return this.groupInviteCardSeqs_.get(groupUin); + } + rememberGroupRequests(requests: GroupRequestInfo[]): void { for (const request of requests) { this.rememberUidUin(request.targetUid, request.targetUin); diff --git a/packages/protocol/src/msg-push/decoders/friend-message.ts b/packages/protocol/src/msg-push/decoders/friend-message.ts index d0bedd21..858b4e4e 100644 --- a/packages/protocol/src/msg-push/decoders/friend-message.ts +++ b/packages/protocol/src/msg-push/decoders/friend-message.ts @@ -2,7 +2,37 @@ import type { FriendMessage } from '../../events'; import type { MsgPushDecoder } from '../registry'; import { decodeRichBody } from '../rich-body-decoder'; +/** + * A private "qun.invite" ark card (someone DMs the bot a group-invite card) + * carries the only sequence the server accepts when the bot later approves the + * invite via 0x10c8 — the `msgseq` embedded in its `mqqapi://group/invite_join` + * jumpUrl, applied with eventType=2 / filtered=false. The MSF invite push + * (PkgType 87) never carries it. Parse it out so `set_group_add_request` can + * use it. See issue #125. + */ +function parseGroupInviteCard(jsonText: string): { groupUin: number; sequence: number } | null { + if (!jsonText.includes('group/invite_join')) return null; + const m = jsonText.match(/mqqapi:\/\/group\/invite_join[^"\\\s]*/); + if (!m) return null; + const query = m[0].split('?')[1] ?? ''; + const params = new URLSearchParams(query); + const groupUin = Number(params.get('groupcode') ?? 0); + const sequence = Number(params.get('msgseq') ?? 0); + if (!Number.isSafeInteger(groupUin) || !Number.isSafeInteger(sequence)) return null; + if (groupUin <= 0 || sequence <= 0) return null; + return { groupUin, sequence }; +} + export const decodeFriendMessage: MsgPushDecoder = (ctx) => { + const elements = decodeRichBody(ctx.body, false); + // Stash the approval msgseq from a private group-invite card so a later + // `set_group_add_request` can approve a bot self-invite (issue #125). + for (const el of elements) { + if (el.type === 'json' && typeof el.text === 'string') { + const card = parseGroupInviteCard(el.text); + if (card) ctx.identity.rememberGroupInviteCardSequence(card.groupUin, card.sequence); + } + } const ev: FriendMessage = { kind: 'friend_message', time: ctx.head.timestamp, @@ -10,7 +40,7 @@ export const decodeFriendMessage: MsgPushDecoder = (ctx) => { senderUin: ctx.fromUin, msgSeq: ctx.head.sequence, msgId: ctx.head.msgId & 0x7FFFFFFF, - elements: decodeRichBody(ctx.body, false), + elements, senderNick: '', }; if (ctx.responseHead?.forward?.friendName) {