@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/wecom.ts
CHANGED
|
@@ -1,203 +1,235 @@
|
|
|
1
|
-
import type { ChannelAdapter, ImMessage, ReplyStream } from '../engine/types.js'
|
|
2
|
-
import { quietSdkLogger } from '../engine/quiet-logger.js'
|
|
3
|
-
|
|
4
|
-
export interface WecomConfig {
|
|
5
|
-
botId?: string
|
|
6
|
-
secret?: string
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface WecomSdkClient {
|
|
10
|
-
replyStream(frame: unknown, streamId: string, content: string, finish?: boolean): Promise<unknown>
|
|
11
|
-
sendMessage(chatId: string, body: unknown): Promise<unknown>
|
|
12
|
-
connect(): unknown
|
|
13
|
-
disconnect(): void
|
|
14
|
-
on(event: string, fn: (payload?: unknown) => void): void
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function frameBody(frame: unknown): Record<string, unknown> {
|
|
18
|
-
const body = (frame as { body?: Record<string, unknown> }).body
|
|
19
|
-
return body && typeof body === 'object' ? body : {}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function messageText(body: Record<string, unknown>): string {
|
|
23
|
-
if (body.msgtype === 'text') return String((body.text as { content?: string } | undefined)?.content ?? '').trim()
|
|
24
|
-
if (body.msgtype === 'voice') return String((body.voice as { content?: string } | undefined)?.content ?? '').trim()
|
|
25
|
-
const mixed = body.mixed as { msg_item?: Array<{ msgtype?: string; text?: { content?: string } }> } | undefined
|
|
26
|
-
if (body.msgtype === 'mixed' && Array.isArray(mixed?.msg_item)) {
|
|
27
|
-
return mixed.msg_item
|
|
28
|
-
.filter((item) => item?.msgtype === 'text' && item.text?.content)
|
|
29
|
-
.map((item) => String(item.text?.content ?? ''))
|
|
30
|
-
.join('\n')
|
|
31
|
-
.trim()
|
|
32
|
-
}
|
|
33
|
-
return ''
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** 企业微信智能机器人必须按回调帧 replyStream,主动 sendMessage 用户看不到。 */
|
|
1
|
+
import type { ChannelAdapter, ImMessage, ReplyStream } from '../engine/types.js'
|
|
2
|
+
import { quietSdkLogger } from '../engine/quiet-logger.js'
|
|
3
|
+
|
|
4
|
+
export interface WecomConfig {
|
|
5
|
+
botId?: string
|
|
6
|
+
secret?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface WecomSdkClient {
|
|
10
|
+
replyStream(frame: unknown, streamId: string, content: string, finish?: boolean): Promise<unknown>
|
|
11
|
+
sendMessage(chatId: string, body: unknown): Promise<unknown>
|
|
12
|
+
connect(): unknown
|
|
13
|
+
disconnect(): void
|
|
14
|
+
on(event: string, fn: (payload?: unknown) => void): void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function frameBody(frame: unknown): Record<string, unknown> {
|
|
18
|
+
const body = (frame as { body?: Record<string, unknown> }).body
|
|
19
|
+
return body && typeof body === 'object' ? body : {}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function messageText(body: Record<string, unknown>): string {
|
|
23
|
+
if (body.msgtype === 'text') return String((body.text as { content?: string } | undefined)?.content ?? '').trim()
|
|
24
|
+
if (body.msgtype === 'voice') return String((body.voice as { content?: string } | undefined)?.content ?? '').trim()
|
|
25
|
+
const mixed = body.mixed as { msg_item?: Array<{ msgtype?: string; text?: { content?: string } }> } | undefined
|
|
26
|
+
if (body.msgtype === 'mixed' && Array.isArray(mixed?.msg_item)) {
|
|
27
|
+
return mixed.msg_item
|
|
28
|
+
.filter((item) => item?.msgtype === 'text' && item.text?.content)
|
|
29
|
+
.map((item) => String(item.text?.content ?? ''))
|
|
30
|
+
.join('\n')
|
|
31
|
+
.trim()
|
|
32
|
+
}
|
|
33
|
+
return ''
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 企业微信智能机器人必须按回调帧 replyStream,主动 sendMessage 用户看不到。 */
|
|
37
37
|
export class WecomReplyBroker {
|
|
38
38
|
// 同一聊天可能连续来多条消息,每条都有独立的回调帧,必须排队而不是单槽覆盖
|
|
39
|
-
private readonly pending = new Map<string, Array<{ frame: unknown; streamId: string; started: boolean }>>()
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
private readonly pending = new Map<string, Array<{ frame: unknown; streamId: string; started: boolean; expiresAt: number }>>()
|
|
40
|
+
private readonly sweepTimer: ReturnType<typeof setInterval>
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
42
43
|
private readonly client: Pick<WecomSdkClient, 'replyStream' | 'sendMessage'>,
|
|
43
44
|
private readonly log: (line: string) => void,
|
|
44
45
|
private readonly newStreamId: () => string = () => `stream_${Date.now()}_${Math.random().toString(16).slice(2, 10)}`,
|
|
45
|
-
|
|
46
|
+
private readonly ttlMs = 120_000,
|
|
47
|
+
) {
|
|
48
|
+
this.sweepTimer = setInterval(() => this.pruneAll(), Math.max(1000, Math.min(ttlMs, 30_000)))
|
|
49
|
+
this.sweepTimer.unref?.()
|
|
50
|
+
}
|
|
46
51
|
|
|
52
|
+
private prune(chatId: string, now = Date.now()): void {
|
|
53
|
+
const list = this.pending.get(chatId)?.filter((item) => item.expiresAt > now) ?? []
|
|
54
|
+
if (list.length > 0) this.pending.set(chatId, list)
|
|
55
|
+
else this.pending.delete(chatId)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private pruneAll(): void {
|
|
59
|
+
const now = Date.now()
|
|
60
|
+
for (const chatId of this.pending.keys()) this.prune(chatId, now)
|
|
61
|
+
}
|
|
62
|
+
|
|
47
63
|
remember(chatId: string, frame: unknown): string {
|
|
64
|
+
this.prune(chatId)
|
|
48
65
|
const list = this.pending.get(chatId) ?? []
|
|
49
66
|
const streamId = this.newStreamId()
|
|
50
|
-
list.push({ frame, streamId, started: false })
|
|
67
|
+
list.push({ frame, streamId, started: false, expiresAt: Date.now() + this.ttlMs })
|
|
68
|
+
// 单个聊天异常突发时也要有硬上限,避免 TTL 窗口内无限增长。
|
|
69
|
+
if (list.length > 20) list.splice(0, list.length - 20)
|
|
51
70
|
this.pending.set(chatId, list)
|
|
52
|
-
return streamId
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private shift(chatId: string): { frame: unknown; streamId: string; started: boolean } | undefined {
|
|
71
|
+
return streamId
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private shift(chatId: string): { frame: unknown; streamId: string; started: boolean; expiresAt: number } | undefined {
|
|
75
|
+
this.prune(chatId)
|
|
56
76
|
const list = this.pending.get(chatId)
|
|
57
77
|
if (!list?.length) return undefined
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
78
|
+
const item = list.shift()
|
|
79
|
+
if (list.length === 0) this.pending.delete(chatId)
|
|
80
|
+
return item
|
|
81
|
+
}
|
|
82
|
+
|
|
61
83
|
async startThinking(chatId: string): Promise<void> {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
item.
|
|
66
|
-
|
|
84
|
+
this.prune(chatId)
|
|
85
|
+
for (const item of this.pending.get(chatId) ?? []) {
|
|
86
|
+
if (item.started) continue
|
|
87
|
+
await this.client.replyStream(item.frame, item.streamId, '正在思考中…', false)
|
|
88
|
+
item.started = true
|
|
89
|
+
}
|
|
67
90
|
}
|
|
68
91
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.log(`[wecom] 已通过回调回复 ${chatId}`)
|
|
75
|
-
return
|
|
76
|
-
} catch (error) {
|
|
77
|
-
this.log(`[wecom] 回调回复失败,改走主动推送:${error instanceof Error ? error.message : String(error)}`)
|
|
78
|
-
// 帧先放回去,后续回复仍可尝试回调通道
|
|
79
|
-
const list = this.pending.get(chatId)
|
|
80
|
-
if (list) list.unshift(item)
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
await this.client.sendMessage(chatId, { msgtype: 'markdown', markdown: { content: text } })
|
|
84
|
-
this.log(`[wecom] 已主动推送 ${chatId}`)
|
|
92
|
+
pendingCount(): number {
|
|
93
|
+
this.pruneAll()
|
|
94
|
+
let count = 0
|
|
95
|
+
for (const list of this.pending.values()) count += list.length
|
|
96
|
+
return count
|
|
85
97
|
}
|
|
86
98
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (!item.started) {
|
|
91
|
-
await this.client.replyStream(item.frame, item.streamId, '正在思考中…', false)
|
|
92
|
-
item.started = true
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
// 企业微信客户端会把未完成分片渲染成一条条气泡,这里只收最终全文。
|
|
96
|
-
update: async () => undefined,
|
|
97
|
-
finish: async (text) => {
|
|
98
|
-
try {
|
|
99
|
-
await this.client.replyStream(item.frame, item.streamId, text, true)
|
|
100
|
-
} catch (error) {
|
|
101
|
-
this.log(`[wecom] 回调收口失败,改走主动推送:${error instanceof Error ? error.message : String(error)}`)
|
|
102
|
-
await this.client.sendMessage(chatId, { msgtype: 'markdown', markdown: { content: text } })
|
|
103
|
-
}
|
|
104
|
-
this.log(`[wecom] 已通过回调回复 ${chatId}`)
|
|
105
|
-
},
|
|
106
|
-
}
|
|
99
|
+
dispose(): void {
|
|
100
|
+
clearInterval(this.sweepTimer)
|
|
101
|
+
this.pending.clear()
|
|
107
102
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
let statusText = '未连接'
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
id: 'wecom',
|
|
122
|
-
label: '企业微信',
|
|
123
|
-
maxMessageLength: 4000,
|
|
124
|
-
skipMerge: true,
|
|
125
|
-
async start() {
|
|
126
|
-
let sdk: {
|
|
127
|
-
WSClient: new (opts: { botId: string; secret: string; maxAuthFailureAttempts?: number; logger?: { debug: Function; info: Function; warn: Function; error: Function } }) => WecomSdkClient
|
|
128
|
-
generateReqId?: (prefix: string) => string
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
sdk = await import('@wecom/aibot-node-sdk') as typeof sdk
|
|
132
|
-
} catch {
|
|
133
|
-
throw new Error('缺少依赖 @wecom/aibot-node-sdk')
|
|
103
|
+
|
|
104
|
+
async send(chatId: string, text: string): Promise<void> {
|
|
105
|
+
const item = this.shift(chatId)
|
|
106
|
+
if (item) {
|
|
107
|
+
try {
|
|
108
|
+
await this.client.replyStream(item.frame, item.streamId, text, true)
|
|
109
|
+
this.log(`[wecom] 已通过回调回复 ${chatId}`)
|
|
110
|
+
return
|
|
111
|
+
} catch (error) {
|
|
112
|
+
this.log(`[wecom] 回调回复失败,改走主动推送:${error instanceof Error ? error.message : String(error)}`)
|
|
134
113
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
114
|
+
}
|
|
115
|
+
await this.client.sendMessage(chatId, { msgtype: 'markdown', markdown: { content: text } })
|
|
116
|
+
this.log(`[wecom] 已主动推送 ${chatId}`)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async beginReply(chatId: string): Promise<ReplyStream> {
|
|
120
|
+
const item = this.shift(chatId)
|
|
121
|
+
if (!item) throw new Error('wecom: 没有待回复的回调帧')
|
|
122
|
+
if (!item.started) {
|
|
123
|
+
await this.client.replyStream(item.frame, item.streamId, '正在思考中…', false)
|
|
124
|
+
item.started = true
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
// 企业微信客户端会把未完成分片渲染成一条条气泡,这里只收最终全文。
|
|
128
|
+
update: async () => undefined,
|
|
129
|
+
finish: async (text) => {
|
|
130
|
+
try {
|
|
131
|
+
await this.client.replyStream(item.frame, item.streamId, text, true)
|
|
132
|
+
} catch (error) {
|
|
133
|
+
this.log(`[wecom] 回调收口失败,改走主动推送:${error instanceof Error ? error.message : String(error)}`)
|
|
134
|
+
await this.client.sendMessage(chatId, { msgtype: 'markdown', markdown: { content: text } })
|
|
135
|
+
}
|
|
136
|
+
this.log(`[wecom] 已通过回调回复 ${chatId}`)
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function createWecomChannel(config: WecomConfig, log: (line: string) => void): ChannelAdapter | undefined {
|
|
143
|
+
const botId = config.botId?.trim()
|
|
144
|
+
const secret = config.secret?.trim()
|
|
145
|
+
if (!botId || !secret) return undefined
|
|
146
|
+
|
|
147
|
+
let handler: ((msg: ImMessage) => void | Promise<void>) | undefined
|
|
148
|
+
let client: WecomSdkClient | undefined
|
|
149
|
+
let broker: WecomReplyBroker | undefined
|
|
150
|
+
let statusText = '未连接'
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
id: 'wecom',
|
|
154
|
+
label: '企业微信',
|
|
155
|
+
maxMessageLength: 4000,
|
|
156
|
+
skipMerge: true,
|
|
157
|
+
async start() {
|
|
158
|
+
let sdk: {
|
|
159
|
+
WSClient: new (opts: { botId: string; secret: string; maxAuthFailureAttempts?: number; logger?: { debug: Function; info: Function; warn: Function; error: Function } }) => WecomSdkClient
|
|
160
|
+
generateReqId?: (prefix: string) => string
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
sdk = await import('@wecom/aibot-node-sdk') as typeof sdk
|
|
164
|
+
} catch {
|
|
165
|
+
throw new Error('缺少依赖 @wecom/aibot-node-sdk')
|
|
166
|
+
}
|
|
167
|
+
client = new sdk.WSClient({ botId, secret, maxAuthFailureAttempts: 1, logger: quietSdkLogger(log, 'wecom') })
|
|
168
|
+
const newStreamId = sdk.generateReqId
|
|
169
|
+
? () => sdk.generateReqId!('stream')
|
|
170
|
+
: () => `stream_${Date.now()}_${Math.random().toString(16).slice(2, 10)}`
|
|
171
|
+
broker = new WecomReplyBroker(client, log, newStreamId)
|
|
172
|
+
client.on('message', (frame) => {
|
|
173
|
+
const body = frameBody(frame)
|
|
174
|
+
const chattype = String(body.chattype ?? '')
|
|
175
|
+
const from = body.from as { userid?: string } | undefined
|
|
176
|
+
const senderId = from?.userid ?? ''
|
|
177
|
+
const chatId = chattype === 'group' ? String(body.chatid ?? '') : senderId
|
|
178
|
+
const text = messageText(body)
|
|
147
179
|
if (!chatId || !text || !['single', 'group'].includes(chattype)) {
|
|
148
|
-
log(`[wecom] 忽略一帧 chattype=${chattype || '-'} msgtype=${String(body.msgtype ?? '-')}`)
|
|
180
|
+
log(`[wecom] 忽略一帧 chattype=${chattype || '-'} msgtype=${String(body.msgtype ?? '-')}`)
|
|
149
181
|
return
|
|
150
182
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
messageId: typeof body.msgid === 'string' ? body.msgid : undefined,
|
|
163
|
-
})
|
|
164
|
-
})
|
|
165
|
-
const ready = new Promise<void>((resolve, reject) => {
|
|
166
|
-
const timer = setTimeout(() => reject(new Error('企业微信连接超时')), 20_000)
|
|
167
|
-
client?.on('authenticated', () => {
|
|
168
|
-
clearTimeout(timer)
|
|
169
|
-
resolve()
|
|
170
|
-
})
|
|
171
|
-
client?.on('error', (error) => {
|
|
172
|
-
const detail = error instanceof Error ? error.message : String(error ?? 'connection-error')
|
|
173
|
-
if (/auth|unauthorized|invalid/i.test(detail)) {
|
|
174
|
-
clearTimeout(timer)
|
|
175
|
-
reject(new Error(`企业微信鉴权失败:${detail}`))
|
|
176
|
-
} else {
|
|
177
|
-
log(`[wecom] 连接异常:${detail}`)
|
|
178
|
-
}
|
|
183
|
+
// 企微长连接模式只会在群聊中 @ 当前机器人时推送回调,这里无需也无法校验 mention;
|
|
184
|
+
// 不做 text.includes('@') 兜底,避免正文不含 ASCII @ 时误丢合法消息。
|
|
185
|
+
log(`[wecom] 收到 ${chattype} ${senderId}: ${text.slice(0, 80)}`)
|
|
186
|
+
broker?.remember(chatId, frame)
|
|
187
|
+
void handler?.({
|
|
188
|
+
chatId,
|
|
189
|
+
userId: senderId,
|
|
190
|
+
text,
|
|
191
|
+
kind: chattype === 'group' ? 'group' : 'dm',
|
|
192
|
+
addressed: true,
|
|
193
|
+
messageId: typeof body.msgid === 'string' ? body.msgid : undefined,
|
|
179
194
|
})
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
195
|
+
})
|
|
196
|
+
const ready = new Promise<void>((resolve, reject) => {
|
|
197
|
+
const timer = setTimeout(() => reject(new Error('企业微信连接超时')), 20_000)
|
|
198
|
+
client?.on('authenticated', () => {
|
|
199
|
+
clearTimeout(timer)
|
|
200
|
+
resolve()
|
|
201
|
+
})
|
|
202
|
+
client?.on('error', (error) => {
|
|
203
|
+
const detail = error instanceof Error ? error.message : String(error ?? 'connection-error')
|
|
204
|
+
if (/auth|unauthorized|invalid/i.test(detail)) {
|
|
205
|
+
clearTimeout(timer)
|
|
206
|
+
reject(new Error(`企业微信鉴权失败:${detail}`))
|
|
207
|
+
} else {
|
|
208
|
+
log(`[wecom] 连接异常:${detail}`)
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
client.connect()
|
|
213
|
+
await ready
|
|
214
|
+
statusText = '长连接已建立'
|
|
215
|
+
},
|
|
216
|
+
async stop() {
|
|
186
217
|
client?.disconnect()
|
|
187
218
|
client = undefined
|
|
219
|
+
broker?.dispose()
|
|
188
220
|
broker = undefined
|
|
189
|
-
statusText = '已停止'
|
|
190
|
-
},
|
|
191
|
-
async send(chatId, text) {
|
|
192
|
-
if (!broker) throw new Error('wecom: 尚未连接')
|
|
193
|
-
await broker.send(chatId, text)
|
|
194
|
-
},
|
|
195
|
-
async sendAction(chatId) {
|
|
196
|
-
await broker?.startThinking(chatId).catch(() => undefined)
|
|
197
|
-
},
|
|
198
|
-
setMessageHandler(h) { handler = h },
|
|
199
|
-
status() { return statusText },
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
221
|
+
statusText = '已停止'
|
|
222
|
+
},
|
|
223
|
+
async send(chatId, text) {
|
|
224
|
+
if (!broker) throw new Error('wecom: 尚未连接')
|
|
225
|
+
await broker.send(chatId, text)
|
|
226
|
+
},
|
|
227
|
+
async sendAction(chatId) {
|
|
228
|
+
await broker?.startThinking(chatId).catch(() => undefined)
|
|
229
|
+
},
|
|
230
|
+
setMessageHandler(h) { handler = h },
|
|
231
|
+
status() { return statusText },
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
|