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.
- package/dist/bot/chatgpt/index.js +235 -0
- package/dist/bot/coze/sdk/index.js +110 -0
- package/dist/bot/dify/sdk/index.js +461 -0
- package/dist/bot/dify/sdk/office.js +319 -0
- package/dist/bot/fastgpt/index.js +98 -0
- package/dist/bot/qanything/index.js +136 -0
- package/dist/botInstance/coze.js +167 -0
- package/dist/botInstance/cozev3.js +157 -0
- package/dist/botInstance/dify.js +160 -0
- package/dist/botInstance/fastgpt.js +130 -0
- package/dist/botInstance/gpt4v.js +95 -0
- package/dist/botInstance/officialOpenAi.js +186 -0
- package/dist/botInstance/qany.js +144 -0
- package/dist/botInstance/sdk/chatGPT4V.js +89 -0
- package/dist/botInstance/sdk/coze.js +200 -0
- package/dist/botInstance/sdk/difyClient.js +354 -0
- package/dist/botInstance/sdk/pTimeout.js +97 -0
- package/dist/botInstance/sdk/qanything.js +137 -0
- package/dist/botInstance/sdk/quick-lru.js +237 -0
- package/dist/common/hook.js +66 -0
- package/dist/common/index.js +513 -0
- package/dist/common/multiReply.js +158 -0
- package/dist/common/reply.js +23 -0
- package/dist/const/puppet-type.js +71 -0
- package/dist/db/aiDb.js +27 -0
- package/dist/db/aichatDb.js +84 -0
- package/dist/db/chatHistory.js +137 -0
- package/dist/db/configDb.js +97 -0
- package/dist/db/global.js +62 -0
- package/dist/db/gptConfig.js +85 -0
- package/dist/db/nedb.js +88 -0
- package/dist/db/puppetDb.js +58 -0
- package/dist/db/roomDb.js +83 -0
- package/dist/db/rssConfig.js +82 -0
- package/dist/db/rssHistory.js +88 -0
- package/dist/db/userDb.js +27 -0
- package/dist/handlers/on-callback-message.js +183 -0
- package/dist/handlers/on-error.js +5 -0
- package/dist/handlers/on-friend.js +62 -0
- package/dist/handlers/on-heartbeat.js +20 -0
- package/dist/handlers/on-login.js +58 -0
- package/dist/handlers/on-logout.js +17 -0
- package/dist/handlers/on-message.js +644 -0
- package/dist/handlers/on-ready.js +36 -0
- package/dist/handlers/on-record-message.js +56 -0
- package/dist/handlers/on-roomjoin.js +42 -0
- package/dist/handlers/on-roomleave.js +12 -0
- package/dist/handlers/on-roomtopic.js +16 -0
- package/dist/handlers/on-scan.js +64 -0
- package/dist/handlers/on-verifycode.js +42 -0
- package/dist/index.js +81 -69306
- package/dist/lib/contentCensor.js +23 -0
- package/dist/lib/index.js +562 -0
- package/dist/lib/oss.js +43 -0
- package/dist/lib/s3oss.js +33 -0
- package/dist/mcp/mcp-server.js +26 -0
- package/dist/mcp/src/config/database.js +51 -0
- package/dist/mcp/src/index.js +238 -0
- package/dist/mcp/src/mcp/schemas.js +178 -0
- package/dist/mcp/src/mcp/server.js +421 -0
- package/dist/mcp/src/mcp/streamable-server.js +690 -0
- package/dist/mcp/src/models/ChatMessage.js +151 -0
- package/dist/mcp/src/models/Friend.js +64 -0
- package/dist/mcp/src/models/Group.js +55 -0
- package/dist/mcp/src/models/GroupMember.js +67 -0
- package/dist/mcp/src/models/index.js +27 -0
- package/dist/mcp/src/scripts/migrate.js +21 -0
- package/dist/mcp/src/services/ChatDataService.js +284 -0
- package/dist/mcp/src/services/McpService.js +521 -0
- package/dist/mcp/src/services/McpTools.js +504 -0
- package/dist/mcp/streamable-examples.js +283 -0
- package/dist/mcp/streamable-server.js +79 -0
- package/dist/mcp/test-mcp.js +64 -0
- package/dist/mcp/test-streamable-server.js +86 -0
- package/dist/package-json.js +89 -0
- package/dist/proxy/aibotk.js +829 -0
- package/dist/proxy/api.js +431 -0
- package/dist/proxy/apib.js +587 -0
- package/dist/proxy/bot/chatgpt.js +38 -0
- package/dist/proxy/bot/coze.js +38 -0
- package/dist/proxy/bot/cozev3.js +38 -0
- package/dist/proxy/bot/dify.js +38 -0
- package/dist/proxy/bot/dispatch.js +81 -0
- package/dist/proxy/bot/fastgpt.js +27 -0
- package/dist/proxy/bot/qany.js +27 -0
- package/dist/proxy/config.js +14 -0
- package/dist/proxy/cozeAi.js +60 -0
- package/dist/proxy/cozeV3Ai.js +60 -0
- package/dist/proxy/difyAi.js +58 -0
- package/dist/proxy/fastgpt.js +55 -0
- package/dist/proxy/mqtt.js +275 -0
- package/dist/proxy/multimodal.js +122 -0
- package/dist/proxy/openAi.js +63 -0
- package/dist/proxy/outapi.js +62 -0
- package/dist/proxy/qAnyAi.js +57 -0
- package/dist/proxy/superagent.js +200 -0
- package/dist/proxy/tencent-open.js +255 -0
- package/dist/service/event-dispatch-service.js +309 -0
- package/dist/service/gpt4vService.js +45 -0
- package/dist/service/msg-filter-service.js +121 -0
- package/dist/service/msg-filters.js +645 -0
- package/dist/service/room-async-service.js +455 -0
- package/dist/task/index.js +535 -0
- package/dist/task/rss.js +174 -0
- package/package.json +3 -3
- package/src/package-json.js +3 -3
- package/tsconfig.json +3 -12
- package/dist/index.d.ts +0 -9
- package/tsconfig.cjs.json +0 -12
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import dispatch from './event-dispatch-service.js';
|
|
2
|
+
import { getConfig, setSchedule, updateSchedule } from '../proxy/aibotk.js';
|
|
3
|
+
import { contentDistinguish, setLocalSchedule, isRealDate, delay } from '../lib/index.js';
|
|
4
|
+
import { addRoom } from '../common/index.js';
|
|
5
|
+
import { service, callbackAibotApi } from '../proxy/superagent.js';
|
|
6
|
+
import { dispatchBot } from '../proxy/bot/dispatch.js';
|
|
7
|
+
import globalConfig from '../db/global.js';
|
|
8
|
+
import { getUser } from '../db/userDb.js';
|
|
9
|
+
import { allConfig } from '../db/configDb.js';
|
|
10
|
+
import { getPuppetEol } from '../const/puppet-type.js';
|
|
11
|
+
import dayjs from 'dayjs';
|
|
12
|
+
import { getHistoryByNum, getHistoryByTime } from "../db/chatHistory.js";
|
|
13
|
+
async function emptyMsg({ room, isMention }) {
|
|
14
|
+
const config = await allConfig();
|
|
15
|
+
if (room && !isMention)
|
|
16
|
+
return [];
|
|
17
|
+
let msgArr = []; // 返回的消息列表
|
|
18
|
+
let obj = { type: 1, content: config.defaultReply, url: '' }; // 消息主体
|
|
19
|
+
msgArr.push(obj);
|
|
20
|
+
return msgArr;
|
|
21
|
+
}
|
|
22
|
+
function officialMsg() {
|
|
23
|
+
console.log('官方消息,不做回复');
|
|
24
|
+
return [{ type: 1, content: '', url: '' }];
|
|
25
|
+
}
|
|
26
|
+
function maxLengthMsg() {
|
|
27
|
+
console.log('字符超过设定值,不做回复');
|
|
28
|
+
return [{ type: 1, content: '', url: '' }];
|
|
29
|
+
}
|
|
30
|
+
function newFriendMsg({ config, name }) {
|
|
31
|
+
console.log(`消息为新添加好友:${name},默认回复`);
|
|
32
|
+
return config.newFriendReplys || [{ type: 1, content: '', url: '' }];
|
|
33
|
+
}
|
|
34
|
+
async function roomInviteMsg({ that, msg, contact, config }) {
|
|
35
|
+
try {
|
|
36
|
+
for (const item of config.roomJoinKeywords) {
|
|
37
|
+
if (item.reg === 2 && item.keywords.includes(msg)) {
|
|
38
|
+
console.log(`精确匹配到加群关键词${msg},正在邀请用户进群`);
|
|
39
|
+
await addRoom(that, contact, item.roomName, item.replys);
|
|
40
|
+
return [{ type: 1, content: '', url: '' }];
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
for (let key of item.keywords) {
|
|
44
|
+
if (msg.includes(key)) {
|
|
45
|
+
console.log(`模糊匹配到加群关键词${msg},正在邀请用户进群`);
|
|
46
|
+
await addRoom(that, contact, item.roomName, item.replys);
|
|
47
|
+
return [{ type: 1, content: '', url: '' }];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
console.log('roomInviteMsg error', e);
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 添加定时提醒
|
|
61
|
+
* @param that wechaty实例
|
|
62
|
+
* @param obj 定时对线
|
|
63
|
+
* @returns {Promise<boolean>}
|
|
64
|
+
*/
|
|
65
|
+
async function addSchedule(that, obj) {
|
|
66
|
+
try {
|
|
67
|
+
let scheduleObj = await setSchedule(obj);
|
|
68
|
+
let nickName = scheduleObj.subscribe;
|
|
69
|
+
let time = scheduleObj.time;
|
|
70
|
+
let Rule1 = scheduleObj.isLoop ? time : new Date(time);
|
|
71
|
+
let content = scheduleObj.content.replaceAll('\\n', '\n');
|
|
72
|
+
let contact = await that.Contact.find({ name: nickName });
|
|
73
|
+
let id = scheduleObj.id;
|
|
74
|
+
setLocalSchedule(Rule1, async () => {
|
|
75
|
+
console.log('你的专属提醒开启啦!');
|
|
76
|
+
await contact.say(content);
|
|
77
|
+
if (!scheduleObj.isLoop) {
|
|
78
|
+
updateSchedule(id);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
console.log('设置定时任务失败', error);
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function scheduleJobMsg({ that, msg, name }) {
|
|
89
|
+
try {
|
|
90
|
+
let obj = { type: 1, content: '', url: '' }; // 消息主体
|
|
91
|
+
let msgArr = msg.replace(/\s+/g, ' ').split(' ');
|
|
92
|
+
if (msgArr.length > 3) {
|
|
93
|
+
let schedule = contentDistinguish(msgArr, name);
|
|
94
|
+
let time = schedule.isLoop ? schedule.time : isRealDate(schedule.time);
|
|
95
|
+
if (time) {
|
|
96
|
+
let res = await addSchedule(that, schedule);
|
|
97
|
+
if (res) {
|
|
98
|
+
obj.content = '小助手已经把你的提醒牢记在小本本上了';
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
obj.content = '添加提醒失败,请稍后重试';
|
|
102
|
+
}
|
|
103
|
+
msgArr.push(obj);
|
|
104
|
+
return msgArr;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
obj.content = '提醒设置失败,请保证每个关键词之间使用空格分割开,并保证日期格式正确。正确格式为:“提醒(空格)我(空格)每天(空格)18:30(空格)下班回家';
|
|
108
|
+
msgArr.push(obj);
|
|
109
|
+
return msgArr;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
obj.content = '提醒设置失败,请保证每个关键词之间使用空格分割开,并保证日期格式正确。正确格式为:“提醒(空格)我(空格)18:30(空格)下班回家”';
|
|
114
|
+
msgArr.push(obj);
|
|
115
|
+
return msgArr;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
console.log('scheduleJobMsg error:', e);
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 获取事件处理返回的内容
|
|
125
|
+
* @param {*} event 事件名
|
|
126
|
+
* @param {*} msg 消息
|
|
127
|
+
* @param {*} name 用户
|
|
128
|
+
* @param {*} id 用户id
|
|
129
|
+
* @param avatar 用户头像
|
|
130
|
+
* @returns {String}
|
|
131
|
+
*/
|
|
132
|
+
async function getEventReply(that, event, msg, name, id, avatar, room, roomName, sourceMsg, contact, eventInfo) {
|
|
133
|
+
try {
|
|
134
|
+
let reply = await dispatch.dispatchEventContent(that, event, msg, name, id, avatar, room, roomName, sourceMsg, contact, eventInfo);
|
|
135
|
+
return reply;
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
console.log('getEventReply error', e);
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 回调函数事件
|
|
144
|
+
* @param that
|
|
145
|
+
* @param msg
|
|
146
|
+
* @param name
|
|
147
|
+
* @param id
|
|
148
|
+
* @param config
|
|
149
|
+
* @param room
|
|
150
|
+
* @returns Promise
|
|
151
|
+
*/
|
|
152
|
+
async function callbackEvent({ that, msg, name, id, config, room, isMention }) {
|
|
153
|
+
try {
|
|
154
|
+
for (let item of config.callBackEvents) {
|
|
155
|
+
for (let key of item.keywords) {
|
|
156
|
+
if ((item.reg === 1 && msg.includes(key)) || (item.reg === 2 && msg === key)) {
|
|
157
|
+
// 如果匹配到关键词 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发
|
|
158
|
+
if ((room && item.needAt === 1 && !isMention) || (room && item.needAt === undefined && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
msg = msg.trim();
|
|
162
|
+
const topic = room ? await room.topic() : '';
|
|
163
|
+
const data = {
|
|
164
|
+
uid: id,
|
|
165
|
+
uname: name,
|
|
166
|
+
roomId: (room && room.id) || '',
|
|
167
|
+
roomName: (room && topic) || '',
|
|
168
|
+
isMention: isMention ? 1 : 0,
|
|
169
|
+
word: msg
|
|
170
|
+
};
|
|
171
|
+
item.moreData &&
|
|
172
|
+
item.moreData.length &&
|
|
173
|
+
item.moreData.forEach((mItem) => {
|
|
174
|
+
if (mItem.key !== 'uid' && mItem.key !== 'uname' && mItem.key !== 'word' && mItem.key !== 'roomId' && mItem.key !== 'roomName' && mItem.key !== 'isMention') {
|
|
175
|
+
data[mItem.key] = mItem.value;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
const timeout = item.timeout || 60;
|
|
179
|
+
if (item.type === 100) {
|
|
180
|
+
let res = await service.post(item.customUrl, data, { timeout: timeout * 1000 });
|
|
181
|
+
return res;
|
|
182
|
+
}
|
|
183
|
+
else if (item.type === 1) {
|
|
184
|
+
let res = await callbackAibotApi(item.postUrl, data, timeout);
|
|
185
|
+
return res;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
catch (e) {
|
|
193
|
+
console.log('error', e);
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async function eventMsg({ that, msg, name, id, avatar, config, room, isMention, roomName, contact }) {
|
|
198
|
+
try {
|
|
199
|
+
for (let item of config.eventKeywords) {
|
|
200
|
+
for (let key of item.keywords) {
|
|
201
|
+
if ((item.reg === 1 && msg.includes(key)) || (item.reg === 2 && msg === key)) {
|
|
202
|
+
// 如果匹配到关键词 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发
|
|
203
|
+
if ((room && item.needAt === 1 && !isMention) || (room && item.needAt === undefined && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) {
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
const replaceMsg = msg.replace(key, '');
|
|
207
|
+
let res = await getEventReply(that, item.event, replaceMsg, name, id, avatar, room, roomName, msg, contact, item);
|
|
208
|
+
return res;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return [];
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
console.log('eventMsg error:', e);
|
|
216
|
+
return [];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* 关键词回复
|
|
221
|
+
* @returns {Promise<*>}
|
|
222
|
+
*/
|
|
223
|
+
async function keywordsMsg({ msg, config, room, isMention }) {
|
|
224
|
+
try {
|
|
225
|
+
if (config.replyKeywords && config.replyKeywords.length > 0) {
|
|
226
|
+
for (let item of config.replyKeywords) {
|
|
227
|
+
if (item.reg === 2 && item.keywords.includes(msg)) {
|
|
228
|
+
// 如果匹配到关键词 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发
|
|
229
|
+
if ((room && item.needAt === 1 && !isMention) || (room && item.needAt === undefined && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) {
|
|
230
|
+
return [];
|
|
231
|
+
}
|
|
232
|
+
console.log(`精确匹配到关键词${msg},正在回复用户`);
|
|
233
|
+
return item.replys;
|
|
234
|
+
}
|
|
235
|
+
else if (item.reg === 1) {
|
|
236
|
+
for (let key of item.keywords) {
|
|
237
|
+
if (msg.includes(key)) {
|
|
238
|
+
// 如果匹配到关键词 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发
|
|
239
|
+
if ((room && item.needAt === 1 && !isMention) || (room && item.needAt === undefined && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) {
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
242
|
+
console.log(`模糊匹配到关键词${msg},正在回复用户`);
|
|
243
|
+
return item.replys;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
return [];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
catch (e) {
|
|
254
|
+
console.log('keywordsMsg error:', e);
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async function robotMsg({ msg, name, id, config, isMention, roomName, userAlias, userWeixin, room, roomId, isFriend, file }) {
|
|
259
|
+
// 如果群里没有提及不开启机器人聊天
|
|
260
|
+
if (room && !isMention && config.roomAt || room && !isMention && !config.roomAt && isFriend && config.friendNoReplyInRoom) {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
try {
|
|
265
|
+
let msgArr = []; // 返回的消息列表
|
|
266
|
+
if (config.autoReply) {
|
|
267
|
+
console.log('开启了机器人自动回复功能');
|
|
268
|
+
msgArr = await dispatch.dispatchAiBot({ bot: config.defaultBot, isMention, msg, name, userAlias, userWeixin, uname: name, uid: id, roomName: roomName || '', roomId: roomId || '', id: `${roomId ? roomId + '_' : ''}${id}`, file });
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
console.log('没有开启机器人自动回复功能');
|
|
272
|
+
msgArr = [{ type: 1, content: '', url: '' }];
|
|
273
|
+
}
|
|
274
|
+
return msgArr;
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
console.log('robotMsg error:', e);
|
|
278
|
+
return [];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async function getCustomConfig({ name, id, room, roomId, roomName, type }) {
|
|
283
|
+
const gptConfigs = globalConfig.getAllGptConfig();
|
|
284
|
+
if (gptConfigs && gptConfigs.length) {
|
|
285
|
+
let finalConfig = '';
|
|
286
|
+
if (room) {
|
|
287
|
+
finalConfig = room && gptConfigs.find((item) => {
|
|
288
|
+
if (item?.isAllRoom) {
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
const targetNames = [];
|
|
292
|
+
const targetIds = [];
|
|
293
|
+
item.targets.forEach(tItem => {
|
|
294
|
+
targetNames.push(tItem.name);
|
|
295
|
+
targetIds.push(tItem.id);
|
|
296
|
+
});
|
|
297
|
+
if (type) {
|
|
298
|
+
if (item.onlyId) {
|
|
299
|
+
if (item?.allowRoomMembers && item?.allowRoomMembers.length) {
|
|
300
|
+
const allowRoomMembersIds = item.allowRoomMembers.map(mItem => mItem.id);
|
|
301
|
+
const res = item.type === 'room' && targetIds.includes(roomId) && item.botConfig[type] && item.openChat && allowRoomMembersIds.includes(id);
|
|
302
|
+
return res;
|
|
303
|
+
}
|
|
304
|
+
return item.type === 'room' && targetIds.includes(roomId) && item.botConfig[type] && item.openChat;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
if (item?.allowRoomMembers && item?.allowRoomMembers.length) {
|
|
308
|
+
const allowRoomMembersIds = item.allowRoomMembers.map(mItem => mItem.id);
|
|
309
|
+
const allowRoomMembersNames = item.allowRoomMembers.map(mItem => mItem.name);
|
|
310
|
+
const res = item.type === 'room' && (targetNames.includes(roomName) && item.botConfig[type] && item.openChat || targetIds.includes(roomId) && item.botConfig[type] && item.openChat) && (allowRoomMembersIds.includes(id) || allowRoomMembersNames.includes(name));
|
|
311
|
+
return res;
|
|
312
|
+
}
|
|
313
|
+
return item.type === 'room' && (targetNames.includes(roomName) && item.botConfig[type] && item.openChat || targetIds.includes(roomId) && item.botConfig[type] && item.openChat);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
if (item.onlyId) {
|
|
318
|
+
if (item?.allowRoomMembers && item?.allowRoomMembers.length) {
|
|
319
|
+
const allowRoomMembersIds = item.allowRoomMembers.map(mItem => mItem.id);
|
|
320
|
+
const res = item.type === 'room' && targetIds.includes(roomId) && allowRoomMembersIds.includes(id);
|
|
321
|
+
console.log('校验是否为允许的群成员:', res);
|
|
322
|
+
return res;
|
|
323
|
+
}
|
|
324
|
+
return item.type === 'room' && targetIds.includes(roomId);
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
if (item?.allowRoomMembers && item?.allowRoomMembers.length) {
|
|
328
|
+
const allowRoomMembersIds = item.allowRoomMembers.map(mItem => mItem.id);
|
|
329
|
+
const allowRoomMembersNames = item.allowRoomMembers.map(mItem => mItem.name);
|
|
330
|
+
const res = item.type === 'room' && (targetNames.includes(roomName) || targetIds.includes(roomId)) && (allowRoomMembersIds.includes(id) || allowRoomMembersNames.includes(name));
|
|
331
|
+
console.log('校验是否为允许的群成员:', res);
|
|
332
|
+
return res;
|
|
333
|
+
}
|
|
334
|
+
return item.type === 'room' && (targetNames.includes(roomName) || targetIds.includes(roomId));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
finalConfig = !room && gptConfigs.find((item) => {
|
|
341
|
+
if (item?.isAllFriend) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
const targetNames = [];
|
|
345
|
+
const targetIds = [];
|
|
346
|
+
item.targets.forEach(tItem => {
|
|
347
|
+
targetNames.push(tItem.name);
|
|
348
|
+
targetIds.push(tItem.id);
|
|
349
|
+
});
|
|
350
|
+
if (type) {
|
|
351
|
+
if (item.onlyId) {
|
|
352
|
+
return item.type === 'contact' && targetIds.includes(id) && item.botConfig[type] && item.openChat;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
return item.type === 'contact' && (targetNames.includes(name) && item.botConfig[type] && item.openChat || targetIds.includes(id) && item.botConfig[type] && item.openChat);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
if (item.onlyId) {
|
|
360
|
+
return item.type === 'contact' && targetIds.includes(id);
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
return item.type === 'contact' && (targetNames.includes(name) || targetIds.includes(id));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
return finalConfig;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
async function customChat({ msg, name, id, config, isMention, room, userAlias, userWeixin, roomId, roomName, file }) {
|
|
372
|
+
try {
|
|
373
|
+
const gptConfigs = globalConfig.getAllGptConfig();
|
|
374
|
+
if (gptConfigs && gptConfigs.length) {
|
|
375
|
+
const finalConfig = await getCustomConfig({ name, id, room, roomId, roomName });
|
|
376
|
+
if (finalConfig) {
|
|
377
|
+
const isRoom = finalConfig.type === 'room';
|
|
378
|
+
if (finalConfig.openChat) {
|
|
379
|
+
if ((isRoom && finalConfig.needAt === 1 && isMention) || isRoom && !finalConfig.needAt || !isRoom || file && finalConfig?.openFileAnalyze) {
|
|
380
|
+
const keyword = finalConfig?.keywords.find((item) => msg.includes(item));
|
|
381
|
+
if (keyword || !finalConfig?.keywords.length || file && finalConfig?.openFileAnalyze) {
|
|
382
|
+
if (!!finalConfig?.replaceKeyword) {
|
|
383
|
+
msg = keyword ? msg.replace(keyword, '') : msg;
|
|
384
|
+
}
|
|
385
|
+
if (finalConfig.limitNum > 0 && finalConfig.limitNum <= finalConfig.usedNum) {
|
|
386
|
+
return [{ type: 1, content: finalConfig.rechargeTip || '' }];
|
|
387
|
+
}
|
|
388
|
+
const msgArr = await dispatchBot({
|
|
389
|
+
botType: finalConfig.robotType,
|
|
390
|
+
content: msg,
|
|
391
|
+
id: `${roomId ? roomId + '_' : ''}${id}`,
|
|
392
|
+
adminId: finalConfig.id,
|
|
393
|
+
config: finalConfig.botConfig,
|
|
394
|
+
uid: id,
|
|
395
|
+
uname: name,
|
|
396
|
+
isMention,
|
|
397
|
+
roomId,
|
|
398
|
+
userAlias,
|
|
399
|
+
userWeixin,
|
|
400
|
+
roomName,
|
|
401
|
+
file
|
|
402
|
+
});
|
|
403
|
+
if (msgArr.length)
|
|
404
|
+
return msgArr;
|
|
405
|
+
console.log('自定义回复获取内容失败,启用全局配置');
|
|
406
|
+
return [];
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return [];
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
// 如果没有开启对话 也要检测一下是不是需要@ 才返回默认回复
|
|
413
|
+
if ((isRoom && finalConfig.needAt === 1 && isMention) || isRoom && !finalConfig.needAt || !isRoom) {
|
|
414
|
+
return finalConfig.defaultReply ? [{ type: 1, content: finalConfig.defaultReply }] : [{
|
|
415
|
+
type: 1,
|
|
416
|
+
content: ''
|
|
417
|
+
}];
|
|
418
|
+
}
|
|
419
|
+
return [];
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return [];
|
|
423
|
+
}
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
catch (e) {
|
|
427
|
+
console.log('catch error:' + e);
|
|
428
|
+
return [];
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* 校验禁止词
|
|
433
|
+
* @param msg
|
|
434
|
+
* @param name
|
|
435
|
+
* @param config
|
|
436
|
+
* @return {*[]}
|
|
437
|
+
*/
|
|
438
|
+
function preventWordCheck({ msg, config, isMention, room }) {
|
|
439
|
+
const preventWords = config.preventWords.replaceAll(',', ',').split(',');
|
|
440
|
+
// 如果是群消息,但是没有提及机器人,则不需要返回任何消息 因为可能是正常交流而已
|
|
441
|
+
if (room && !isMention) {
|
|
442
|
+
return [];
|
|
443
|
+
}
|
|
444
|
+
if (preventWords && preventWords.length) {
|
|
445
|
+
for (let item of preventWords) {
|
|
446
|
+
if (item && item !== '' && msg.includes(item)) {
|
|
447
|
+
console.log(`触发禁止词【${item}】,不回复用户`);
|
|
448
|
+
return [{ type: 1, content: '这个话题不适合讨论,换个话题吧。' }];
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return [];
|
|
453
|
+
}
|
|
454
|
+
async function customBot({ that, msg, name, userAlias, userWeixin, id, config, room, isMention, file }) {
|
|
455
|
+
const item = config.customBot;
|
|
456
|
+
// 如果匹配到关键词 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发
|
|
457
|
+
if ((room && item.needAt === 1 && !isMention) || (room && item.needAt === undefined && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) {
|
|
458
|
+
return [];
|
|
459
|
+
}
|
|
460
|
+
const contactSelf = await getUser();
|
|
461
|
+
msg = msg.trim();
|
|
462
|
+
const topic = room ? await room.topic() : '';
|
|
463
|
+
const data = {
|
|
464
|
+
robotId: contactSelf.robotId,
|
|
465
|
+
robotName: contactSelf.name,
|
|
466
|
+
uid: id,
|
|
467
|
+
uname: name,
|
|
468
|
+
ualias: userAlias,
|
|
469
|
+
uweixin: userWeixin,
|
|
470
|
+
roomId: (room && room.id) || '',
|
|
471
|
+
roomName: (room && topic) || '',
|
|
472
|
+
word: msg
|
|
473
|
+
};
|
|
474
|
+
item.moreData &&
|
|
475
|
+
item.moreData.length &&
|
|
476
|
+
item.moreData.forEach((mItem) => {
|
|
477
|
+
if (mItem.key !== 'uid' && mItem.key !== 'uname' && mItem.key !== 'ualias' && mItem.key !== 'uweixin' && mItem.key !== 'word' && mItem.key !== 'roomId' && mItem.key !== 'roomName' && mItem.key !== 'robotId' && mItem.key !== 'robotName') {
|
|
478
|
+
data[mItem.key] = mItem.value;
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
const timeout = item.timeout || 60;
|
|
482
|
+
let res = await service.post(item.customUrl, data, { timeout: timeout * 1000 });
|
|
483
|
+
return res;
|
|
484
|
+
}
|
|
485
|
+
function checkKeyword(forward, msg) {
|
|
486
|
+
const keywords = forward.keywords;
|
|
487
|
+
const reg = forward.reg;
|
|
488
|
+
for (let key of keywords) {
|
|
489
|
+
if ((reg === 1 && msg.includes(key)) || (reg === 2 && msg === key)) {
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
function getForwardConfig({ name, id, room, roomId, roomName, msg, config }) {
|
|
496
|
+
const configs = config.forwards;
|
|
497
|
+
if (configs && configs.length) {
|
|
498
|
+
let finalConfig = '';
|
|
499
|
+
if (room) {
|
|
500
|
+
finalConfig = room && configs.find((item) => {
|
|
501
|
+
const targetNames = [];
|
|
502
|
+
const targetIds = [];
|
|
503
|
+
item.targets.forEach(tItem => {
|
|
504
|
+
targetNames.push(tItem.name);
|
|
505
|
+
targetIds.push(tItem.id);
|
|
506
|
+
});
|
|
507
|
+
const keywordCheck = checkKeyword(item, msg);
|
|
508
|
+
return keywordCheck && item.type === 'room' && (targetNames.includes(roomName) || targetIds.includes(roomId));
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
finalConfig = configs.find((item) => {
|
|
513
|
+
const targetNames = [];
|
|
514
|
+
const targetIds = [];
|
|
515
|
+
item.targets.forEach(tItem => {
|
|
516
|
+
targetNames.push(tItem.name);
|
|
517
|
+
targetIds.push(tItem.id);
|
|
518
|
+
});
|
|
519
|
+
const keywordCheck = checkKeyword(item, msg);
|
|
520
|
+
return keywordCheck && item.type === 'contact' && (targetNames.includes(name) || targetIds.includes(id));
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
return finalConfig;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
async function getTargets(that, targets, type) {
|
|
527
|
+
const finalTargets = [];
|
|
528
|
+
if (type === 'room') {
|
|
529
|
+
for (let item of targets) {
|
|
530
|
+
try {
|
|
531
|
+
const room = await that.Room.find({ id: item.id, topic: item.name });
|
|
532
|
+
finalTargets.push(room);
|
|
533
|
+
}
|
|
534
|
+
catch (e) {
|
|
535
|
+
console.log('查询转发群组失败', e);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
else {
|
|
540
|
+
for (let item of targets) {
|
|
541
|
+
try {
|
|
542
|
+
const room = await that.Contact.find({ id: item.id, name: item.name });
|
|
543
|
+
finalTargets.push(room);
|
|
544
|
+
}
|
|
545
|
+
catch (e) {
|
|
546
|
+
console.log('查询转发好友失败', e);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return finalTargets;
|
|
551
|
+
}
|
|
552
|
+
export async function keywordForward({ that, msg, name, id, config, room, isMention, roomId, roomName }) {
|
|
553
|
+
try {
|
|
554
|
+
const forwards = config.forwards || [];
|
|
555
|
+
if (forwards.length) {
|
|
556
|
+
const finalConfig = getForwardConfig({ name, id, room, msg, config, roomId, roomName });
|
|
557
|
+
if (finalConfig) {
|
|
558
|
+
const finalTargets = await getTargets(that, finalConfig.forwardTargets, finalConfig.forwardType);
|
|
559
|
+
if (finalTargets.length) {
|
|
560
|
+
const eol = await getPuppetEol();
|
|
561
|
+
for (let contact of finalTargets) {
|
|
562
|
+
const content = room ? `【关键词转发】${eol} 时间:${dayjs().format('MM-DD HH:mm:ss')} ${eol}收到群聊【${roomName}】中【${name}】发送的内容:${msg}` : `【关键词转发】${eol} 时间:${dayjs().format('MM-DD HH:mm:ss')} ${eol}收到好友【${name}】发送的内容:${msg}`;
|
|
563
|
+
await contact.say(content);
|
|
564
|
+
await delay(3000);
|
|
565
|
+
}
|
|
566
|
+
return [];
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return [];
|
|
571
|
+
}
|
|
572
|
+
catch (e) {
|
|
573
|
+
console.log('转发消息失败', e);
|
|
574
|
+
return [];
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
export async function summerChat({ that, msg, name, id, config, room, userAlias, userWeixin, isMention, roomId, roomName }) {
|
|
578
|
+
const { role } = config.userInfo;
|
|
579
|
+
const item = config.summerConfig;
|
|
580
|
+
// 如果关闭了 或者关键词没有匹配上 直接不需要总结回复
|
|
581
|
+
if (!item.open || !checkKeyword(item, msg) || role !== 'vip')
|
|
582
|
+
return [];
|
|
583
|
+
// 群消息要求是必须@,但是没@ 就不需要回复 || 当为群消息关键词只在好友私聊时触发 || 非群消息只在群中触发
|
|
584
|
+
if ((room && item.needAt === 1 && !isMention) || (room && item.scope === 'friend') || (!room && item.scope === 'room')) {
|
|
585
|
+
return [];
|
|
586
|
+
}
|
|
587
|
+
let histories = [];
|
|
588
|
+
if (item.summerType === 'time') {
|
|
589
|
+
histories = await getHistoryByTime({ id: room ? roomId : id, name: room ? roomName : name }, item.summerTime);
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
histories = await getHistoryByNum({ id: room ? roomId : id, name: room ? roomName : name }, item.summerNum);
|
|
593
|
+
}
|
|
594
|
+
let content = '';
|
|
595
|
+
if (histories.length) {
|
|
596
|
+
for (const item of histories) {
|
|
597
|
+
if (item.content && item.content !== 'undefined' && item.content !== 'null' && item.content) {
|
|
598
|
+
content = `${content}\n对话时间:${dayjs.unix(item.time).format('YYYY-MM-DD HH:mm:ss')}:\n用户昵称:${item.chatName}:\n对话内容:${item.content}\n`;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (item?.debug) {
|
|
602
|
+
console.log('获取到的聊天内容', content);
|
|
603
|
+
}
|
|
604
|
+
console.log('开始总结聊天内容');
|
|
605
|
+
const res = await dispatch.dispatchSummerBot({ content, config: item, uid: id, userAlias, userWeixin, uname: name, roomId: roomId, roomName, id: room ? `${roomId ? roomId + '_' : ''}${id}` : id });
|
|
606
|
+
return res;
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
console.log('没有获取到任何聊天记录内容,无法进行总结,请确认已经开启聊天记录');
|
|
610
|
+
return [{ type: 1, content: '' }];
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
export { customBot };
|
|
614
|
+
export { callbackEvent };
|
|
615
|
+
export { emptyMsg };
|
|
616
|
+
export { officialMsg };
|
|
617
|
+
export { newFriendMsg };
|
|
618
|
+
export { roomInviteMsg };
|
|
619
|
+
export { scheduleJobMsg };
|
|
620
|
+
export { eventMsg };
|
|
621
|
+
export { keywordsMsg };
|
|
622
|
+
export { robotMsg };
|
|
623
|
+
export { maxLengthMsg };
|
|
624
|
+
export { customChat };
|
|
625
|
+
export { preventWordCheck };
|
|
626
|
+
export { getCustomConfig };
|
|
627
|
+
export default {
|
|
628
|
+
getCustomConfig,
|
|
629
|
+
summerChat,
|
|
630
|
+
customBot,
|
|
631
|
+
callbackEvent,
|
|
632
|
+
emptyMsg,
|
|
633
|
+
officialMsg,
|
|
634
|
+
newFriendMsg,
|
|
635
|
+
roomInviteMsg,
|
|
636
|
+
scheduleJobMsg,
|
|
637
|
+
eventMsg,
|
|
638
|
+
keywordsMsg,
|
|
639
|
+
robotMsg,
|
|
640
|
+
maxLengthMsg,
|
|
641
|
+
customChat,
|
|
642
|
+
preventWordCheck,
|
|
643
|
+
keywordForward
|
|
644
|
+
};
|
|
645
|
+
//# sourceMappingURL=msg-filters.js.map
|