-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendMessage.js
155 lines (136 loc) · 4.12 KB
/
sendMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { dallEImageReply as getImgReply } from '../openai/index.js'
import {chat} from "../openai/AIFactory.js";
import { botName, roomWhiteList, aliasWhiteList, timeoutLimit } from '../../config.js'
import { FileBox } from 'file-box'
/**
* 默认消息发送
* @param msg
* @param bot
* @returns {Promise<void>}
*/
export async function defaultMessage(msg, bot) {
// 发消息人
const contact = msg.talker()
// 消息接收人
const receiver = msg.to()
// 消息内容
const content = msg.text()
// 是否是群消息
const room = msg.room()
// 群名称
const roomName = (await room?.topic()) || null
// 发消息人昵称
const alias = (await contact.alias()) || (await contact.name())
// 备注名称
const remarkName = await contact.alias()
// 微信名称
const name = await contact.name()
// 消息类型是否为文本
const isText = msg.type() === bot.Message.Type.Text
// 是否在群聊白名单内并且艾特了机器人
const isRoom = roomWhiteList.includes(roomName)
&&
(content.indexOf(`@${botName}`) === 0 || content.includes('<br/>- - - - - - - - - - - - - - -<br/>@' + botName))
// 发消息的人是否在联系人白名单内
const isAlias = aliasWhiteList.includes(remarkName) || aliasWhiteList.includes(name)
// 是否是机器人自己
const isBotSelf = botName === remarkName || botName === name
// TODO 你们可以根据自己的需求修改这里的逻辑
// console.log(msg)
if (isText && ! isBotSelf) {
// 超过时限的过时消息不恢复
if ((Date.now() - 1e3 * msg.payload.timestamp) > timeoutLimit) return
try {
// 过滤单个字的消息
if (content.length < 2) return
// 区分群聊和私聊
if (isRoom) {
// 去掉机器人name
const prompt = content.replaceAll(`@${botName}`, '');
await room.say(await intentionJudgmentAndReply(prompt))
return
}
// 私人聊天,白名单内的直接发送
if (isAlias && ! room) {
await contact.say(await intentionJudgmentAndReply(content))
}
} catch (e) {
console.error(e)
}
}
}
// 意图判断
async function intentionJudgmentAndReply(prompt) {
if (prompt.trim().startsWith('作图//')) {
const url = await getImgReply(prompt);
// 绘图
return FileBox.fromUrl(url);
} else {
// chat模型
return await chat(prompt);
}
}
/**
* 分片消息发送
* @param message
* @param bot
* @returns {Promise<void>}
*/
export async function shardingMessage(message, bot) {
const talker = message.talker()
const isText = message.type() === bot.Message.Type.Text // 消息类型是否为文本
if (talker.self() || message.type() > 10 || (talker.name() === '微信团队' && isText)) {
return
}
const text = message.text()
const room = message.room()
if (! room) {
console.log(`Chat GPT Enabled User: ${talker.name()}`)
const response = await getChatGPTReply(text)
await trySay(talker, response)
return
}
let realText = splitMessage(text)
// 如果是群聊但不是指定艾特人那么就不进行发送消息
if (text.indexOf(`${botName}`) === -1) {
return
}
realText = text.replace(`${botName}`, '')
const topic = await room.topic()
const response = await getChatGPTReply(realText)
const result = `${realText}\n ---------------- \n ${response}`
await trySay(room, result)
}
// 分片长度
const SINGLE_MESSAGE_MAX_SIZE = 500
/**
* 发送
* @param talker 发送哪个 room为群聊类 text为单人
* @param msg
* @returns {Promise<void>}
*/
async function trySay(talker, msg) {
const messages = []
let message = msg
while (message.length > SINGLE_MESSAGE_MAX_SIZE) {
messages.push(message.slice(0, SINGLE_MESSAGE_MAX_SIZE))
message = message.slice(SINGLE_MESSAGE_MAX_SIZE)
}
messages.push(message)
for (const msg of messages) {
await talker.say(msg)
}
}
/**
* 分组消息
* @param text
* @returns {Promise<*>}
*/
async function splitMessage(text) {
let realText = text
const item = text.split('- - - - - - - - - - - - - - -')
if (item.length > 1) {
realText = item[item.length - 1]
}
return realText
}