wechaty-web-panel 1.6.111 → 1.6.113

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.
Files changed (109) hide show
  1. package/dist/bot/chatgpt/index.js +235 -0
  2. package/dist/bot/coze/sdk/index.js +110 -0
  3. package/dist/bot/dify/sdk/index.js +461 -0
  4. package/dist/bot/dify/sdk/office.js +319 -0
  5. package/dist/bot/fastgpt/index.js +98 -0
  6. package/dist/bot/qanything/index.js +136 -0
  7. package/dist/botInstance/coze.js +167 -0
  8. package/dist/botInstance/cozev3.js +157 -0
  9. package/dist/botInstance/dify.js +160 -0
  10. package/dist/botInstance/fastgpt.js +130 -0
  11. package/dist/botInstance/gpt4v.js +95 -0
  12. package/dist/botInstance/officialOpenAi.js +186 -0
  13. package/dist/botInstance/qany.js +144 -0
  14. package/dist/botInstance/sdk/chatGPT4V.js +89 -0
  15. package/dist/botInstance/sdk/coze.js +200 -0
  16. package/dist/botInstance/sdk/difyClient.js +354 -0
  17. package/dist/botInstance/sdk/pTimeout.js +97 -0
  18. package/dist/botInstance/sdk/qanything.js +137 -0
  19. package/dist/botInstance/sdk/quick-lru.js +237 -0
  20. package/dist/common/hook.js +66 -0
  21. package/dist/common/index.js +513 -0
  22. package/dist/common/multiReply.js +158 -0
  23. package/dist/common/reply.js +23 -0
  24. package/dist/const/puppet-type.js +71 -0
  25. package/dist/db/aiDb.js +27 -0
  26. package/dist/db/aichatDb.js +84 -0
  27. package/dist/db/chatHistory.js +137 -0
  28. package/dist/db/configDb.js +97 -0
  29. package/dist/db/global.js +62 -0
  30. package/dist/db/gptConfig.js +85 -0
  31. package/dist/db/nedb.js +88 -0
  32. package/dist/db/puppetDb.js +58 -0
  33. package/dist/db/roomDb.js +83 -0
  34. package/dist/db/rssConfig.js +82 -0
  35. package/dist/db/rssHistory.js +88 -0
  36. package/dist/db/userDb.js +27 -0
  37. package/dist/handlers/on-callback-message.js +183 -0
  38. package/dist/handlers/on-error.js +5 -0
  39. package/dist/handlers/on-friend.js +62 -0
  40. package/dist/handlers/on-heartbeat.js +20 -0
  41. package/dist/handlers/on-login.js +58 -0
  42. package/dist/handlers/on-logout.js +17 -0
  43. package/dist/handlers/on-message.js +644 -0
  44. package/dist/handlers/on-ready.js +36 -0
  45. package/dist/handlers/on-record-message.js +56 -0
  46. package/dist/handlers/on-roomjoin.js +42 -0
  47. package/dist/handlers/on-roomleave.js +12 -0
  48. package/dist/handlers/on-roomtopic.js +16 -0
  49. package/dist/handlers/on-scan.js +64 -0
  50. package/dist/handlers/on-verifycode.js +42 -0
  51. package/dist/index.js +81 -69306
  52. package/dist/lib/contentCensor.js +23 -0
  53. package/dist/lib/index.js +562 -0
  54. package/dist/lib/oss.js +43 -0
  55. package/dist/lib/s3oss.js +33 -0
  56. package/dist/mcp/mcp-server.js +26 -0
  57. package/dist/mcp/src/config/database.js +51 -0
  58. package/dist/mcp/src/index.js +238 -0
  59. package/dist/mcp/src/mcp/schemas.js +178 -0
  60. package/dist/mcp/src/mcp/server.js +421 -0
  61. package/dist/mcp/src/mcp/streamable-server.js +690 -0
  62. package/dist/mcp/src/models/ChatMessage.js +151 -0
  63. package/dist/mcp/src/models/Friend.js +64 -0
  64. package/dist/mcp/src/models/Group.js +55 -0
  65. package/dist/mcp/src/models/GroupMember.js +67 -0
  66. package/dist/mcp/src/models/index.js +27 -0
  67. package/dist/mcp/src/scripts/migrate.js +21 -0
  68. package/dist/mcp/src/services/ChatDataService.js +284 -0
  69. package/dist/mcp/src/services/McpService.js +521 -0
  70. package/dist/mcp/src/services/McpTools.js +504 -0
  71. package/dist/mcp/streamable-examples.js +283 -0
  72. package/dist/mcp/streamable-server.js +79 -0
  73. package/dist/mcp/test-mcp.js +64 -0
  74. package/dist/mcp/test-streamable-server.js +86 -0
  75. package/dist/package-json.js +89 -0
  76. package/dist/proxy/aibotk.js +829 -0
  77. package/dist/proxy/api.js +431 -0
  78. package/dist/proxy/apib.js +587 -0
  79. package/dist/proxy/bot/chatgpt.js +38 -0
  80. package/dist/proxy/bot/coze.js +38 -0
  81. package/dist/proxy/bot/cozev3.js +38 -0
  82. package/dist/proxy/bot/dify.js +38 -0
  83. package/dist/proxy/bot/dispatch.js +81 -0
  84. package/dist/proxy/bot/fastgpt.js +27 -0
  85. package/dist/proxy/bot/qany.js +27 -0
  86. package/dist/proxy/config.js +14 -0
  87. package/dist/proxy/cozeAi.js +60 -0
  88. package/dist/proxy/cozeV3Ai.js +60 -0
  89. package/dist/proxy/difyAi.js +58 -0
  90. package/dist/proxy/fastgpt.js +55 -0
  91. package/dist/proxy/mqtt.js +275 -0
  92. package/dist/proxy/multimodal.js +122 -0
  93. package/dist/proxy/openAi.js +63 -0
  94. package/dist/proxy/outapi.js +62 -0
  95. package/dist/proxy/qAnyAi.js +57 -0
  96. package/dist/proxy/superagent.js +200 -0
  97. package/dist/proxy/tencent-open.js +255 -0
  98. package/dist/service/event-dispatch-service.js +309 -0
  99. package/dist/service/gpt4vService.js +45 -0
  100. package/dist/service/msg-filter-service.js +121 -0
  101. package/dist/service/msg-filters.js +645 -0
  102. package/dist/service/room-async-service.js +455 -0
  103. package/dist/task/index.js +535 -0
  104. package/dist/task/rss.js +174 -0
  105. package/package.json +3 -3
  106. package/src/package-json.js +3 -3
  107. package/tsconfig.json +3 -12
  108. package/dist/index.d.ts +0 -9
  109. package/tsconfig.cjs.json +0 -12
@@ -0,0 +1,455 @@
1
+ import Mustache from 'mustache';
2
+ function roomTalker(options) {
3
+ if (!options) {
4
+ return () => undefined;
5
+ }
6
+ if (!Array.isArray(options)) {
7
+ options = [options];
8
+ }
9
+ const optionList = options;
10
+ return async function talkRoom(room, contact, mustacheView) {
11
+ for (const option of optionList) {
12
+ let msg;
13
+ if (option instanceof Function) {
14
+ msg = await option(room, contact);
15
+ }
16
+ else {
17
+ msg = option;
18
+ }
19
+ if (!msg) {
20
+ continue;
21
+ }
22
+ if (typeof msg === 'string') {
23
+ if (mustacheView) {
24
+ msg = Mustache.render(msg, mustacheView);
25
+ }
26
+ if (contact) {
27
+ await room.say(msg, contact);
28
+ }
29
+ else {
30
+ await room.say(msg);
31
+ }
32
+ }
33
+ else {
34
+ /**
35
+ * FIXME(huan): https://github.com/microsoft/TypeScript/issues/14107
36
+ */
37
+ await room.say(msg);
38
+ }
39
+ await room.wechaty.sleep(1000);
40
+ }
41
+ };
42
+ }
43
+ function messageMapper(mapperOptions, one) {
44
+ return async function mapMessage(message) {
45
+ return normalizeMappedMessageList(mapperOptions, message, one);
46
+ };
47
+ }
48
+ async function normalizeMappedMessageList(options, message, one) {
49
+ try {
50
+ const msgList = [];
51
+ let optionList;
52
+ if (Array.isArray(options)) {
53
+ optionList = options;
54
+ }
55
+ else {
56
+ optionList = [options];
57
+ }
58
+ for (const option of optionList) {
59
+ if (!option) {
60
+ continue;
61
+ }
62
+ if (typeof option === 'function') {
63
+ const ret = await option(message, one);
64
+ if (ret) {
65
+ msgList.push(...(await normalizeMappedMessageList(ret, message)));
66
+ }
67
+ }
68
+ else {
69
+ msgList.push(option);
70
+ }
71
+ }
72
+ return msgList;
73
+ }
74
+ catch (e) {
75
+ console.log('normalizeMappedMessageList error', e);
76
+ }
77
+ }
78
+ function messageMatcher(matcherOptions) {
79
+ if (!matcherOptions) {
80
+ return () => Promise.resolve(false);
81
+ }
82
+ if (!Array.isArray(matcherOptions)) {
83
+ matcherOptions = [matcherOptions];
84
+ }
85
+ const matcherOptionList = matcherOptions;
86
+ return async function matchMessage(message) {
87
+ try {
88
+ const room = message.room();
89
+ const roomTopic = await room.topic();
90
+ const talker = message.talker();
91
+ const talkerName = talker?.name();
92
+ let isMatch = false;
93
+ for (const option of matcherOptionList) {
94
+ if (typeof option === 'boolean') {
95
+ isMatch = option;
96
+ }
97
+ else if (typeof option === 'string') {
98
+ const idCheckList = [message.from()?.id, message.room()?.id, roomTopic, talkerName];
99
+ isMatch = idCheckList.includes(option);
100
+ }
101
+ else if (option instanceof RegExp) {
102
+ const text = await message.mentionText();
103
+ const textCheckList = [text, message.from()?.name(), await message.room()?.topic()];
104
+ isMatch = textCheckList.some((text) => text && option.test(text));
105
+ }
106
+ else if (typeof option === 'function') {
107
+ isMatch = await option(message);
108
+ }
109
+ else {
110
+ throw new Error('unknown matcher ' + option);
111
+ }
112
+ if (isMatch) {
113
+ return true;
114
+ }
115
+ }
116
+ // no match
117
+ return false;
118
+ }
119
+ catch (e) {
120
+ console.log('messageMatcher error', e);
121
+ return false;
122
+ }
123
+ };
124
+ }
125
+ /**
126
+ * 消息发送者name
127
+ * @param message
128
+ * @returns {Promise<*|string|string>}
129
+ */
130
+ const senderDisplayName = async (message) => {
131
+ try {
132
+ const talker = message.talker();
133
+ const room = message.room();
134
+ const alias = await room?.alias(talker);
135
+ return alias || talker.name() || 'Noname';
136
+ }
137
+ catch (e) {
138
+ console.log('senderDisplayName error', e);
139
+ return 'Noname';
140
+ }
141
+ };
142
+ /**
143
+ * 过滤群名
144
+ * @param matcher
145
+ * @returns {function(*): *}
146
+ * @example const abbrRoomTopicForDevelopersHome = abbrRoomTopicByRegex(/\s*([^\s]*\s*[^\s]+)$/) "Wechaty Developers' Home 8" -> "Home 8"
147
+ */
148
+ function abbrRoomTopicByRegex(matcher) {
149
+ return async function abbrRoomTopic(message) {
150
+ const room = message.room();
151
+ if (!room) {
152
+ return;
153
+ }
154
+ const topic = await room.topic();
155
+ const matched = topic.match(matcher);
156
+ if (!matched) {
157
+ return;
158
+ }
159
+ return matched[1];
160
+ };
161
+ }
162
+ /**
163
+ * 只转发文字消息
164
+ * @param message
165
+ * @returns {Promise<string>}
166
+ */
167
+ const bidirectionalMapper = async (message) => {
168
+ try {
169
+ const abbrRoomTopicForDevelopersHome = abbrRoomTopicByRegex(/\s*([^\s]*\s*[^\s]+)$/);
170
+ // Drop all messages if not Text
171
+ if (message.type() !== 7) {
172
+ return;
173
+ }
174
+ const talkerDisplayName = await senderDisplayName(message);
175
+ const roomShortName = (await abbrRoomTopicForDevelopersHome(message)) || 'Nowhere';
176
+ const text = message.text();
177
+ return `[${talkerDisplayName}@${roomShortName}]: ${text}`;
178
+ }
179
+ catch (e) {
180
+ console.log('bidirectionalMapper error', e);
181
+ return ``;
182
+ }
183
+ };
184
+ /**
185
+ * 转发文字和其他类型
186
+ * @param message
187
+ * @one topic name 指定群的名称 如果是这个群发的 那么就加一个消息类型
188
+ * @returns {Promise<(string|Message)[]>}
189
+ */
190
+ const unidirectionalMapper = async (message, one) => {
191
+ try {
192
+ const abbrRoomTopicForDevelopersHome = abbrRoomTopicByRegex(/\s*([^\s]*\s*[^\s]+)$/);
193
+ const talkerDisplayName = await senderDisplayName(message);
194
+ const roomShortName = (await abbrRoomTopicForDevelopersHome(message)) || 'Nowhere';
195
+ const prefix = `[${talkerDisplayName}@${roomShortName}]`;
196
+ const messageList = [];
197
+ const room = message.room();
198
+ const topic = await room.topic();
199
+ switch (message.type()) {
200
+ case 7:
201
+ messageList.push(`${prefix}: ${message.text()}`);
202
+ break;
203
+ default:
204
+ // Forward all non-Text messages
205
+ messageList.push(message);
206
+ /**
207
+ * If message is not sent from Headquarters Room,
208
+ * then we add a sender information for the destination rooms.
209
+ */
210
+ if (topic === one) {
211
+ const typeMap = {
212
+ 0: 'Unknown',
213
+ 1: 'Attachment', // Attach(6),
214
+ 2: 'Audio', // Audio(1), Voice(34)
215
+ 3: 'Contact', // ShareCard(42)
216
+ 4: 'ChatHistory', // ChatHistory(19)
217
+ 5: 'Emoticon', // Sticker: Emoticon(15), Emoticon(47)
218
+ 6: 'Image', // Img(2), Image(3)
219
+ 7: 'Text', // Text(1)
220
+ 8: 'Location', // Location(48)
221
+ 9: 'MiniProgram', // MiniProgram(33)
222
+ 10: 'GroupNote', // GroupNote(53)
223
+ 11: 'Transfer', // Transfers(2000)
224
+ 12: 'RedEnvelope', // RedEnvelopes(2001)
225
+ 13: 'Recalled', // Recalled(10002)
226
+ 14: 'Url', // Url(5)
227
+ 15: 'Video', // Video(4), Video(43)
228
+ 16: 'Post', // Moment, Channel, Tweet, etc
229
+ 17: 'Channel', // Channel
230
+ 18: 'System', // System Message
231
+ 19: 'Markdown', // Markdown Message
232
+ 20: 'CallRecord', // Call Record (voice and video, maybe group?)
233
+ };
234
+ const type = typeMap[message.type()];
235
+ // messageList.unshift(`${prefix}: ${type}`)
236
+ }
237
+ break;
238
+ }
239
+ return messageList;
240
+ }
241
+ catch (e) {
242
+ console.log('unidirectionalMapper error', e);
243
+ return [];
244
+ }
245
+ };
246
+ const isMatchConfig = (config) => {
247
+ const matchWhitelist = messageMatcher(config.whitelist);
248
+ const matchBlacklist = messageMatcher(config.blacklist);
249
+ return async function isMatch(message) {
250
+ try {
251
+ if (message.self()) {
252
+ return;
253
+ }
254
+ const room = message.room();
255
+ const roomTopic = await room.topic();
256
+ if (config.model === 2 || config.model === 3) {
257
+ if (!room || !config.many.includes(roomTopic)) {
258
+ return;
259
+ }
260
+ }
261
+ else if (config.model === 1) {
262
+ if (!room || roomTopic !== config.one) {
263
+ return;
264
+ }
265
+ }
266
+ if (await matchWhitelist(message)) {
267
+ return true;
268
+ }
269
+ if (await matchBlacklist(message)) {
270
+ return false;
271
+ }
272
+ return true;
273
+ }
274
+ catch (e) {
275
+ console.log('isMatchConfig error', e);
276
+ return false;
277
+ }
278
+ };
279
+ };
280
+ /**
281
+ * 多个群消息同步
282
+ * @param that
283
+ * @param config
284
+ * @param msg
285
+ * @returns {Promise<void>}
286
+ */
287
+ async function manyToMany(that, config, msg) {
288
+ try {
289
+ const isMatch = isMatchConfig(config);
290
+ const mapMessage = messageMapper(config.map);
291
+ const matchAndForward = async (message, roomList) => {
292
+ const match = await isMatch(message);
293
+ if (!match) {
294
+ return;
295
+ }
296
+ const msgList = await mapMessage(message);
297
+ if (msgList.length <= 0) {
298
+ return;
299
+ }
300
+ for (let i = 0; i < roomList.length; i++) {
301
+ const room = roomList[i];
302
+ const topic = await room.topic();
303
+ const msgTopic = await message.room().topic();
304
+ if (topic === msgTopic) {
305
+ continue;
306
+ }
307
+ const talkRoom = roomTalker(msgList);
308
+ await talkRoom(room);
309
+ }
310
+ };
311
+ let manyRoomList = [];
312
+ if (!manyRoomList.length) {
313
+ for (let i = 0; i < config.many.length; i++) {
314
+ const room = await that.Room.find({ topic: config.many[i] });
315
+ if (room) {
316
+ manyRoomList.push(room);
317
+ }
318
+ else {
319
+ console.log(`没有查找到群:${config.many[i]}`);
320
+ }
321
+ }
322
+ }
323
+ await matchAndForward(msg, manyRoomList);
324
+ }
325
+ catch (e) {
326
+ console.log('manyToMany error', e);
327
+ }
328
+ }
329
+ /**
330
+ * 多个群消息同步到指定群
331
+ * @param that
332
+ * @param config
333
+ * @param msg
334
+ * @returns {Promise<void>}
335
+ */
336
+ async function manyToOne(that, config, msg) {
337
+ try {
338
+ const isMatch = isMatchConfig(config);
339
+ const mapMessage = messageMapper(config.map);
340
+ const matchAndForward = async (message, room) => {
341
+ const match = await isMatch(message);
342
+ if (!match) {
343
+ return;
344
+ }
345
+ try {
346
+ const msgList = await mapMessage(message);
347
+ const talkRoom = roomTalker(msgList);
348
+ await talkRoom(room);
349
+ }
350
+ catch (e) {
351
+ console.log('WechatyPluginContrib, ManyToOneRoomConnector() filterThenToManyRoom(%s, %s) rejection: %s');
352
+ }
353
+ };
354
+ let oneRoom = '';
355
+ if (!oneRoom) {
356
+ oneRoom = await that.Room.find({ topic: config.one }); // await loadRoom(wechaty, config.one)
357
+ }
358
+ await matchAndForward(msg, oneRoom);
359
+ }
360
+ catch (e) {
361
+ console.log('manyToOne error', e);
362
+ }
363
+ }
364
+ /**
365
+ * 一对多, 一个群发的消息同步到其他几个群
366
+ * @param that
367
+ * @param config
368
+ * @param msg
369
+ * @returns {Promise<void>}
370
+ */
371
+ async function oneToMany(that, config, msg) {
372
+ try {
373
+ const isMatch = isMatchConfig(config);
374
+ const mapMessage = messageMapper(config.map, config.one);
375
+ const matchAndForward = async (message, roomList) => {
376
+ const match = await isMatch(message);
377
+ if (!match) {
378
+ return;
379
+ }
380
+ try {
381
+ const msgList = await mapMessage(message);
382
+ const talkRoom = roomTalker(msgList);
383
+ for (let i = 0; i < roomList.length; i++) {
384
+ const room = roomList[i];
385
+ if (room) {
386
+ await talkRoom(room);
387
+ await room.wechaty.sleep(1000);
388
+ }
389
+ }
390
+ }
391
+ catch (e) {
392
+ console.log('WechatyPluginContrib', 'OneToManyRoomConnector() filterThenToManyRoom(%s, %s) rejection: %s');
393
+ }
394
+ };
395
+ let manyRoomList = [];
396
+ if (!manyRoomList.length) {
397
+ for (let i = 0; i < config.many.length; i++) {
398
+ const room = await that.Room.find({ topic: config.many[i] });
399
+ if (room) {
400
+ manyRoomList.push(room);
401
+ }
402
+ else {
403
+ console.log(`没有查找到群:${config.many[i]}`);
404
+ }
405
+ }
406
+ }
407
+ await matchAndForward(msg, manyRoomList);
408
+ }
409
+ catch (e) {
410
+ console.log('oneToMany error', e);
411
+ }
412
+ }
413
+ /**
414
+ * model: 1 一对多 2 多对一 3 多对多
415
+ * @param that wechaty实例
416
+ * @param msg 消息
417
+ * @param list 规则列表
418
+ * @returns {Promise<void>}
419
+ */
420
+ async function dispatchAsync(that, msg, list) {
421
+ try {
422
+ const type = msg.type();
423
+ const mentionSelf = await msg.mentionSelf();
424
+ if (7 === type && mentionSelf) {
425
+ // 如果内容中有提及机器人的内容,不进行转发
426
+ return;
427
+ }
428
+ for (let i = 0; i < list.length; i++) {
429
+ const config = list[i];
430
+ config.blacklist = [async () => true];
431
+ if (config.forward === 1) {
432
+ config.map = bidirectionalMapper;
433
+ config.whitelist = [async (message) => message.type() === 7];
434
+ }
435
+ else if (config.forward === 2) {
436
+ config.blacklist = [];
437
+ config.map = unidirectionalMapper;
438
+ }
439
+ if (config.model === 3) {
440
+ await manyToMany(that, config, msg);
441
+ }
442
+ else if (config.model === 2) {
443
+ await manyToOne(that, config, msg);
444
+ }
445
+ else if (config.model === 1) {
446
+ await oneToMany(that, config, msg);
447
+ }
448
+ }
449
+ }
450
+ catch (e) {
451
+ console.log('dispatchAsync error:', e);
452
+ }
453
+ }
454
+ export { dispatchAsync };
455
+ //# sourceMappingURL=room-async-service.js.map