forked from wangrongding/wechat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendMessage.js
114 lines (108 loc) · 3.68 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
// import { getChatGPTReply as getReply } from '../chatgpt/index.js'
import { getOpenAiReply as getReply } from '../openai/index.js'
import { botName, roomWhiteList, aliasWhiteList } from '../../config.js'
/**
* 默认消息发送
* @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.includes(`${botName}`) // 是否在群聊白名单内并且艾特了机器人
const isAlias = aliasWhiteList.includes(remarkName) || aliasWhiteList.includes(name) // 发消息的人是否在联系人白名单内
const isBotSelf = botName === remarkName || botName === name // 是否是机器人自己
// TODO 你们可以根据自己的需求修改这里的逻辑
if (isText && !isBotSelf) {
console.log(JSON.stringify(msg))
if ((Date.now() - 1e3 * msg.payload.timestamp) > 3000) return
if (!content.startsWith('? ') && !content.startsWith('? ') && !content.startsWith('> ')) return
try {
const trimed = content.substr(2)
if (trimed.length < 5) return
// 区分群聊和私聊
if (isRoom && room) {
await room.say(await getReply(trimed.replace(`${botName}`, '')))
return
}
// 私人聊天,白名单内的直接发送
if (isAlias && !room) {
await contact.say(await getReply(trimed))
}
} 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
}