-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (135 loc) · 5.53 KB
/
index.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
// const { loginQQBot, QQBotClient } = require('./QQBot');
// const { openaiClient } = require('./ChatGPT')
// const { app } = require('./expressPort')
import dayjs from 'dayjs';
import dotenv from 'dotenv';
import lodash from 'lodash';
import { openaiClient } from './ChatGPT.js';
import { app } from './expressPort.js';
import { loginQQBot, QQBotClient } from './QQBot.js';
dotenv.config();
const keyWord = '哇啦'
const keyWordN = '哇啦啦'
const keyWordNN = '哇啦啦啦'
const handleWebhook = () => {
app.post('/gitlab_pipeline', (req, res) => {
const data = req.body
console.log(req.body)
if (!['pending', 'running'].includes(data.object_attributes.detailed_status)) {
const msg = `流水线
${data.user.name} ${data.project.name} ${data.object_attributes.ref} ${data.object_attributes.detailed_status}
${data.commit.message?.trim()}
${data.project.web_url}/-/pipelines
${dayjs(data.object_attributes.created_at).add(8, 'hour').format('MM-DD HH:mm')}
`
QQBotClient.pickFriend(270692377).sendMsg(msg)
QQBotClient.pickFriend(3357038237).sendMsg(msg)
}
res.send('ok')
})
app.post('/gitlab_issues', (req, res) => {
const data = req.body
console.log(req.body)
const msg = `ISSUES
【${data.object_attributes.state}】${data.object_attributes.title}
${data.object_attributes.url}
`
QQBotClient.pickFriend(270692377).sendMsg(msg)
QQBotClient.pickFriend(3357038237).sendMsg(msg)
res.send('ok')
})
}
const handleGroupMsg = () => {
QQBotClient.on('message.group', async (e) => {
const group = QQBotClient.pickGroup(e.group_id)
const chatHistory = await group.getChatHistory(e.seq, 100)
const chatChain = []
lodash.forEachRight(chatHistory, (item) => {
if (
item.seq === e.seq ||
item.seq === e.source?.seq ||
item.seq === chatChain[0]?.source?.seq
) {
chatChain.unshift(item)
}
})
const toSendContent = chatChain
.map((item) => {
const text =
(
item.message.find(
(item) => item.type === 'text'
)
)?.text || item.raw_message
return text.trim()
})
.join('\n')
console.log('toSendContent', toSendContent)
if (toSendContent.includes('和我说')) {
const tts = toSendContent.replace('和我说', '')
await exec(`python3 /node/python/MoeGoe.py -t ${tts}`)
await exec(`/usr/local/ffmpeg-5.1.1-i686-static/ffmpeg -i /node/python/model/77_2.wav -c:a libopencore_amrnb -ac 1 -ar 8000 -b:a 7.95k -y /node/python/model/77_2.amr`)
e.reply(segment.record('/node/python/model/77_2.amr'))
}
if (toSendContent.includes('设定指令:')) {
instruction = toSendContent.replace('设定指令:', '')
process.env.instruction = instruction
return
}
if (toSendContent.includes('查询指令。')) {
e.reply(process.env.instruction)
return
}
if (toSendContent.includes(keyWord) || toSendContent.includes(keyWordN) || toSendContent.includes(keyWordN)) {
let n = 1
if (toSendContent.includes(keyWordN)) {
n = 2
}
if (toSendContent.includes(keyWordNN)) {
n = 4
}
try {
console.log('-----------------消息发出-----------------')
console.log('messages', [
{ role: 'system', content: process.env.instruction },
...chatChain.map(item => ({
role: item.user_id === process.env.bot_qq ? 'assistant' : 'user', content: item.message.find(
(item) => item.type === 'text'
).text?.replace(keyWordNN, '')?.replace(keyWordN, '')?.replace(keyWord, '') || item.raw_message?.replace(keyWordNN, '')?.replace(keyWordN, '')?.replace(keyWord, ''),
}))
])
console.log('-----------------消息结束-----------------')
const messages = [
{ role: 'system', content: process.env.instruction },
...chatChain.map(item => ({
role: item.user_id === process.env.bot_qq ? 'assistant' : 'user', content: item.message.find(
(item) => item.type === 'text'
).text || item.raw_message,
}))
]
const completion = await openaiClient.createChatCompletion({
model: "gpt-3.5-turbo",
messages: messages,
n: n,
}).catch((e) => {
console.log('errrrrrrr', e)
throw e;
});
completion.data.choices.map(item => {
e.reply(item.message.content.replace(/^\n\n/, '') || '(Empty)')
})
// e.reply((completion.data.choices.map(item => item.message.content.replace(/^\n\n/, '')).join('\n \n') || '(Empty)'), true)
} catch (err) {
console.log(err)
e.reply('机器人又出错了~', true)
}
}
})
}
const init = () => {
loginQQBot()
handleGroupMsg()
handleWebhook()
}
init()
console.log('cwd : ' + process.cwd())