-
Notifications
You must be signed in to change notification settings - Fork 682
/
sendMessage.js
125 lines (116 loc) · 4.09 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
import dotenv from 'dotenv'
// 加载环境变量
dotenv.config()
const env = dotenv.config().parsed // 环境参数
// 从环境变量中导入机器人的名称
const botName = env.BOT_NAME
// 从环境变量中导入联系人白名单
const aliasWhiteList = env.ALIAS_WHITELIST ? env.ALIAS_WHITELIST.split(',') : []
// 从环境变量中导入群聊白名单
const roomWhiteList = env.ROOM_WHITELIST ? env.ROOM_WHITELIST.split(',') : []
import { getServe } from './serve.js'
/**
* 默认消息发送
* @param msg
* @param bot
* @param ServiceType 服务类型 'GPT' | 'Kimi'
* @returns {Promise<void>}
*/
export async function defaultMessage(msg, bot, ServiceType = 'GPT') {
const getReply = getServe(ServiceType)
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.includes(`${botName}`) // 是否在群聊白名单内并且艾特了机器人
const isAlias = aliasWhiteList.includes(remarkName) || aliasWhiteList.includes(name) // 发消息的人是否在联系人白名单内
const isBotSelf = botName === remarkName || botName === name // 是否是机器人自己
// TODO 你们可以根据自己的需求修改这里的逻辑
if (isBotSelf || !isText) return // 如果是机器人自己发送的消息或者消息类型不是文本则不处理
try {
// 区分群聊和私聊
if (isRoom && room) {
const question = (await msg.mentionText()) || content.replace(`${botName}`, '') // 去掉艾特的消息主体
console.log('🌸🌸🌸 / question: ', question)
const response = await getReply(question)
await room.say(response)
}
// 私人聊天,白名单内的直接发送
if (isAlias && !room) {
console.log('🌸🌸🌸 / content: ', content)
const response = await getReply(content)
await contact.say(response)
}
} catch (e) {
console.error(e)
}
}
/**
* 分片消息发送
* @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
}