@alemonjs/kook 0.2.7 → 2.1.0-alpha.0
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/lib/config.js +20 -0
- package/lib/desktop.d.ts +2 -2
- package/lib/desktop.js +54 -55
- package/lib/hook.d.ts +38 -0
- package/lib/hook.js +24 -0
- package/lib/index.d.ts +6 -7
- package/lib/index.js +314 -274
- package/lib/sdk/api.d.ts +275 -0
- package/lib/sdk/{platform/kook/sdk/api.js → api.js} +25 -26
- package/lib/sdk/{platform/kook/sdk/config.js → config.js} +1 -1
- package/lib/sdk/{platform/kook/sdk/message.js → message.js} +1 -4
- package/lib/sdk/typings.d.ts +109 -0
- package/package.json +2 -2
- package/dist/assets/index.css +0 -1
- package/dist/assets/index.js +0 -49
- package/dist/index.html +0 -15
- package/lib/sdk/core/utils/from.js +0 -42
- package/lib/sdk/platform/kook/sdk/api.d.ts +0 -288
- package/lib/sdk/platform/kook/sdk/message/INTERACTION.d.ts +0 -8
- package/lib/sdk/platform/kook/sdk/message/MEMBER_ADD.d.ts +0 -10
- package/lib/sdk/platform/kook/sdk/message/MEMBER_REMOVE.d.ts +0 -10
- package/lib/sdk/platform/kook/sdk/message/MESSAGES_DIRECT.d.ts +0 -10
- package/lib/sdk/platform/kook/sdk/message/MESSAGES_PUBLIC.d.ts +0 -10
- package/lib/sdk/platform/kook/sdk/message/REACTIONS.d.ts +0 -10
- package/lib/sdk/platform/kook/sdk/message.d.ts +0 -18
- package/lib/sdk/platform/kook/sdk/typings.d.ts +0 -221
- package/lib/sdk/platform/kook/sdk/wss.d.ts +0 -26
- package/lib/sdk/platform/kook/sdk/wss.types.d.ts +0 -12
- /package/lib/{sdk/core → core}/config.js +0 -0
- /package/lib/sdk/{platform/kook/sdk/conversation.js → conversation.js} +0 -0
- /package/lib/sdk/{platform/kook/sdk/typings.js → typings.js} +0 -0
- /package/lib/sdk/{platform/kook/sdk/wss.js → wss.js} +0 -0
package/lib/index.js
CHANGED
|
@@ -1,27 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getConfigValue, cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
2
|
+
export { KOOKAPI as API } from './sdk/api.js';
|
|
3
|
+
import { KOOKClient } from './sdk/wss.js';
|
|
2
4
|
import { readFileSync } from 'fs';
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
|
|
6
|
-
import 'public-ip';
|
|
7
|
-
import 'stream';
|
|
8
|
-
import 'file-type';
|
|
9
|
-
import 'form-data';
|
|
10
|
-
import './sdk/platform/kook/sdk/typings.js';
|
|
11
|
-
import { KOOKClient } from './sdk/platform/kook/sdk/wss.js';
|
|
5
|
+
import { getMaster } from './config.js';
|
|
6
|
+
import { getBufferByURL } from 'alemonjs/utils';
|
|
7
|
+
export { useClient, useValue } from './hook.js';
|
|
12
8
|
|
|
13
9
|
const platform = 'kook';
|
|
14
|
-
|
|
15
|
-
get: (_, prop) => {
|
|
16
|
-
if (prop in global.client) {
|
|
17
|
-
const original = global.client[prop];
|
|
18
|
-
// 防止函数内this丢失
|
|
19
|
-
return typeof original === 'function' ? original.bind(global.client) : original;
|
|
20
|
-
}
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
var index = defineBot(() => {
|
|
10
|
+
var index = () => {
|
|
25
11
|
let value = getConfigValue();
|
|
26
12
|
if (!value)
|
|
27
13
|
value = {};
|
|
@@ -32,36 +18,22 @@ var index = defineBot(() => {
|
|
|
32
18
|
});
|
|
33
19
|
// 连接
|
|
34
20
|
client.connect();
|
|
21
|
+
const url = `ws://127.0.0.1:${process.env?.port || config?.port || 17117}`;
|
|
22
|
+
const cbp = cbpPlatform(url);
|
|
35
23
|
client.on('MESSAGES_DIRECT', async (event) => {
|
|
36
24
|
// 过滤机器人
|
|
37
25
|
if (event.extra?.author?.bot)
|
|
38
26
|
return false;
|
|
39
|
-
//
|
|
40
|
-
const
|
|
41
|
-
const isMaster = master_key.includes(event.author_id);
|
|
27
|
+
// 创建私聊标记
|
|
28
|
+
const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
|
|
42
29
|
// 头像
|
|
43
30
|
const avatar = event.extra.author.avatar;
|
|
44
31
|
// 获取消息
|
|
45
32
|
let msg = event.content;
|
|
46
33
|
const url = avatar.substring(0, avatar.indexOf('?'));
|
|
47
|
-
const UserAvatar =
|
|
48
|
-
toBuffer: async () => {
|
|
49
|
-
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
50
|
-
return Buffer.from(arrayBuffer);
|
|
51
|
-
},
|
|
52
|
-
toBase64: async () => {
|
|
53
|
-
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
54
|
-
return Buffer.from(arrayBuffer).toString('base64');
|
|
55
|
-
},
|
|
56
|
-
toURL: async () => {
|
|
57
|
-
return url;
|
|
58
|
-
}
|
|
59
|
-
};
|
|
34
|
+
const UserAvatar = url;
|
|
60
35
|
const UserId = event.author_id;
|
|
61
|
-
const UserKey =
|
|
62
|
-
Platform: platform,
|
|
63
|
-
UserId
|
|
64
|
-
});
|
|
36
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
65
37
|
// 定义消
|
|
66
38
|
const e = {
|
|
67
39
|
name: 'private.message.create',
|
|
@@ -77,14 +49,13 @@ var index = defineBot(() => {
|
|
|
77
49
|
// message
|
|
78
50
|
MessageId: event.msg_id,
|
|
79
51
|
MessageText: msg,
|
|
80
|
-
OpenId:
|
|
52
|
+
OpenId: data?.code,
|
|
81
53
|
CreateAt: Date.now(),
|
|
82
54
|
//
|
|
83
55
|
tag: 'MESSAGES_PUBLIC',
|
|
84
|
-
value:
|
|
56
|
+
value: event
|
|
85
57
|
};
|
|
86
|
-
|
|
87
|
-
onProcessor('private.message.create', e, event);
|
|
58
|
+
cbp.send(e);
|
|
88
59
|
});
|
|
89
60
|
// 监听消息
|
|
90
61
|
client.on('MESSAGES_PUBLIC', async (event) => {
|
|
@@ -93,9 +64,6 @@ var index = defineBot(() => {
|
|
|
93
64
|
return false;
|
|
94
65
|
// 创建私聊标记
|
|
95
66
|
const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
|
|
96
|
-
// 主人
|
|
97
|
-
const master_key = config?.master_key ?? [];
|
|
98
|
-
const isMaster = master_key.includes(event.author_id);
|
|
99
67
|
// 头像
|
|
100
68
|
const avatar = event.extra.author.avatar;
|
|
101
69
|
// 获取消息
|
|
@@ -115,25 +83,9 @@ var index = defineBot(() => {
|
|
|
115
83
|
for (const item of mention_part) {
|
|
116
84
|
msg = msg.replace(`(met)${item.id}(met)`, '').trim();
|
|
117
85
|
}
|
|
118
|
-
const
|
|
119
|
-
const UserAvatar = {
|
|
120
|
-
toBuffer: async () => {
|
|
121
|
-
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
122
|
-
return Buffer.from(arrayBuffer);
|
|
123
|
-
},
|
|
124
|
-
toBase64: async () => {
|
|
125
|
-
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
126
|
-
return Buffer.from(arrayBuffer).toString('base64');
|
|
127
|
-
},
|
|
128
|
-
toURL: async () => {
|
|
129
|
-
return url;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
86
|
+
const UserAvatar = avatar.substring(0, avatar.indexOf('?'));
|
|
132
87
|
const UserId = event.author_id;
|
|
133
|
-
const UserKey =
|
|
134
|
-
Platform: platform,
|
|
135
|
-
UserId
|
|
136
|
-
});
|
|
88
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
137
89
|
// 定义消
|
|
138
90
|
const e = {
|
|
139
91
|
name: 'message.create',
|
|
@@ -142,6 +94,7 @@ var index = defineBot(() => {
|
|
|
142
94
|
//
|
|
143
95
|
GuildId: event.extra.guild_id,
|
|
144
96
|
ChannelId: event.target_id,
|
|
97
|
+
SpaceId: event.target_id,
|
|
145
98
|
// 用户Id
|
|
146
99
|
UserId: UserId,
|
|
147
100
|
UserKey,
|
|
@@ -156,227 +109,314 @@ var index = defineBot(() => {
|
|
|
156
109
|
CreateAt: Date.now(),
|
|
157
110
|
//
|
|
158
111
|
tag: 'MESSAGES_PUBLIC',
|
|
159
|
-
value:
|
|
112
|
+
value: event
|
|
160
113
|
};
|
|
161
|
-
|
|
162
|
-
onProcessor('message.create', e, event);
|
|
114
|
+
cbp.send(e);
|
|
163
115
|
});
|
|
164
116
|
// 发送错误时
|
|
165
117
|
client.on('ERROR', msg => {
|
|
166
118
|
console.error(msg);
|
|
167
119
|
});
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
item.value == 'all' ||
|
|
187
|
-
item.value == '' ||
|
|
188
|
-
typeof item.value != 'string') {
|
|
189
|
-
return `(met)all(met)`;
|
|
190
|
-
}
|
|
191
|
-
if (item.options?.belong == 'user') {
|
|
192
|
-
return `(met)${item.value}(met)`;
|
|
193
|
-
}
|
|
194
|
-
else if (item.options?.belong == 'channel') {
|
|
195
|
-
return `(chn)${item.value}(chn)`;
|
|
196
|
-
}
|
|
197
|
-
return '';
|
|
198
|
-
}
|
|
199
|
-
else if (item.type == 'Text') {
|
|
200
|
-
if (item.options?.style == 'block') {
|
|
201
|
-
return `\`${item.value}\``;
|
|
202
|
-
}
|
|
203
|
-
else if (item.options?.style == 'italic') {
|
|
204
|
-
return `*${item.value}*`;
|
|
205
|
-
}
|
|
206
|
-
else if (item.options?.style == 'bold') {
|
|
207
|
-
return `**${item.value}**`;
|
|
208
|
-
}
|
|
209
|
-
else if (item.options?.style == 'strikethrough') {
|
|
210
|
-
return `~~${item.value}~~`;
|
|
211
|
-
}
|
|
212
|
-
else if (item.options?.style == 'boldItalic') {
|
|
213
|
-
return `***${item.value}***`;
|
|
214
|
-
}
|
|
215
|
-
return item.value;
|
|
216
|
-
}
|
|
217
|
-
})
|
|
218
|
-
.join('');
|
|
219
|
-
if (content) {
|
|
220
|
-
return Promise.all([content].map(item => client.createMessage({
|
|
221
|
-
type: 9,
|
|
222
|
-
target_id: channel_id,
|
|
223
|
-
content: item
|
|
224
|
-
})));
|
|
225
|
-
}
|
|
226
|
-
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
227
|
-
if (images) {
|
|
228
|
-
return Promise.all(images.map(async (item) => {
|
|
229
|
-
if (item.type == 'ImageURL') {
|
|
230
|
-
return await client.createMessage({
|
|
231
|
-
type: 2,
|
|
232
|
-
target_id: channel_id,
|
|
233
|
-
content: item.value
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
let data = item.value;
|
|
237
|
-
if (item.type == 'ImageFile') {
|
|
238
|
-
data = readFileSync(item.value);
|
|
239
|
-
}
|
|
240
|
-
// 上传图片
|
|
241
|
-
const res = await client.postImage(data);
|
|
242
|
-
if (!res)
|
|
243
|
-
return Promise.resolve();
|
|
244
|
-
// 发送消息
|
|
245
|
-
return await client.createMessage({
|
|
246
|
-
type: 2,
|
|
247
|
-
target_id: channel_id,
|
|
248
|
-
content: res.data.url
|
|
249
|
-
});
|
|
250
|
-
}));
|
|
251
|
-
}
|
|
252
|
-
return Promise.all([]);
|
|
253
|
-
},
|
|
254
|
-
user: (channel_id, val) => {
|
|
255
|
-
return Promise.all([]);
|
|
256
|
-
}
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* @param channel_id
|
|
123
|
+
* @param val
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
const sendChannel = async (target_id, val) => {
|
|
127
|
+
if (val.length < 0)
|
|
128
|
+
return Promise.all([]);
|
|
129
|
+
const content = val
|
|
130
|
+
.filter(item => item.type == 'Mention' || item.type == 'Text')
|
|
131
|
+
.map(item => {
|
|
132
|
+
if (item.type == 'Mention') {
|
|
133
|
+
if (item.value == 'everyone' ||
|
|
134
|
+
item.value == 'all' ||
|
|
135
|
+
item.value == '' ||
|
|
136
|
+
typeof item.value != 'string') {
|
|
137
|
+
return `(met)all(met)`;
|
|
257
138
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
.
|
|
304
|
-
if (content) {
|
|
305
|
-
return Promise.all([content].map(item => client.createMessage({
|
|
306
|
-
type: 9,
|
|
307
|
-
target_id: event.ChannelId,
|
|
308
|
-
content: item
|
|
309
|
-
})));
|
|
139
|
+
if (item.options?.belong == 'user') {
|
|
140
|
+
return `(met)${item.value}(met)`;
|
|
141
|
+
}
|
|
142
|
+
else if (item.options?.belong == 'channel') {
|
|
143
|
+
return `(chn)${item.value}(chn)`;
|
|
144
|
+
}
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
else if (item.type == 'Text') {
|
|
148
|
+
if (item.options?.style == 'block') {
|
|
149
|
+
return `\`${item.value}\``;
|
|
150
|
+
}
|
|
151
|
+
else if (item.options?.style == 'italic') {
|
|
152
|
+
return `*${item.value}*`;
|
|
153
|
+
}
|
|
154
|
+
else if (item.options?.style == 'bold') {
|
|
155
|
+
return `**${item.value}**`;
|
|
156
|
+
}
|
|
157
|
+
else if (item.options?.style == 'strikethrough') {
|
|
158
|
+
return `~~${item.value}~~`;
|
|
159
|
+
}
|
|
160
|
+
else if (item.options?.style == 'boldItalic') {
|
|
161
|
+
return `***${item.value}***`;
|
|
162
|
+
}
|
|
163
|
+
return item.value;
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
.join('');
|
|
167
|
+
try {
|
|
168
|
+
if (content) {
|
|
169
|
+
const res = await client.createMessage({
|
|
170
|
+
type: 9,
|
|
171
|
+
target_id: target_id,
|
|
172
|
+
content: content
|
|
173
|
+
});
|
|
174
|
+
return [createResult(ResultCode.Ok, 'client.createMessage', res)];
|
|
175
|
+
}
|
|
176
|
+
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
177
|
+
if (images.length > 0) {
|
|
178
|
+
let bufferData = null;
|
|
179
|
+
for (let i = 0; i < images.length; i++) {
|
|
180
|
+
if (bufferData)
|
|
181
|
+
break;
|
|
182
|
+
const item = images[i];
|
|
183
|
+
if (item.type == 'Image') {
|
|
184
|
+
bufferData = Buffer.from(item.value, 'base64');
|
|
310
185
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
return Promise.all(images.map(async (item) => {
|
|
314
|
-
if (item.type == 'ImageURL') {
|
|
315
|
-
return await client.createMessage({
|
|
316
|
-
type: 2,
|
|
317
|
-
target_id: event.ChannelId,
|
|
318
|
-
content: item.value
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
let data = item.value;
|
|
322
|
-
if (item.type == 'ImageFile') {
|
|
323
|
-
data = readFileSync(item.value);
|
|
324
|
-
}
|
|
325
|
-
// 上传图片
|
|
326
|
-
const res = await client.postImage(data);
|
|
327
|
-
if (!res)
|
|
328
|
-
return Promise.resolve();
|
|
329
|
-
// 发送消息
|
|
330
|
-
return await client.createMessage({
|
|
331
|
-
type: 2,
|
|
332
|
-
target_id: event.ChannelId,
|
|
333
|
-
content: res.data.url
|
|
334
|
-
});
|
|
335
|
-
}));
|
|
186
|
+
else if (item.type == 'ImageURL') {
|
|
187
|
+
bufferData = await getBufferByURL(item.value);
|
|
336
188
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
mention: async (e) => {
|
|
340
|
-
const event = e.value;
|
|
341
|
-
const MessageMention = [];
|
|
342
|
-
/**
|
|
343
|
-
* 艾特类型所得到的,包括机器人在内
|
|
344
|
-
*/
|
|
345
|
-
const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
|
|
346
|
-
for (const item of mention_role_part) {
|
|
347
|
-
MessageMention.push({
|
|
348
|
-
UserId: item.role_id,
|
|
349
|
-
UserName: item.name,
|
|
350
|
-
UserKey: useUserHashKey({
|
|
351
|
-
Platform: platform,
|
|
352
|
-
UserId: item.role_id
|
|
353
|
-
}),
|
|
354
|
-
IsMaster: false,
|
|
355
|
-
IsBot: true
|
|
356
|
-
});
|
|
189
|
+
else if (item.type == 'ImageFile') {
|
|
190
|
+
bufferData = readFileSync(item.value);
|
|
357
191
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
192
|
+
}
|
|
193
|
+
if (!bufferData)
|
|
194
|
+
return [];
|
|
195
|
+
// 上传图片
|
|
196
|
+
const imageRes = await client.postImage(bufferData);
|
|
197
|
+
if (!imageRes)
|
|
198
|
+
return [];
|
|
199
|
+
const url = imageRes.data?.url;
|
|
200
|
+
if (!url)
|
|
201
|
+
return [];
|
|
202
|
+
// 发送消息
|
|
203
|
+
const res = await client.createMessage({
|
|
204
|
+
type: 2,
|
|
205
|
+
target_id: target_id,
|
|
206
|
+
content: url
|
|
207
|
+
});
|
|
208
|
+
return [createResult(ResultCode.Ok, 'client.createMessage', res)];
|
|
209
|
+
}
|
|
210
|
+
return [];
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
return [createResult(ResultCode.Fail, 'client.createMessage', error)];
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
*
|
|
218
|
+
* @param channel_id
|
|
219
|
+
* @param val
|
|
220
|
+
* @returns
|
|
221
|
+
*/
|
|
222
|
+
const sendUser = async (open_id, val) => {
|
|
223
|
+
if (val.length < 0)
|
|
224
|
+
return [];
|
|
225
|
+
const content = val
|
|
226
|
+
.filter(item => item.type == 'Mention' || item.type == 'Text')
|
|
227
|
+
.map(item => {
|
|
228
|
+
if (item.type == 'Mention') {
|
|
229
|
+
if (item.value == 'everyone' ||
|
|
230
|
+
item.value == 'all' ||
|
|
231
|
+
item.value == '' ||
|
|
232
|
+
typeof item.value != 'string') {
|
|
233
|
+
return `(met)all(met)`;
|
|
234
|
+
}
|
|
235
|
+
if (item.options?.belong == 'user') {
|
|
236
|
+
return `(met)${item.value}(met)`;
|
|
237
|
+
}
|
|
238
|
+
else if (item.options?.belong == 'channel') {
|
|
239
|
+
return `(chn)${item.value}(chn)`;
|
|
240
|
+
}
|
|
241
|
+
return '';
|
|
242
|
+
}
|
|
243
|
+
else if (item.type == 'Text') {
|
|
244
|
+
if (item.options?.style == 'block') {
|
|
245
|
+
return `\`${item.value}\``;
|
|
246
|
+
}
|
|
247
|
+
else if (item.options?.style == 'italic') {
|
|
248
|
+
return `*${item.value}*`;
|
|
249
|
+
}
|
|
250
|
+
else if (item.options?.style == 'bold') {
|
|
251
|
+
return `**${item.value}**`;
|
|
252
|
+
}
|
|
253
|
+
else if (item.options?.style == 'strikethrough') {
|
|
254
|
+
return `~~${item.value}~~`;
|
|
255
|
+
}
|
|
256
|
+
else if (item.options?.style == 'boldItalic') {
|
|
257
|
+
return `***${item.value}***`;
|
|
258
|
+
}
|
|
259
|
+
return item.value;
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
.join('');
|
|
263
|
+
try {
|
|
264
|
+
if (content) {
|
|
265
|
+
const res = await client.createDirectMessage({
|
|
266
|
+
type: 9,
|
|
267
|
+
chat_code: open_id,
|
|
268
|
+
content: content
|
|
269
|
+
});
|
|
270
|
+
return [createResult(ResultCode.Ok, 'client.createDirectMessage', res)];
|
|
271
|
+
}
|
|
272
|
+
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
273
|
+
if (images.length > 0) {
|
|
274
|
+
let bufferData = null;
|
|
275
|
+
for (let i = 0; i < images.length; i++) {
|
|
276
|
+
if (bufferData)
|
|
277
|
+
break;
|
|
278
|
+
const item = images[i];
|
|
279
|
+
if (item.type == 'Image') {
|
|
280
|
+
bufferData = Buffer.from(item.value, 'base64');
|
|
281
|
+
}
|
|
282
|
+
else if (item.type == 'ImageURL') {
|
|
283
|
+
bufferData = await getBufferByURL(item.value);
|
|
284
|
+
}
|
|
285
|
+
else if (item.type == 'ImageFile') {
|
|
286
|
+
bufferData = readFileSync(item.value);
|
|
374
287
|
}
|
|
375
|
-
return MessageMention;
|
|
376
288
|
}
|
|
289
|
+
if (!bufferData)
|
|
290
|
+
return [];
|
|
291
|
+
// 上传图片
|
|
292
|
+
const imageRes = await client.postImage(bufferData);
|
|
293
|
+
if (!imageRes)
|
|
294
|
+
return [];
|
|
295
|
+
const url = imageRes.data?.url;
|
|
296
|
+
if (!url)
|
|
297
|
+
return [];
|
|
298
|
+
const res = await client.createDirectMessage({
|
|
299
|
+
type: 2,
|
|
300
|
+
chat_code: open_id,
|
|
301
|
+
content: url
|
|
302
|
+
});
|
|
303
|
+
return [createResult(ResultCode.Ok, 'client.createDirectMessage', res)];
|
|
304
|
+
}
|
|
305
|
+
return [];
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
return [createResult(ResultCode.Fail, 'client.createDirectMessage', error)];
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
*
|
|
313
|
+
* @param user_id
|
|
314
|
+
* @param val
|
|
315
|
+
* @returns
|
|
316
|
+
*/
|
|
317
|
+
// const sendUserByUserId = async (user_id: string, val: DataEnums[]) => {
|
|
318
|
+
// if (val.length < 0) return []
|
|
319
|
+
// // 创建私聊标记
|
|
320
|
+
// const data = await client.userChatCreate(user_id).then(res => res?.data)
|
|
321
|
+
// const open_id = data?.code
|
|
322
|
+
// return await sendUser(open_id, val)
|
|
323
|
+
// }
|
|
324
|
+
const api = {
|
|
325
|
+
active: {
|
|
326
|
+
send: {
|
|
327
|
+
channel: sendChannel,
|
|
328
|
+
user: sendUser
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
use: {
|
|
332
|
+
send: async (event, val) => {
|
|
333
|
+
if (val.length < 0)
|
|
334
|
+
return [];
|
|
335
|
+
if (event.name == 'message.create') {
|
|
336
|
+
return await sendChannel(event.ChannelId, val);
|
|
337
|
+
}
|
|
338
|
+
else if (event.name == 'private.message.create') {
|
|
339
|
+
return await sendUser(event.OpenId, val);
|
|
340
|
+
}
|
|
341
|
+
return [];
|
|
342
|
+
},
|
|
343
|
+
mention: async (e) => {
|
|
344
|
+
const event = e.value;
|
|
345
|
+
const MessageMention = [];
|
|
346
|
+
const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
|
|
347
|
+
for (const item of mention_role_part) {
|
|
348
|
+
const UserId = item.role_id;
|
|
349
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
350
|
+
MessageMention.push({
|
|
351
|
+
UserId: UserId,
|
|
352
|
+
UserName: item.name,
|
|
353
|
+
UserKey: UserKey,
|
|
354
|
+
IsMaster: isMaster,
|
|
355
|
+
IsBot: true
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
const mention_part = event.extra.kmarkdown?.mention_part ?? [];
|
|
359
|
+
for (const item of mention_part) {
|
|
360
|
+
const UserId = item.id;
|
|
361
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
362
|
+
MessageMention.push({
|
|
363
|
+
UserId: UserId,
|
|
364
|
+
UserName: item.username,
|
|
365
|
+
UserKey: UserKey,
|
|
366
|
+
IsMaster: isMaster,
|
|
367
|
+
IsBot: false
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return MessageMention;
|
|
377
371
|
}
|
|
378
372
|
}
|
|
379
373
|
};
|
|
380
|
-
|
|
374
|
+
cbp.onactions(async (data, consume) => {
|
|
375
|
+
if (data.action === 'message.send') {
|
|
376
|
+
const event = data.payload.event;
|
|
377
|
+
const paramFormat = data.payload.params.format;
|
|
378
|
+
const res = await api.use.send(event, paramFormat);
|
|
379
|
+
if (!res) {
|
|
380
|
+
consume([createResult(ResultCode.Ok, '请求完成', null)]);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
384
|
+
}
|
|
385
|
+
else if (data.action === 'message.send.channel') {
|
|
386
|
+
const channel_id = data.payload.ChannelId;
|
|
387
|
+
const val = data.payload.params.format;
|
|
388
|
+
const res = await api.active.send.channel(channel_id, val);
|
|
389
|
+
if (!res) {
|
|
390
|
+
consume([createResult(ResultCode.Ok, '请求完成', null)]);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
394
|
+
}
|
|
395
|
+
else if (data.action === 'message.send.user') {
|
|
396
|
+
const user_id = data.payload.UserId;
|
|
397
|
+
const val = data.payload.params.format;
|
|
398
|
+
const res = await api.active.send.user(user_id, val);
|
|
399
|
+
if (!res) {
|
|
400
|
+
consume([createResult(ResultCode.Ok, '请求完成', null)]);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
404
|
+
}
|
|
405
|
+
else if (data.action === 'mention.get') {
|
|
406
|
+
const event = data.payload.event;
|
|
407
|
+
const res = await api.use.mention(event);
|
|
408
|
+
consume([createResult(ResultCode.Ok, '请求完成', res)]);
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
cbp.onapis(async (data, consume) => {
|
|
412
|
+
const key = data.payload?.key;
|
|
413
|
+
if (client[key]) {
|
|
414
|
+
// 如果 client 上有对应的 key,直接调用。
|
|
415
|
+
const params = data.payload.params;
|
|
416
|
+
const res = await client[key](...params);
|
|
417
|
+
consume([createResult(ResultCode.Ok, '请求完成', res)]);
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
};
|
|
381
421
|
|
|
382
|
-
export {
|
|
422
|
+
export { index as default, platform };
|