@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/dingtalk.ts
CHANGED
|
@@ -1,147 +1,151 @@
|
|
|
1
|
-
import type { ChannelAdapter, ImMessage, ReplyStream } from '../engine/types.js'
|
|
1
|
+
import type { ChannelAdapter, ImMessage, ReplyStream } from '../engine/types.js'
|
|
2
2
|
import { DingtalkCardClient, openDingtalkCardStream, type CardTarget } from './dingtalk-card.js'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
let
|
|
41
|
-
let
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
map.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
client.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (parsed?.chatId)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
client
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
3
|
+
import { timeoutSignal } from '../engine/abort.js'
|
|
4
|
+
|
|
5
|
+
export interface DingtalkConfig {
|
|
6
|
+
clientId?: string
|
|
7
|
+
clientSecret?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function parseDingtalkRobotEvent(payload: {
|
|
11
|
+
text?: { content?: string }
|
|
12
|
+
senderStaffId?: string
|
|
13
|
+
senderId?: string
|
|
14
|
+
conversationId?: string
|
|
15
|
+
conversationType?: string
|
|
16
|
+
sessionWebhook?: string
|
|
17
|
+
msgId?: string
|
|
18
|
+
msgid?: string
|
|
19
|
+
msgIdEnc?: string
|
|
20
|
+
}): { chatId: string; userId: string; text: string; kind: 'dm' | 'group'; messageId?: string } | undefined {
|
|
21
|
+
const text = payload.text?.content?.trim() ?? ''
|
|
22
|
+
const sender = payload.senderStaffId ?? payload.senderId ?? ''
|
|
23
|
+
const group = String(payload.conversationType) === '2'
|
|
24
|
+
const chatId = group ? (payload.conversationId ?? '') : sender
|
|
25
|
+
if (!chatId) return undefined
|
|
26
|
+
const messageId = payload.msgId || payload.msgid || payload.msgIdEnc
|
|
27
|
+
return {
|
|
28
|
+
chatId,
|
|
29
|
+
userId: sender,
|
|
30
|
+
text,
|
|
31
|
+
kind: group ? 'group' : 'dm',
|
|
32
|
+
messageId,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function createDingtalkChannel(config: DingtalkConfig, log: (line: string) => void): ChannelAdapter | undefined {
|
|
36
|
+
const clientId = config.clientId?.trim()
|
|
37
|
+
const clientSecret = config.clientSecret?.trim()
|
|
38
|
+
if (!clientId || !clientSecret) return undefined
|
|
39
|
+
|
|
40
|
+
let handler: ((msg: ImMessage) => void | Promise<void>) | undefined
|
|
41
|
+
let client: { connect(): Promise<void>; disconnect(): void; registerCallbackListener(topic: string, cb: (res: { data: string }) => unknown): void } | undefined
|
|
42
|
+
let statusText = '未连接'
|
|
43
|
+
const webhooks = new Map<string, string>()
|
|
44
|
+
const targets = new Map<string, CardTarget>()
|
|
45
|
+
const cards = new DingtalkCardClient(clientId, clientSecret, log)
|
|
46
|
+
const remember = <T>(map: Map<string, T>, key: string, value: T) => {
|
|
47
|
+
map.delete(key)
|
|
48
|
+
map.set(key, value)
|
|
49
|
+
if (map.size > 1000) {
|
|
50
|
+
const oldest = map.keys().next().value
|
|
51
|
+
if (oldest !== undefined) map.delete(oldest)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
id: 'dingtalk',
|
|
57
|
+
label: '钉钉',
|
|
58
|
+
maxMessageLength: 4000,
|
|
59
|
+
async start() {
|
|
60
|
+
try {
|
|
61
|
+
const sdk = await import('dingtalk-stream') as {
|
|
62
|
+
DWClient: new (opts: Record<string, unknown>) => NonNullable<typeof client>
|
|
63
|
+
TOPIC_ROBOT: string
|
|
64
|
+
}
|
|
65
|
+
client = new sdk.DWClient({ clientId, clientSecret, autoReconnect: true })
|
|
66
|
+
client.registerCallbackListener(sdk.TOPIC_ROBOT, (res) => {
|
|
67
|
+
let payload: {
|
|
68
|
+
text?: { content?: string }
|
|
69
|
+
senderStaffId?: string
|
|
70
|
+
senderId?: string
|
|
71
|
+
conversationId?: string
|
|
72
|
+
conversationType?: string
|
|
73
|
+
sessionWebhook?: string
|
|
74
|
+
}
|
|
75
|
+
try { payload = JSON.parse(res.data) as typeof payload } catch { return }
|
|
76
|
+
const parsed = parseDingtalkRobotEvent(payload)
|
|
77
|
+
if (payload.sessionWebhook && parsed?.chatId) remember(webhooks, parsed.chatId, payload.sessionWebhook)
|
|
78
|
+
if (parsed?.chatId) {
|
|
79
|
+
remember(targets, parsed.chatId, parsed.kind === 'group'
|
|
80
|
+
? { type: 'group', openConversationId: payload.conversationId ?? parsed.chatId }
|
|
81
|
+
: { type: 'user', userId: parsed.userId })
|
|
82
|
+
}
|
|
83
|
+
if (!parsed?.chatId || !parsed.text) return { status: 'SUCCESS' }
|
|
84
|
+
void handler?.({
|
|
85
|
+
chatId: parsed.chatId,
|
|
86
|
+
userId: parsed.userId,
|
|
87
|
+
text: parsed.text,
|
|
88
|
+
kind: parsed.kind,
|
|
89
|
+
addressed: true,
|
|
90
|
+
messageId: parsed.messageId,
|
|
91
|
+
})
|
|
92
|
+
return { status: 'SUCCESS' }
|
|
93
|
+
})
|
|
94
|
+
await client.connect()
|
|
95
|
+
statusText = 'Stream 已连接'
|
|
96
|
+
log('[dingtalk] Stream 已连接')
|
|
97
|
+
} catch (error) {
|
|
98
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
99
|
+
const missing = /Cannot find package ['"]dingtalk-stream['"]/i.test(message)
|
|
100
|
+
throw new Error(missing ? '缺少依赖 dingtalk-stream' : `钉钉连接失败: ${message}`)
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
async stop() {
|
|
104
|
+
client?.disconnect()
|
|
105
|
+
client = undefined
|
|
106
|
+
webhooks.clear()
|
|
107
|
+
targets.clear()
|
|
108
|
+
statusText = '已停止'
|
|
109
|
+
},
|
|
110
|
+
async send(chatId, text) {
|
|
111
|
+
const webhook = webhooks.get(chatId)
|
|
112
|
+
if (!webhook) throw new Error('dingtalk: 没有可回复的 webhook,请先在钉钉里发一条消息')
|
|
113
|
+
const res = await fetch(webhook, {
|
|
114
|
+
method: 'POST',
|
|
114
115
|
headers: { 'content-type': 'application/json' },
|
|
115
116
|
body: JSON.stringify({ msgtype: 'markdown', markdown: { title: 'IM助理', text } }),
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
117
|
+
signal: timeoutSignal(30_000),
|
|
118
|
+
})
|
|
119
|
+
if (!res.ok) throw new Error(`dingtalk send HTTP ${res.status}`)
|
|
120
|
+
},
|
|
121
|
+
async beginReply(chatId): Promise<ReplyStream> {
|
|
122
|
+
const target = targets.get(chatId)
|
|
123
|
+
if (!target) throw new Error('dingtalk: 还没有卡片投放目标')
|
|
124
|
+
try {
|
|
125
|
+
return await openDingtalkCardStream(cards, target, log)
|
|
126
|
+
} catch (error) {
|
|
127
|
+
log(`[dingtalk] AI Card 创建失败,回退普通文本: ${error instanceof Error ? error.message : String(error)}`)
|
|
128
|
+
const sendText = async (text: string) => {
|
|
129
|
+
const webhook = webhooks.get(chatId)
|
|
130
|
+
if (!webhook) throw new Error('dingtalk: 没有可回复的 webhook')
|
|
131
|
+
const res = await fetch(webhook, {
|
|
130
132
|
method: 'POST',
|
|
131
133
|
headers: { 'content-type': 'application/json' },
|
|
132
134
|
body: JSON.stringify({ msgtype: 'markdown', markdown: { title: 'IM助理', text } }),
|
|
135
|
+
signal: timeoutSignal(30_000),
|
|
133
136
|
})
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
async
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
137
|
+
if (!res.ok) throw new Error(`dingtalk send HTTP ${res.status}`)
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
async update() { /* 普通文本无法中途改 */ },
|
|
141
|
+
async finish(text) { await sendText(text) },
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
setMessageHandler(h) { handler = h },
|
|
146
|
+
status() { return statusText },
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
package/src/channels/feishu.ts
CHANGED
|
@@ -1,81 +1,107 @@
|
|
|
1
|
-
import type { ChannelAdapter, ImMessage } from '../engine/types.js'
|
|
2
|
-
import { quietSdkLogger } from '../engine/quiet-logger.js'
|
|
3
|
-
|
|
1
|
+
import type { ChannelAdapter, ImMessage } from '../engine/types.js'
|
|
2
|
+
import { quietSdkLogger } from '../engine/quiet-logger.js'
|
|
3
|
+
|
|
4
4
|
export interface FeishuConfig {
|
|
5
|
-
appId?: string
|
|
6
|
-
appSecret?: string
|
|
7
|
-
domain?: 'feishu' | 'lark'
|
|
5
|
+
appId?: string
|
|
6
|
+
appSecret?: string
|
|
7
|
+
domain?: 'feishu' | 'lark'
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
let handler: ((msg: ImMessage) => void | Promise<void>) | undefined
|
|
16
|
-
let client: { im: { message: { create(opts: unknown): Promise<unknown> } } } | undefined
|
|
17
|
-
let ws: { close(opts?: { force?: boolean }): void } | undefined
|
|
18
|
-
let statusText = '未连接'
|
|
19
|
-
const label = id === 'lark' ? 'Lark' : '飞书'
|
|
10
|
+
interface FeishuMention {
|
|
11
|
+
key?: string
|
|
12
|
+
id?: { open_id?: string }
|
|
13
|
+
}
|
|
20
14
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
15
|
+
/** 群消息只有明确 mention 当前机器人本身才算 addressed;@ 其他成员不触发。 */
|
|
16
|
+
export function isFeishuBotMentioned(mentions: FeishuMention[] | undefined, botOpenId: string): boolean {
|
|
17
|
+
if (!botOpenId || !Array.isArray(mentions)) return false
|
|
18
|
+
return mentions.some((mention) => mention.id?.open_id === botOpenId)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createFeishuChannel(id: 'feishu' | 'lark', config: FeishuConfig, log: (line: string) => void): ChannelAdapter | undefined {
|
|
22
|
+
const appId = config.appId?.trim()
|
|
23
|
+
const appSecret = config.appSecret?.trim()
|
|
24
|
+
if (!appId || !appSecret) return undefined
|
|
25
|
+
|
|
26
|
+
let handler: ((msg: ImMessage) => void | Promise<void>) | undefined
|
|
27
|
+
let client: {
|
|
28
|
+
request(opts: { url: string; method: 'GET' }): Promise<unknown>
|
|
29
|
+
im: { message: { create(opts: unknown): Promise<unknown> } }
|
|
30
|
+
} | undefined
|
|
31
|
+
let ws: { close(opts?: { force?: boolean }): void } | undefined
|
|
32
|
+
let statusText = '未连接'
|
|
33
|
+
const label = id === 'lark' ? 'Lark' : '飞书'
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
label,
|
|
38
|
+
maxMessageLength: 4000,
|
|
39
|
+
async start() {
|
|
40
|
+
let sdk: typeof import('@larksuiteoapi/node-sdk')
|
|
41
|
+
try {
|
|
42
|
+
sdk = await import('@larksuiteoapi/node-sdk')
|
|
43
|
+
} catch {
|
|
44
|
+
throw new Error('缺少依赖 @larksuiteoapi/node-sdk')
|
|
45
|
+
}
|
|
46
|
+
const domain = id === 'lark' || config.domain === 'lark' ? 'https://open.larksuite.com' : undefined
|
|
47
|
+
const logger = quietSdkLogger(log, id)
|
|
34
48
|
const loggerLevel = sdk.LoggerLevel?.error ?? 1
|
|
35
49
|
client = new sdk.Client({ appId, appSecret, logger, loggerLevel, ...(domain ? { domain } : {}) }) as unknown as typeof client
|
|
50
|
+
const identity = await client!.request({ url: '/open-apis/bot/v3/info', method: 'GET' }) as {
|
|
51
|
+
bot?: { open_id?: string }
|
|
52
|
+
}
|
|
53
|
+
const botOpenId = identity.bot?.open_id?.trim() ?? ''
|
|
54
|
+
if (!botOpenId) throw new Error(`${id}: 无法获取机器人 open_id`)
|
|
36
55
|
const dispatcher = new sdk.EventDispatcher({}).register({
|
|
37
|
-
'im.message.receive_v1': (data: {
|
|
38
|
-
sender?: { sender_id?: { open_id?: string } }
|
|
39
|
-
message?: { chat_id?: string; chat_type?: string; message_type?: string; content?: string; mentions?:
|
|
56
|
+
'im.message.receive_v1': (data: {
|
|
57
|
+
sender?: { sender_id?: { open_id?: string } }
|
|
58
|
+
message?: { chat_id?: string; chat_type?: string; message_type?: string; content?: string; mentions?: FeishuMention[] }
|
|
40
59
|
}) => {
|
|
41
60
|
const message = data.message
|
|
42
|
-
if (!message
|
|
43
|
-
let text = ''
|
|
44
|
-
try { text = (JSON.parse(message.content ?? '{}') as { text?: string }).text ?? '' } catch { text = message.content ?? '' }
|
|
61
|
+
if (!message) return
|
|
45
62
|
const group = message.chat_type === 'group'
|
|
46
|
-
|
|
47
|
-
|
|
63
|
+
const addressed = !group || isFeishuBotMentioned(message.mentions, botOpenId)
|
|
64
|
+
if (message.message_type !== 'text') {
|
|
65
|
+
if (addressed && message.chat_id) {
|
|
66
|
+
void client?.im.message.create({
|
|
67
|
+
params: { receive_id_type: 'chat_id' },
|
|
68
|
+
data: { receive_id: message.chat_id, msg_type: 'text', content: JSON.stringify({ text: '暂不支持该消息类型,请发送文字。' }) },
|
|
69
|
+
}).catch((error) => log(`[${id}] 非文本提示发送失败: ${error instanceof Error ? error.message : String(error)}`))
|
|
70
|
+
}
|
|
71
|
+
return
|
|
48
72
|
}
|
|
73
|
+
let text = ''
|
|
74
|
+
try { text = (JSON.parse(message.content ?? '{}') as { text?: string }).text ?? '' } catch { text = message.content ?? '' }
|
|
49
75
|
void handler?.({
|
|
50
|
-
chatId: message.chat_id ?? '',
|
|
51
|
-
userId: data.sender?.sender_id?.open_id,
|
|
52
|
-
text,
|
|
53
|
-
kind: group ? 'group' : 'dm',
|
|
54
|
-
addressed
|
|
55
|
-
})
|
|
56
|
-
},
|
|
57
|
-
})
|
|
58
|
-
const wsClient = new sdk.WSClient({ appId, appSecret, logger, loggerLevel, ...(domain ? { domain } : {}) })
|
|
59
|
-
ws = wsClient
|
|
60
|
-
await wsClient.start({ eventDispatcher: dispatcher })
|
|
61
|
-
statusText = '长连接已建立'
|
|
62
|
-
log(`[${id}] WebSocket 长连接已启动`)
|
|
63
|
-
},
|
|
64
|
-
async stop() {
|
|
65
|
-
ws?.close({ force: true })
|
|
66
|
-
ws = undefined
|
|
67
|
-
statusText = '已停止'
|
|
68
|
-
},
|
|
69
|
-
async send(chatId, text) {
|
|
70
|
-
if (!client) throw new Error(`${id}: 尚未连接`)
|
|
71
|
-
await client.im.message.create({
|
|
72
|
-
params: { receive_id_type: 'chat_id' },
|
|
73
|
-
data: { receive_id: chatId, msg_type: 'text', content: JSON.stringify({ text }) },
|
|
74
|
-
})
|
|
75
|
-
},
|
|
76
|
-
setMessageHandler(h) { handler = h },
|
|
77
|
-
status() { return statusText },
|
|
78
|
-
}
|
|
76
|
+
chatId: message.chat_id ?? '',
|
|
77
|
+
userId: data.sender?.sender_id?.open_id,
|
|
78
|
+
text,
|
|
79
|
+
kind: group ? 'group' : 'dm',
|
|
80
|
+
addressed,
|
|
81
|
+
})
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
const wsClient = new sdk.WSClient({ appId, appSecret, logger, loggerLevel, ...(domain ? { domain } : {}) })
|
|
85
|
+
ws = wsClient
|
|
86
|
+
await wsClient.start({ eventDispatcher: dispatcher })
|
|
87
|
+
statusText = '长连接已建立'
|
|
88
|
+
log(`[${id}] WebSocket 长连接已启动`)
|
|
89
|
+
},
|
|
90
|
+
async stop() {
|
|
91
|
+
ws?.close({ force: true })
|
|
92
|
+
ws = undefined
|
|
93
|
+
statusText = '已停止'
|
|
94
|
+
},
|
|
95
|
+
async send(chatId, text) {
|
|
96
|
+
if (!client) throw new Error(`${id}: 尚未连接`)
|
|
97
|
+
await client.im.message.create({
|
|
98
|
+
params: { receive_id_type: 'chat_id' },
|
|
99
|
+
data: { receive_id: chatId, msg_type: 'text', content: JSON.stringify({ text }) },
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
setMessageHandler(h) { handler = h },
|
|
103
|
+
status() { return statusText },
|
|
104
|
+
}
|
|
79
105
|
}
|
|
80
106
|
|
|
81
107
|
|