wechaty-web-panel 1.6.112 → 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 +2 -2
  106. package/src/package-json.js +2 -2
  107. package/tsconfig.json +3 -12
  108. package/dist/index.d.ts +0 -9
  109. package/tsconfig.cjs.json +0 -12
@@ -0,0 +1,535 @@
1
+ import { setLocalSchedule, delay, cancelAllSchedule } from "../lib/index.js";
2
+ import { allConfig } from "../db/configDb.js";
3
+ import { getScheduleList, updateSchedule } from "../proxy/aibotk.js";
4
+ import { getNewsContent, getEveryDayContent, roomSay, getRoomEveryDayContent, contactSay, getCountDownContent, getCustomContent } from "../common/index.js";
5
+ import globalConfig from "../db/global.js";
6
+ const typeMap = {
7
+ contact: "用户名", room: "群名"
8
+ };
9
+ /**
10
+ * 查找发送的目标
11
+ * type: contact room
12
+ * name: 目标名称
13
+ * alias: 别名
14
+ * wxid: wxid
15
+ */
16
+ async function findTarget({ that, type, name, alias, wxid = "" }) {
17
+ let target = null;
18
+ if (type === "contact") {
19
+ target = wxid && that.Contact.find({ id: wxid }) || name && (await that.Contact.find({ name: name })) || alias && (await that.Contact.find({ alias: alias })); // 获取你要发送的联系人
20
+ }
21
+ else {
22
+ target = wxid && that.Room.find({ id: wxid }) || name && (await that.Room.find({ topic: name })); // 获取你要发送的群组
23
+ }
24
+ return target;
25
+ }
26
+ /**
27
+ * 设置定时任务
28
+ * @param that
29
+ * @param item
30
+ * @param name
31
+ * @param callback
32
+ * @return {Promise<void>}
33
+ */
34
+ async function setTask(that, item, name, callback) {
35
+ try {
36
+ let time = item.date;
37
+ item.type = item.type ? item.type : "contact";
38
+ const target = await findTarget({
39
+ that, type: item.type, name: item.roomName || item.name, alias: item.alias || "", wxid: item.wxid || ""
40
+ });
41
+ if (!target) {
42
+ console.log(`查找不到${typeMap[item.type]}:${item.roomName || item.name}`);
43
+ }
44
+ else {
45
+ console.log(`${typeMap[item.type]}:“${item.roomName || item.name}”设置定时任务成功`);
46
+ setLocalSchedule(time, callback.bind(null, { that, target, item }), name);
47
+ }
48
+ }
49
+ catch (error) {
50
+ console.log("设置自定义定时任务失败:", error);
51
+ }
52
+ }
53
+ /**
54
+ * 发送自定义内容
55
+ * @param that
56
+ * @param target
57
+ * @param item
58
+ * @param isMulti 是否多个目标
59
+ * @param targets 发送的多个目标
60
+ * @return {Promise<void>}
61
+ */
62
+ async function sendCustom({ that, target, item, isMulti, targets }) {
63
+ for (let reply of item.contents) {
64
+ console.log("定时任务开始发送,内容:", `${reply.type === 1 ? reply.content : reply.url}`);
65
+ await delay(1000);
66
+ if (item.type === "room") {
67
+ if (!isMulti) {
68
+ await roomSay.call(that, target, "", reply);
69
+ }
70
+ else {
71
+ for (let single of targets) {
72
+ try {
73
+ await roomSay.call(that, single, "", reply);
74
+ if (item.delay) {
75
+ await delay(item.delay);
76
+ }
77
+ else {
78
+ await delay(1000);
79
+ }
80
+ }
81
+ catch (e) {
82
+ console.log(`定时任务失败,可能群已经解散或者好友不存在: ${e}`);
83
+ }
84
+ }
85
+ }
86
+ }
87
+ else {
88
+ if (!isMulti) {
89
+ await contactSay.call(that, target, reply);
90
+ }
91
+ else {
92
+ for (let single of targets) {
93
+ try {
94
+ await contactSay.call(that, single, reply);
95
+ if (item.delay) {
96
+ await delay(item.delay);
97
+ }
98
+ else {
99
+ await delay(1000);
100
+ }
101
+ }
102
+ catch (e) {
103
+ console.log(`定时任务失败,可能群已经解散或者好友不存在: ${e}`);
104
+ }
105
+ }
106
+ }
107
+ }
108
+ }
109
+ }
110
+ ;
111
+ /**
112
+ * 发送新闻资讯
113
+ * @param that
114
+ * @param target
115
+ * @param item
116
+ * @param isMulti 是否多个目标
117
+ * @param targets 发送的多个目标
118
+ * @return {Promise<void>}
119
+ */
120
+ async function sendNews({ that, target, item, isMulti, targets }) {
121
+ let content = await getNewsContent(item.sortId, item.endWord, item.num || 10);
122
+ console.log("新闻资讯开始发送,内容:", content);
123
+ await delay(200);
124
+ if (!isMulti) {
125
+ await target.say(content);
126
+ }
127
+ else {
128
+ for (let single of targets) {
129
+ try {
130
+ await single.say(content);
131
+ if (item.delay) {
132
+ await delay(item.delay);
133
+ }
134
+ else {
135
+ await delay(1000);
136
+ }
137
+ }
138
+ catch (e) {
139
+ console.log(`定时任务失败,可能群已经解散或者好友不存在: ${e}`);
140
+ }
141
+ }
142
+ }
143
+ }
144
+ ;
145
+ /**
146
+ * 发送自定义定制内容
147
+ * @param that
148
+ * @param target
149
+ * @param type 类型 room contact
150
+ * @param item
151
+ * @param isMulti 是否多个目标
152
+ * @param targets 发送的多个目标
153
+ * @param taskId 任务id
154
+ * @return {Promise<void>}
155
+ */
156
+ async function sendCustomContent({ that, target, type, item, isMulti, targets, taskId }) {
157
+ console.log("发送定制内容开始,目标:", item.sortId, taskId);
158
+ let contents = await getCustomContent(item.sortId, taskId);
159
+ console.log("定制内容发送", contents);
160
+ if (!isMulti) {
161
+ for (const reply of contents) {
162
+ await contactSay.call(that, target, reply);
163
+ await delay(200);
164
+ }
165
+ }
166
+ else {
167
+ for (let single of targets) {
168
+ try {
169
+ for (const reply of contents) {
170
+ type === 'room' ? await roomSay.call(that, single, '', reply) : await contactSay.call(that, single, reply);
171
+ await delay(200);
172
+ }
173
+ if (item.delay) {
174
+ await delay(item.delay);
175
+ }
176
+ else {
177
+ await delay(800);
178
+ }
179
+ }
180
+ catch (e) {
181
+ console.log(`定时任务失败,可能群已经解散或者好友不存在: ${e}`);
182
+ }
183
+ }
184
+ }
185
+ }
186
+ ;
187
+ /**
188
+ * 发送每日说
189
+ * @param that
190
+ * @param target
191
+ * @param item
192
+ * @return {Promise<void>}
193
+ */
194
+ async function sendEveryDay({ that, target, item }) {
195
+ let content = "";
196
+ if (item.type === "room") {
197
+ content = await getRoomEveryDayContent(item.memorialDay, item.city, item.endWord);
198
+ }
199
+ else {
200
+ content = await getEveryDayContent(item.memorialDay, item.city, item.endWord);
201
+ }
202
+ console.log("每日说任务开始工作,发送内容:", content);
203
+ await delay(1000);
204
+ await target.say(content);
205
+ }
206
+ ;
207
+ /**
208
+ * 发送倒计时
209
+ * @param that
210
+ * @param target
211
+ * @param item
212
+ * @param isMulti 是否多个目标
213
+ * @param targets 发送的多个目标
214
+ * @return {Promise<void>}
215
+ */
216
+ async function sendCountDown({ that, target, item, isMulti, targets }) {
217
+ let content = await getCountDownContent(item.memorialDay, item.prefix, item.suffix, item.endWord);
218
+ console.log("倒计时任务开始工作,发送内容:", content);
219
+ await delay(1000);
220
+ if (!isMulti) {
221
+ await target.say(content);
222
+ }
223
+ else {
224
+ for (let single of targets) {
225
+ try {
226
+ await single.say(content);
227
+ if (item.delay) {
228
+ await delay(item.delay);
229
+ }
230
+ else {
231
+ await delay(1000);
232
+ }
233
+ }
234
+ catch (e) {
235
+ console.log(`定时任务失败,可能群已经解散或者好友不存在,不影响其他群发消息: ${e}`);
236
+ }
237
+ }
238
+ }
239
+ }
240
+ ;
241
+ /**
242
+ * 立即发送群消息
243
+ * @param {*} that bot对象
244
+ * @param {*} item 任务项 { target: 'Room', event: '', message: { roomName: '', type: 'news 新闻 ||task 定时任务', contents: [] } }
245
+ */
246
+ async function sendTaskMessage(that, info) {
247
+ try {
248
+ const item = info.message;
249
+ item.type = item.type ? item.type : "room";
250
+ const target = await findTarget({
251
+ that, type: item.type, name: item.roomName || item.name, alias: item.alias || "", wxid: item.wxid || ""
252
+ });
253
+ if (!target) {
254
+ console.log(`查找不到${typeMap[item.type]}:${item.roomName || item.name}`);
255
+ }
256
+ else {
257
+ if (info.event === "roomNews" || info.event === "news") {
258
+ await sendNews({ that, target, item });
259
+ }
260
+ else if (info.event === "roomTask" || info.event === "custom") {
261
+ await sendCustom({ that, target, item });
262
+ }
263
+ else if (info.event === "wechatEveryday") {
264
+ await sendEveryDay({ that, target, item });
265
+ }
266
+ else if (info.event === "countdown") {
267
+ await sendCountDown({ that, target, item });
268
+ }
269
+ }
270
+ }
271
+ catch (error) {
272
+ console.log("发送定时任务失败:", error);
273
+ }
274
+ }
275
+ /**
276
+ * 设置定时任务
277
+ * @param {*} that bot 对象
278
+ * @param {*} item 定时任务项
279
+ */
280
+ async function setScheduleTask(that, item, name) {
281
+ try {
282
+ let time = item.isLoop ? item.time : new Date(item.time);
283
+ setLocalSchedule(time, async () => {
284
+ try {
285
+ let contact = await that.Contact.find({ name: item.subscribe });
286
+ if (contact) {
287
+ console.log(`${item.subscribe}的专属提醒开启啦!`);
288
+ await contact.say(item.content.replaceAll('\\n', '\n'));
289
+ }
290
+ else {
291
+ console.log(`没有找到联系人:${item.subscribe}`);
292
+ }
293
+ if (!item.isLoop) {
294
+ await updateSchedule(item.id);
295
+ }
296
+ }
297
+ catch (error) {
298
+ console.log("设置定时任务错误", error);
299
+ }
300
+ }, name);
301
+ }
302
+ catch (e) {
303
+ console.log("setScheduleTask error", e);
304
+ }
305
+ }
306
+ /**
307
+ * 初始化提醒任务
308
+ * @param {}} that
309
+ */
310
+ async function initTimeSchedule(that) {
311
+ try {
312
+ cancelAllSchedule("time_tips");
313
+ let scheduleList = await getScheduleList(); // 获取定时任务
314
+ if (scheduleList && scheduleList.length > 0) {
315
+ for (let item of scheduleList) {
316
+ void setScheduleTask(that, item, `time_tips_${item.id}`);
317
+ }
318
+ }
319
+ }
320
+ catch (e) {
321
+ console.log("initTimeSchedule error", e);
322
+ }
323
+ }
324
+ /**
325
+ * 初始化定时任务
326
+ * @param {}} that
327
+ */
328
+ async function initTaskLocalSchedule(that) {
329
+ try {
330
+ cancelAllSchedule("task");
331
+ const config = await allConfig(); // 获取配置信息
332
+ const { dayTaskSchedule, roomNewsSchedule, roomTaskSchedule, countDownTaskSchedule } = config;
333
+ // 每日说定时任务
334
+ if (dayTaskSchedule && dayTaskSchedule.length > 0) {
335
+ dayTaskSchedule.forEach((item, index) => {
336
+ setTask(that, { type: "contact", ...item }, `task_day_${index}`, sendEveryDay);
337
+ });
338
+ }
339
+ // 新闻资讯定时任务
340
+ if (roomNewsSchedule && roomNewsSchedule.length > 0) {
341
+ roomNewsSchedule.forEach((item, index) => {
342
+ setTask(that, { type: "room", ...item }, `task_news_${index}`, sendNews);
343
+ });
344
+ }
345
+ // 自定义内容定时任务
346
+ if (roomTaskSchedule && roomTaskSchedule.length > 0) {
347
+ roomTaskSchedule.forEach((item, index) => {
348
+ setTask(that, { type: "room", ...item }, `task_custom_${index}`, sendCustom);
349
+ });
350
+ }
351
+ // 倒计时定时任务
352
+ if (countDownTaskSchedule && countDownTaskSchedule.length > 0) {
353
+ countDownTaskSchedule.forEach((item, index) => {
354
+ setTask(that, item, `task_countdown_${index}`, sendCountDown);
355
+ });
356
+ }
357
+ }
358
+ catch (e) {
359
+ console.log("initTaskLocalSchedule error", e);
360
+ }
361
+ }
362
+ async function getMultiTargets(that, type, task) {
363
+ const targets = [];
364
+ if (task.isAll) {
365
+ const allTargets = type === "room" ? await that.Room.findAll() : await that.Contact.findAll();
366
+ if (task.excludeTargets) {
367
+ allTargets.forEach(target => {
368
+ const find = type === "room" ? task.excludeTargets.find(eItem => target.id === eItem.id || target.topic() === eItem.name) : task.excludeTargets.find(eItem => target.id === eItem.id || target.name() === eItem.name);
369
+ if (type === "room") {
370
+ if (!find) {
371
+ targets.push(target);
372
+ }
373
+ }
374
+ else {
375
+ if (!find && target.friend()) {
376
+ targets.push(target);
377
+ }
378
+ }
379
+ });
380
+ }
381
+ console.log(`查询到全部${type === 'room' ? '群聊' : '好友'}数量为:${targets.length}`);
382
+ }
383
+ else {
384
+ for (let target of task.targets) {
385
+ const finalTarget = type === "room" ? await that.Room.find({
386
+ id: target.id, topic: target.name
387
+ }) : await that.Contact.find({ id: target.id, name: target.name });
388
+ if (finalTarget) {
389
+ targets.push(finalTarget);
390
+ }
391
+ else {
392
+ console.log(`定时任务查找不到${type === "room" ? "群" : "好友"}:${target.name},请检查${type === "room" ? "群名" : "好友昵称"}是否正确`);
393
+ }
394
+ }
395
+ }
396
+ return targets;
397
+ }
398
+ /**
399
+ * 立即发送批量任务消息
400
+ * @param {*} that bot对象
401
+ * @param {*} item 任务项 { target: 'Room', event: '', message: { roomName: '', type: 'news 新闻 ||task 定时任务', contents: [] } }
402
+ */
403
+ async function sendMultiTaskMessage(that, task) {
404
+ try {
405
+ const targets = await getMultiTargets(that, task.type, task);
406
+ if (!targets.length) {
407
+ console.log("查找不到要发送的目标,请检查后重试");
408
+ return;
409
+ }
410
+ if (task.taskType === 'news') {
411
+ await sendNews({ that, isMulti: true, targets, item: task.taskInfo });
412
+ }
413
+ else if (task.taskType === 'custom') {
414
+ await sendCustom({ that, isMulti: true, targets, item: { ...task.taskInfo, type: task.type } });
415
+ }
416
+ else if (task.taskType === "countdown") {
417
+ await sendCountDown({ that, isMulti: true, targets, item: task.taskInfo });
418
+ }
419
+ else if (task.taskType === "customContent") {
420
+ await sendCustomContent({ that, isMulti: true, targets, type: task.type, item: task.taskInfo, taskId: task.id });
421
+ }
422
+ }
423
+ catch (error) {
424
+ console.log("立即发送定时任务失败:", error);
425
+ }
426
+ }
427
+ async function startSendMultiTask({ that, task }) {
428
+ const targets = await getMultiTargets(that, task.type, task);
429
+ if (!targets.length) {
430
+ return;
431
+ }
432
+ if (task.taskType === "news") {
433
+ await sendNews({
434
+ that,
435
+ isMulti: true,
436
+ targets,
437
+ item: task.taskInfo
438
+ });
439
+ }
440
+ else if (task.taskType === "custom") {
441
+ await sendCustom({
442
+ that,
443
+ isMulti: true,
444
+ targets,
445
+ item: { ...task.taskInfo, type: task.type }
446
+ });
447
+ }
448
+ else if (task.taskType === "countDown") {
449
+ await sendCountDown({
450
+ that,
451
+ isMulti: true,
452
+ targets,
453
+ item: task.taskInfo
454
+ });
455
+ }
456
+ else if (task.taskType === "customContent") {
457
+ await sendCustomContent({
458
+ that,
459
+ isMulti: true,
460
+ targets,
461
+ item: task.taskInfo,
462
+ taskId: task.id
463
+ });
464
+ }
465
+ }
466
+ async function setMultiTask(that, task) {
467
+ try {
468
+ if (task.taskType === "news") {
469
+ setLocalSchedule(task.cron, startSendMultiTask.bind(null, {
470
+ that,
471
+ task
472
+ }), `schedule_news_${task.id}`);
473
+ }
474
+ else if (task.taskType === "custom") {
475
+ setLocalSchedule(task.cron, startSendMultiTask.bind(null, {
476
+ that,
477
+ task
478
+ }), `schedule_custom_${task.id}`);
479
+ }
480
+ else if (task.taskType === "countDown") {
481
+ setLocalSchedule(task.cron, startSendMultiTask.bind(null, {
482
+ that,
483
+ task
484
+ }), `schedule_countdown_${task.id}`);
485
+ }
486
+ else if (task.taskType === "customContent") {
487
+ setLocalSchedule(task.cron, startSendMultiTask.bind(null, {
488
+ that,
489
+ task
490
+ }), `schedule_customcontent_${task.id}`);
491
+ }
492
+ }
493
+ catch (e) {
494
+ console.log("catch error:" + e);
495
+ }
496
+ }
497
+ /**
498
+ * 初始化批量定时任务
499
+ * @param that
500
+ */
501
+ function initMultiTask(that) {
502
+ try {
503
+ cancelAllSchedule("schedule");
504
+ const tasks = globalConfig.getAllTasks(); // 获取所有任务
505
+ if (tasks && tasks.length) {
506
+ tasks.forEach((item, index) => {
507
+ void setMultiTask(that, item);
508
+ });
509
+ }
510
+ }
511
+ catch (e) {
512
+ console.log("initMultiTask error:" + e);
513
+ }
514
+ }
515
+ /**
516
+ * 初始化小助手任务
517
+ * @param {*} that bot对象
518
+ * @param {*} scheduleList 提醒任务列表
519
+ * @param {*} daySayList 每日说任务列表
520
+ * @param {*} RoomSayList 群资讯任务列表
521
+ */
522
+ async function initAllSchedule(that) {
523
+ await initTimeSchedule(that);
524
+ await initTaskLocalSchedule(that);
525
+ }
526
+ export { initTaskLocalSchedule };
527
+ export { initAllSchedule };
528
+ export { initMultiTask };
529
+ export { initTimeSchedule };
530
+ export { sendTaskMessage };
531
+ export { sendMultiTaskMessage };
532
+ export default {
533
+ initTaskLocalSchedule, initAllSchedule, initTimeSchedule
534
+ };
535
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,174 @@
1
+ import { setLocalSchedule, delay, cancelAllSchedule, delHtmlTag } from "../lib/index.js";
2
+ import { getAllRssConfig } from '../db/rssConfig.js';
3
+ import { addRssHistory, updateRssHistory, getRssHistoryById } from "../db/rssHistory.js";
4
+ import { roomSay, contactSay } from "../common/index.js";
5
+ import rssParser from 'rss-parser';
6
+ import { getPuppetEol } from "../const/puppet-type.js";
7
+ import dayjs from "dayjs";
8
+ import { getRssLast, updateRssLast } from '../proxy/aibotk.js';
9
+ async function getRssContent(info) {
10
+ try {
11
+ const parser = new rssParser({
12
+ requestOptions: {
13
+ rejectUnauthorized: false,
14
+ },
15
+ headers: { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' },
16
+ });
17
+ console.log('订阅源:' + info.rssUrl);
18
+ const feed = await parser.parseURL(info.rssUrl);
19
+ const lastItem = await getRssLast(info.id);
20
+ console.log('上一条已经推送的消息', JSON.stringify(lastItem));
21
+ // 当存在文章的时候
22
+ if (feed.items && feed.items.length) {
23
+ // 当存在历史推送记录 需要判读是否推送过
24
+ const last = feed.items[0];
25
+ const lastContent = last.link || last.title;
26
+ if (lastItem) {
27
+ if (lastContent !== lastItem?.lastContent) {
28
+ const content = await setContent(last, info);
29
+ void updateRssLast(info.id, lastContent);
30
+ return content;
31
+ }
32
+ console.log('rss内容未更新,最后一条内容已推送');
33
+ return [];
34
+ }
35
+ else {
36
+ const content = await setContent(last, info);
37
+ void updateRssLast(info.id, lastContent);
38
+ return content;
39
+ }
40
+ }
41
+ }
42
+ catch (e) {
43
+ console.log("获取rss内容失败,大概率是订阅源格式不规范,请更换:" + e);
44
+ return [];
45
+ }
46
+ }
47
+ async function setContent(feed, info) {
48
+ try {
49
+ const eol = await getPuppetEol();
50
+ if (info.showLink) {
51
+ const res = { type: 4, url: feed.link, title: delHtmlTag(feed.title), description: delHtmlTag(feed.content).substring(0, 80), thumbUrl: info.thumbUrl };
52
+ return [res];
53
+ }
54
+ else {
55
+ const desc = delHtmlTag(feed.content)?.substring(0, 1500)?.trim();
56
+ const content = `${info.prefixWord ? info.prefixWord + eol + eol : ''}${delHtmlTag(feed.title)}${eol}${eol} ${desc ? `【摘要】:${desc}...${eol}` : ''}【链接】:${feed.link}${eol}${eol}${info.suffixWord || ''}`;
57
+ return [{ type: 1, content: content }];
58
+ }
59
+ }
60
+ catch (e) {
61
+ console.log('设置rss发送内容报错:', e);
62
+ return [{ type: 1, content: '' }];
63
+ }
64
+ }
65
+ const typeMap = {
66
+ contact: "用户名",
67
+ room: "群名"
68
+ };
69
+ /**
70
+ * 查找发送的目标
71
+ * type: contact room
72
+ * name: 目标名称
73
+ * alias: 别名
74
+ * wxid: wxid
75
+ */
76
+ async function findTarget({ that, type, name, alias, wxid = '' }) {
77
+ let target = null;
78
+ if (type === "contact") {
79
+ target = wxid && that.Contact.find({ id: wxid }) || name && (await that.Contact.find({ name: name })) || alias && (await that.Contact.find({ alias: alias })); // 获取你要发送的联系人
80
+ }
81
+ else {
82
+ target = wxid && that.Room.find({ id: wxid }) || name && (await that.Room.find({ topic: name })); // 获取你要发送的群组
83
+ }
84
+ return target;
85
+ }
86
+ /**
87
+ * 设置定时任务
88
+ * @param that
89
+ * @param item
90
+ * @param name
91
+ * @param callback
92
+ * @return {Promise<void>}
93
+ */
94
+ async function setTask(that, item, name, callback) {
95
+ try {
96
+ let time = item.updateTime;
97
+ for (let user of item.targets) {
98
+ const target = await findTarget({ that, type: item.type, name: user.name, wxid: user.id || '' });
99
+ if (!target) {
100
+ console.log(`查找不到${typeMap[item.type]}:${user.name}`);
101
+ }
102
+ else {
103
+ console.log(`${typeMap[item.type]}:“${user.name}”设置rss定时任务成功`);
104
+ setLocalSchedule(time, callback.bind(null, { that, target, item }), name);
105
+ }
106
+ }
107
+ }
108
+ catch (error) {
109
+ console.log("设置自定义定时任务失败:", error);
110
+ }
111
+ }
112
+ /**
113
+ * 发送rss 更新订阅内容
114
+ * @param that
115
+ * @param target
116
+ * @param item
117
+ * @return {Promise<void>}
118
+ */
119
+ async function sendRssContent({ that, target, item }) {
120
+ const replys = await getRssContent(item);
121
+ for (let reply of replys) {
122
+ console.log("检测到rss内容更新,开始发送:", `${reply.type === 1 ? reply.content : reply.url}`);
123
+ await delay(1000);
124
+ if (item.type === "room") {
125
+ await roomSay.call(that, target, "", reply);
126
+ }
127
+ else {
128
+ await contactSay.call(that, target, reply);
129
+ }
130
+ }
131
+ }
132
+ /**
133
+ * 立即发送rss消息
134
+ * @param {*} that bot对象
135
+ * @param {*} item 任务项 { target: 'Room', event: '', message: { roomName: '', type: 'news 新闻 ||task 定时任务', contents: [] } }
136
+ */
137
+ export async function sendRssTaskMessage(that, info) {
138
+ try {
139
+ const item = info.message;
140
+ for (let user of item.targets) {
141
+ const target = await findTarget({ that, type: item.type, name: user.name || '', wxid: user.id || '' });
142
+ if (!target) {
143
+ console.log(`查找不到${typeMap[item.type]}:${user.name}`);
144
+ }
145
+ else {
146
+ await sendRssContent({ that, target, item });
147
+ }
148
+ }
149
+ }
150
+ catch (error) {
151
+ console.log("发送rss任务失败:", error);
152
+ }
153
+ }
154
+ /**
155
+ * 初始化rss定时任务
156
+ * @param {}} that
157
+ */
158
+ export async function initRssTask(that) {
159
+ try {
160
+ cancelAllSchedule("rss_");
161
+ const rssTasks = await getAllRssConfig(); // 获取配置信息
162
+ console.log(`获取到 rss 任务 :${rssTasks.length}个`);
163
+ // 每日说定时任务
164
+ if (rssTasks && rssTasks.length > 0) {
165
+ rssTasks.forEach((item, index) => {
166
+ setTask(that, item, `rss_${index}`, sendRssContent);
167
+ });
168
+ }
169
+ }
170
+ catch (e) {
171
+ console.log("初始化rss订阅任务", e);
172
+ }
173
+ }
174
+ //# sourceMappingURL=rss.js.map