@michengai/dsh-im-connect 0.1.19 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/SECURITY.md +1 -1
- package/lib/channels/dingtalk-card.d.ts.map +1 -1
- package/lib/channels/dingtalk-card.js +3 -0
- package/lib/channels/dingtalk-card.js.map +1 -1
- package/lib/channels/dingtalk.d.ts.map +1 -1
- package/lib/channels/dingtalk.js +6 -1
- package/lib/channels/dingtalk.js.map +1 -1
- package/lib/channels/feishu.d.ts +9 -0
- package/lib/channels/feishu.d.ts.map +1 -1
- package/lib/channels/feishu.js +23 -6
- package/lib/channels/feishu.js.map +1 -1
- package/lib/channels/qq.d.ts.map +1 -1
- package/lib/channels/qq.js +17 -1
- package/lib/channels/qq.js.map +1 -1
- package/lib/channels/telegram.d.ts.map +1 -1
- package/lib/channels/telegram.js +17 -4
- package/lib/channels/telegram.js.map +1 -1
- package/lib/channels/wecom.d.ts +7 -1
- package/lib/channels/wecom.d.ts.map +1 -1
- package/lib/channels/wecom.js +44 -11
- package/lib/channels/wecom.js.map +1 -1
- package/lib/channels/weixin.d.ts +3 -0
- package/lib/channels/weixin.d.ts.map +1 -1
- package/lib/channels/weixin.js +68 -15
- package/lib/channels/weixin.js.map +1 -1
- package/lib/engine/abort.d.ts +4 -0
- package/lib/engine/abort.d.ts.map +1 -0
- package/lib/engine/abort.js +26 -0
- package/lib/engine/abort.js.map +1 -0
- package/lib/engine/atomic-file.d.ts +5 -0
- package/lib/engine/atomic-file.d.ts.map +1 -0
- package/lib/engine/atomic-file.js +46 -0
- package/lib/engine/atomic-file.js.map +1 -0
- package/lib/engine/json-state.d.ts.map +1 -1
- package/lib/engine/json-state.js +7 -5
- package/lib/engine/json-state.js.map +1 -1
- package/lib/engine/secure-file.d.ts.map +1 -1
- package/lib/engine/secure-file.js +2 -12
- package/lib/engine/secure-file.js.map +1 -1
- package/lib/engine/seen-store.d.ts.map +1 -1
- package/lib/engine/seen-store.js +4 -4
- package/lib/engine/seen-store.js.map +1 -1
- package/lib/engine/session-store.d.ts.map +1 -1
- package/lib/engine/session-store.js +7 -5
- package/lib/engine/session-store.js.map +1 -1
- package/lib/manager.d.ts +7 -0
- package/lib/manager.d.ts.map +1 -1
- package/lib/manager.js +50 -42
- package/lib/manager.js.map +1 -1
- package/package.json +1 -1
- package/src/channels/dingtalk-card.ts +195 -192
- package/src/channels/dingtalk.ts +144 -140
- package/src/channels/feishu.ts +92 -66
- package/src/channels/qq.ts +303 -288
- package/src/channels/telegram.ts +170 -158
- package/src/channels/wecom.ts +209 -177
- package/src/channels/weixin.ts +158 -112
- package/src/engine/abort.ts +25 -0
- package/src/engine/access.ts +34 -34
- package/src/engine/atomic-file.ts +36 -0
- package/src/engine/json-state.ts +16 -15
- package/src/engine/secure-file.ts +7 -13
- package/src/engine/seen-store.ts +39 -39
- package/src/engine/session-store.ts +40 -39
- package/src/manager.ts +346 -321
package/src/channels/qq.ts
CHANGED
|
@@ -1,302 +1,317 @@
|
|
|
1
|
-
/** QQ 开放平台机器人:官方 WebSocket 网关,不是个人 QQ 号。 */
|
|
1
|
+
/** QQ 开放平台机器人:官方 WebSocket 网关,不是个人 QQ 号。 */
|
|
2
2
|
import type { ChannelAdapter, ImMessage, ReplyStream } from '../engine/types.js'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let
|
|
51
|
-
let
|
|
52
|
-
let
|
|
53
|
-
let
|
|
54
|
-
let
|
|
55
|
-
let
|
|
56
|
-
let
|
|
57
|
-
let
|
|
58
|
-
|
|
3
|
+
import { timeoutSignal } from '../engine/abort.js'
|
|
4
|
+
|
|
5
|
+
export interface QqChannelConfig {
|
|
6
|
+
appId?: string
|
|
7
|
+
appSecret?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface GatewayPayload {
|
|
11
|
+
op: number
|
|
12
|
+
d?: unknown
|
|
13
|
+
s?: number
|
|
14
|
+
t?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface QQMessage {
|
|
18
|
+
id: string
|
|
19
|
+
content?: string
|
|
20
|
+
attachments?: unknown[]
|
|
21
|
+
author?: {
|
|
22
|
+
id?: string
|
|
23
|
+
user_openid?: string
|
|
24
|
+
member_openid?: string
|
|
25
|
+
username?: string
|
|
26
|
+
}
|
|
27
|
+
group_openid?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface ChatTarget {
|
|
31
|
+
kind: 'dm' | 'group'
|
|
32
|
+
lastMsgId?: string
|
|
33
|
+
seq: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const TOKEN_URL = 'https://bots.qq.com/app/getAppAccessToken'
|
|
37
|
+
const API = 'https://api.sgroup.qq.com'
|
|
38
|
+
const GATEWAY_PATH = '/gateway'
|
|
39
|
+
const GROUP_AND_C2C_INTENT = 1 << 25
|
|
40
|
+
|
|
41
|
+
export function cleanQqText(text: string): string {
|
|
42
|
+
return text.replace(/<@!?\w+>/g, '').replace(/^\s*@\S+\s+/, '').trim()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createQqChannel(config: QqChannelConfig, log: (line: string) => void): ChannelAdapter | undefined {
|
|
46
|
+
const appId = config.appId?.trim()
|
|
47
|
+
const appSecret = config.appSecret?.trim()
|
|
48
|
+
if (!appId || !appSecret) return undefined
|
|
49
|
+
|
|
50
|
+
let handler: ((msg: ImMessage) => void | Promise<void>) | undefined
|
|
51
|
+
let ws: WebSocket | undefined
|
|
52
|
+
let heartbeat: ReturnType<typeof setInterval> | undefined
|
|
53
|
+
let reconnectTimer: ReturnType<typeof setTimeout> | undefined
|
|
54
|
+
let stableTimer: ReturnType<typeof setTimeout> | undefined
|
|
55
|
+
let reconnectAttempts = 0
|
|
56
|
+
let stopped = false
|
|
57
|
+
let seq: number | null = null
|
|
58
|
+
let accessToken = ''
|
|
59
|
+
// 提前 5 分钟刷新;WebSocket 靠心跳可长期在线,不主动换 token 会在两小时后全线 401
|
|
59
60
|
let tokenExpireAt = 0
|
|
60
61
|
let statusText = '未连接'
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
reconnectAttempts
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
62
|
+
let lifecycle: AbortController | undefined
|
|
63
|
+
const targets = new Map<string, ChatTarget>()
|
|
64
|
+
|
|
65
|
+
function scheduleReconnect(): void {
|
|
66
|
+
if (stopped || reconnectTimer) return
|
|
67
|
+
const delay = Math.min(3000 * (2 ** reconnectAttempts), 60_000)
|
|
68
|
+
reconnectAttempts += 1
|
|
69
|
+
log(`[qq] ${Math.ceil(delay / 1000)}s 后重连`)
|
|
70
|
+
reconnectTimer = setTimeout(() => {
|
|
71
|
+
reconnectTimer = undefined
|
|
72
|
+
void connect().catch((err) => {
|
|
73
|
+
statusText = '重连失败'
|
|
74
|
+
log(`[qq] 重连失败: ${err instanceof Error ? err.message : String(err)}`)
|
|
75
|
+
scheduleReconnect()
|
|
76
|
+
})
|
|
77
|
+
}, delay)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function ensureToken(): Promise<void> {
|
|
81
|
+
if (accessToken && Date.now() < tokenExpireAt) return
|
|
82
|
+
const res = await fetch(TOKEN_URL, {
|
|
83
|
+
method: 'POST',
|
|
82
84
|
headers: { 'content-type': 'application/json' },
|
|
83
85
|
body: JSON.stringify({ appId, clientSecret: appSecret }),
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
86
|
+
signal: timeoutSignal(30_000, lifecycle?.signal),
|
|
87
|
+
})
|
|
88
|
+
const body = await res.text()
|
|
89
|
+
let data: { access_token?: string; expires_in?: number; message?: string }
|
|
90
|
+
try {
|
|
91
|
+
data = JSON.parse(body) as typeof data
|
|
92
|
+
} catch {
|
|
93
|
+
throw new Error(`qq getAppAccessToken: HTTP ${res.status} ${body.slice(0, 200)}`)
|
|
94
|
+
}
|
|
95
|
+
if (!res.ok || !data.access_token) {
|
|
96
|
+
throw new Error(`qq getAppAccessToken: HTTP ${res.status} ${data.message ?? 'no token'}`)
|
|
97
|
+
}
|
|
98
|
+
accessToken = data.access_token
|
|
99
|
+
const ttl = Math.max(60, (data.expires_in ?? 7200) - 300)
|
|
100
|
+
tokenExpireAt = Date.now() + ttl * 1000
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function qqFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
|
104
|
+
const request = () => fetch(`${API}${path}`, {
|
|
105
|
+
...init,
|
|
106
|
+
headers: {
|
|
107
|
+
Authorization: `QQBot ${accessToken}`,
|
|
108
|
+
'content-type': 'application/json',
|
|
106
109
|
...(init?.headers ?? {}),
|
|
107
110
|
},
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
targets.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
heartbeat
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
stableTimer
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (!
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
heartbeat
|
|
234
|
-
|
|
235
|
-
stableTimer
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
111
|
+
signal: timeoutSignal(30_000, lifecycle?.signal),
|
|
112
|
+
})
|
|
113
|
+
await ensureToken()
|
|
114
|
+
let res = await request()
|
|
115
|
+
if (res.status === 401) {
|
|
116
|
+
// token 可能被吊销或时钟漂移提前过期:清缓存强制重取后再试一次
|
|
117
|
+
accessToken = ''
|
|
118
|
+
tokenExpireAt = 0
|
|
119
|
+
await ensureToken()
|
|
120
|
+
res = await request()
|
|
121
|
+
}
|
|
122
|
+
if (!res.ok) {
|
|
123
|
+
const body = await res.text().catch(() => '')
|
|
124
|
+
throw new Error(`qq ${path}: HTTP ${res.status} ${body.slice(0, 200)}`)
|
|
125
|
+
}
|
|
126
|
+
return res.json() as Promise<T>
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function remember(chatId: string, kind: 'dm' | 'group', messageId?: string): void {
|
|
130
|
+
const prev = targets.get(chatId)
|
|
131
|
+
targets.set(chatId, {
|
|
132
|
+
kind,
|
|
133
|
+
lastMsgId: messageId || prev?.lastMsgId,
|
|
134
|
+
seq: prev?.seq ?? 0,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function connect(): Promise<void> {
|
|
139
|
+
if (stopped) return
|
|
140
|
+
await ensureToken()
|
|
141
|
+
if (stopped) return
|
|
142
|
+
const { url } = await qqFetch<{ url: string }>(GATEWAY_PATH)
|
|
143
|
+
if (!url) throw new Error('qq gateway: missing websocket url')
|
|
144
|
+
if (stopped) return
|
|
145
|
+
|
|
146
|
+
const socket = new WebSocket(url)
|
|
147
|
+
ws = socket
|
|
148
|
+
statusText = '连接中'
|
|
149
|
+
socket.onopen = () => {
|
|
150
|
+
if (ws === socket) statusText = '等待网关握手'
|
|
151
|
+
}
|
|
152
|
+
socket.onmessage = (ev) => {
|
|
153
|
+
let payload: GatewayPayload
|
|
154
|
+
try {
|
|
155
|
+
payload = JSON.parse(String(ev.data)) as GatewayPayload
|
|
156
|
+
} catch {
|
|
157
|
+
log('[qq] 收到无法解析的网关消息')
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
if (payload.s !== undefined) seq = payload.s
|
|
161
|
+
switch (payload.op) {
|
|
162
|
+
case 10: {
|
|
163
|
+
const hello = payload.d as { heartbeat_interval: number }
|
|
164
|
+
socket.send(JSON.stringify({
|
|
165
|
+
op: 2,
|
|
166
|
+
d: {
|
|
167
|
+
token: `QQBot ${accessToken}`,
|
|
168
|
+
intents: GROUP_AND_C2C_INTENT,
|
|
169
|
+
shard: [0, 1],
|
|
170
|
+
},
|
|
171
|
+
}))
|
|
172
|
+
clearInterval(heartbeat)
|
|
173
|
+
heartbeat = setInterval(() => {
|
|
174
|
+
if (ws === socket) socket.send(JSON.stringify({ op: 1, d: seq }))
|
|
175
|
+
}, hello.heartbeat_interval)
|
|
176
|
+
statusText = '鉴权中'
|
|
177
|
+
log('[qq] 已收到 Hello,正在鉴权')
|
|
178
|
+
break
|
|
179
|
+
}
|
|
180
|
+
case 0: {
|
|
181
|
+
const t = payload.t
|
|
182
|
+
if (t === 'READY') {
|
|
183
|
+
clearTimeout(stableTimer)
|
|
184
|
+
stableTimer = setTimeout(() => { reconnectAttempts = 0 }, 60_000)
|
|
185
|
+
statusText = '已连接'
|
|
186
|
+
log('[qq] 网关就绪')
|
|
187
|
+
break
|
|
188
|
+
}
|
|
189
|
+
if (t === 'C2C_MESSAGE_CREATE' || t === 'GROUP_AT_MESSAGE_CREATE') {
|
|
190
|
+
const msg = payload.d as QQMessage
|
|
191
|
+
if (!msg?.author) return
|
|
192
|
+
const text = cleanQqText(msg.content ?? '')
|
|
193
|
+
const isGroup = t === 'GROUP_AT_MESSAGE_CREATE'
|
|
194
|
+
const userId = isGroup
|
|
195
|
+
? (msg.author.member_openid ?? msg.author.id)
|
|
196
|
+
: (msg.author.user_openid ?? msg.author.id)
|
|
197
|
+
const chatId = isGroup
|
|
198
|
+
// 群 chatId 带 g: 前缀自描述类型,渠道重启丢内存后发送仍能路由回群接口
|
|
199
|
+
? `g:${msg.group_openid ?? ''}`
|
|
200
|
+
: (msg.author.user_openid ?? msg.author.id ?? '')
|
|
201
|
+
if (!chatId || !userId) return
|
|
202
|
+
if (!text) {
|
|
203
|
+
// 纯图片/文件消息静默丢弃会让用户以为机器人没收到,回一条文字提示
|
|
204
|
+
if (Array.isArray(msg.attachments) && msg.attachments.length > 0) {
|
|
205
|
+
remember(chatId, isGroup ? 'group' : 'dm', msg.id)
|
|
206
|
+
void sendText(chatId, '暂不支持图片/文件,请发送文字。').catch((error) => {
|
|
207
|
+
log(`[qq] 媒体提示发送失败: ${error instanceof Error ? error.message : String(error)}`)
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
remember(chatId, isGroup ? 'group' : 'dm', msg.id)
|
|
213
|
+
void handler?.({
|
|
214
|
+
chatId,
|
|
215
|
+
userId,
|
|
216
|
+
username: msg.author.username,
|
|
217
|
+
text,
|
|
218
|
+
kind: isGroup ? 'group' : 'dm',
|
|
219
|
+
addressed: true,
|
|
220
|
+
messageId: msg.id,
|
|
221
|
+
context: { messageId: msg.id, messageType: isGroup ? 'group' : 'c2c' },
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
break
|
|
225
|
+
}
|
|
226
|
+
case 7:
|
|
227
|
+
// 服务端要求重连:立即断开旧连接,交给 onclose 走统一重连,不能只改状态等对端关
|
|
228
|
+
log('[qq] 网关要求重连,主动断开旧连接')
|
|
229
|
+
statusText = '重连中'
|
|
230
|
+
socket.close(4000, 'reconnect')
|
|
231
|
+
break
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
socket.onclose = (ev) => {
|
|
235
|
+
if (ws !== socket) return
|
|
236
|
+
clearInterval(heartbeat)
|
|
237
|
+
heartbeat = undefined
|
|
238
|
+
clearTimeout(stableTimer)
|
|
239
|
+
stableTimer = undefined
|
|
240
|
+
ws = undefined
|
|
241
|
+
statusText = `已断开(code ${ev.code})`
|
|
242
|
+
if (!stopped) {
|
|
243
|
+
const detail = ev.code === 4004 ? ':鉴权失败,将刷新 AccessToken' : ''
|
|
244
|
+
log(`[qq] 连接断开(${ev.code}${detail})`)
|
|
245
|
+
scheduleReconnect()
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
socket.onerror = () => {
|
|
249
|
+
if (ws === socket) statusText = '连接错误'
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function sendText(chatId: string, text: string): Promise<void> {
|
|
254
|
+
const target = targets.get(chatId)
|
|
255
|
+
const kind = target?.kind ?? (chatId.startsWith('g:') ? 'group' : 'dm')
|
|
256
|
+
const openid = kind === 'group' ? chatId.replace(/^g:/, '') : chatId
|
|
257
|
+
const nextSeq = (target?.seq ?? 0) + 1
|
|
258
|
+
if (target) target.seq = nextSeq
|
|
259
|
+
else remember(chatId, kind)
|
|
260
|
+
const body: Record<string, unknown> = { content: text, msg_type: 0, msg_seq: nextSeq }
|
|
261
|
+
if (target?.lastMsgId) body.msg_id = target.lastMsgId
|
|
262
|
+
const path = kind === 'group'
|
|
263
|
+
? `/v2/groups/${openid}/messages`
|
|
264
|
+
: `/v2/users/${openid}/messages`
|
|
265
|
+
try {
|
|
266
|
+
await qqFetch(path, { method: 'POST', body: JSON.stringify(body) })
|
|
267
|
+
} catch (error) {
|
|
268
|
+
if (!target?.lastMsgId) {
|
|
269
|
+
throw new Error(`QQ 发送失败,当前没有可用的被动回复 msg_id;请让用户重新发送一条消息。${error instanceof Error ? ` ${error.message}` : ''}`, { cause: error })
|
|
242
270
|
}
|
|
271
|
+
throw error
|
|
243
272
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const target = targets.get(chatId)
|
|
251
|
-
const kind = target?.kind ?? (chatId.startsWith('g:') ? 'group' : 'dm')
|
|
252
|
-
const openid = kind === 'group' ? chatId.replace(/^g:/, '') : chatId
|
|
253
|
-
const nextSeq = (target?.seq ?? 0) + 1
|
|
254
|
-
if (target) target.seq = nextSeq
|
|
255
|
-
else remember(chatId, kind)
|
|
256
|
-
const body: Record<string, unknown> = { content: text, msg_type: 0, msg_seq: nextSeq }
|
|
257
|
-
if (target?.lastMsgId) body.msg_id = target.lastMsgId
|
|
258
|
-
const path = kind === 'group'
|
|
259
|
-
? `/v2/groups/${openid}/messages`
|
|
260
|
-
: `/v2/users/${openid}/messages`
|
|
261
|
-
await qqFetch(path, { method: 'POST', body: JSON.stringify(body) })
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return {
|
|
265
|
-
id: 'qq',
|
|
266
|
-
label: 'QQ',
|
|
267
|
-
maxMessageLength: 2000,
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
id: 'qq',
|
|
277
|
+
label: 'QQ',
|
|
278
|
+
maxMessageLength: 2000,
|
|
268
279
|
async start() {
|
|
269
280
|
if (!stopped && (ws || reconnectTimer)) return
|
|
281
|
+
lifecycle?.abort()
|
|
282
|
+
lifecycle = new AbortController()
|
|
270
283
|
stopped = false
|
|
271
|
-
reconnectAttempts = 0
|
|
272
|
-
try {
|
|
273
|
-
await connect()
|
|
274
|
-
} catch (err) {
|
|
275
|
-
statusText = '连接失败'
|
|
276
|
-
log(`[qq] 连接失败: ${err instanceof Error ? err.message : String(err)}`)
|
|
277
|
-
scheduleReconnect()
|
|
278
|
-
}
|
|
279
|
-
},
|
|
284
|
+
reconnectAttempts = 0
|
|
285
|
+
try {
|
|
286
|
+
await connect()
|
|
287
|
+
} catch (err) {
|
|
288
|
+
statusText = '连接失败'
|
|
289
|
+
log(`[qq] 连接失败: ${err instanceof Error ? err.message : String(err)}`)
|
|
290
|
+
scheduleReconnect()
|
|
291
|
+
}
|
|
292
|
+
},
|
|
280
293
|
async stop() {
|
|
281
294
|
stopped = true
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
clearTimeout(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
295
|
+
lifecycle?.abort()
|
|
296
|
+
lifecycle = undefined
|
|
297
|
+
clearInterval(heartbeat)
|
|
298
|
+
clearTimeout(reconnectTimer)
|
|
299
|
+
reconnectTimer = undefined
|
|
300
|
+
clearTimeout(stableTimer)
|
|
301
|
+
stableTimer = undefined
|
|
302
|
+
ws?.close(1000, 'shutdown')
|
|
303
|
+
ws = undefined
|
|
304
|
+
},
|
|
305
|
+
async send(chatId, text) {
|
|
306
|
+
await sendText(chatId, text)
|
|
307
|
+
},
|
|
308
|
+
async beginReply(chatId): Promise<ReplyStream> {
|
|
309
|
+
return {
|
|
310
|
+
async update() { /* QQ 不能按 Telegram 那样原地改气泡,避免每个 token 新发一条 */ },
|
|
311
|
+
async finish(text) { await sendText(chatId, text) },
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
setMessageHandler(h) { handler = h },
|
|
315
|
+
status() { return statusText },
|
|
316
|
+
}
|
|
317
|
+
}
|