Skip to content

Commit 4faa0de

Browse files
fix(qqbot): address review feedback from wenshao
- fixRestoredSessions: use entry.target directly instead of tt.get(undefined) (fixes first restored session routing to wrong conversation when 2+ sessions) - scheduleTokenRefresh: retry in 60s on token refresh failure, not just log - sendMessage: move saveQQState() after chunk loop, avoid redundant disk I/O - handleGroup: drop message when group_openid is missing instead of falling back to author.id (which would cause 404 on group message send)
1 parent db18055 commit 4faa0de

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

packages/channels/qqbot/src/QQChannel.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ export class QQChannel extends ChannelBase {
154154
if (msgId) {
155155
const seq = (this.msgSeqMap.get(msgId) ?? 0) + 1;
156156
this.msgSeqMap.set(msgId, seq);
157-
this.saveQQState();
158157
body['msg_id'] = msgId;
159158
body['msg_seq'] = seq;
160159
}
@@ -179,6 +178,8 @@ export class QQChannel extends ChannelBase {
179178
process.stderr.write(`[QQ:${this.name}] Send error: ${e}\n`);
180179
}
181180
}
181+
// Persist msgSeqMap once after all chunks are sent
182+
if (msgId) this.saveQQState();
182183
}
183184

184185
disconnect(): void {
@@ -283,10 +284,11 @@ export class QQChannel extends ChannelBase {
283284
| undefined;
284285
if (!entry?.sessionId) continue;
285286
const correctId: string = entry.sessionId;
286-
const target = tt.get(sid);
287+
// sid is undefined here — use entry.target directly instead of tt.get(undefined)
288+
const target = entry.target;
287289
tm.set(key, correctId);
288290
tt.delete(undefined as unknown as string);
289-
tt.set(correctId, target ?? entry.target);
291+
tt.set(correctId, target);
290292
if (tc) {
291293
tc.delete(undefined as unknown as string);
292294
tc.set(correctId, entry.cwd || '');
@@ -375,11 +377,15 @@ export class QQChannel extends ChannelBase {
375377
const delay = Math.max(Math.min(ttl * 0.8, ttl - 60_000), 60_000);
376378
if (delay > 0) {
377379
this.tokenRefreshTimer = setTimeout(() => {
378-
this.fetchToken().catch((e) =>
380+
this.fetchToken().catch((e) => {
379381
process.stderr.write(
380-
`[QQ:${this.name}] Token refresh failed: ${e}\n`,
381-
),
382-
);
382+
`[QQ:${this.name}] Token refresh failed: ${e}, retrying in 60s\n`,
383+
);
384+
this.tokenRefreshTimer = setTimeout(
385+
() => this.scheduleTokenRefresh(),
386+
60_000,
387+
);
388+
});
383389
}, delay);
384390
}
385391
}
@@ -677,7 +683,13 @@ export class QQChannel extends ChannelBase {
677683
}
678684

679685
private handleGroup(event: QQGroupMessageEvent): void {
680-
const chatId = event.group_openid || event.author.id;
686+
if (!event.group_openid) {
687+
process.stderr.write(
688+
`[QQ:${this.name}] Group message dropped: missing group_openid\n`,
689+
);
690+
return;
691+
}
692+
const chatId = event.group_openid;
681693
this.chatTypeMap.set(chatId, 'group');
682694
this.replyMsgId.set(chatId, event.id);
683695
this.saveQQState();

0 commit comments

Comments
 (0)