-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
457 lines (429 loc) · 13.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
const axios = require("axios");
const fs = require("fs");
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const ffmpeg = require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegPath);
//Mixin Bot
const { BlazeClient } = require("mixin-node-sdk");
const config = require("./config");
const whitelist = require("./whitelist");
const client = new BlazeClient(
{
pin: config.pin,
client_id: config.client_id,
session_id: config.session_id,
pin_token: config.pin_token,
private_key: config.private_key,
},
{ parse: true, syncAck: true }
);
//ChatGPT
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: config.openai_key,
});
const openai = new OpenAIApi(configuration);
//Data
const words = {
function: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
object: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
class: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
method: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
property: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
array: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
string: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
number: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
boolean: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
null: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
undefin: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
promise: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
callbac: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
event: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
module: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
variabl: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
constru: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
prototy: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
iterato: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
decorat: {
numOfStudies: 0,
lastStudyTime: Date.now(),
nextStudyTime: Date.now(),
},
};
//WS
client.loopBlaze({
async onMessage(msg) {
console.log(msg);
if (
(msg.category === "PLAIN_TEXT" && typeof msg.data === "string") ||
msg.category === "PLAIN_AUDIO"
) {
if (whitelist.user_id.includes(msg.user_id)) {
if (msg.category === "PLAIN_AUDIO") {
const id = msg.data.attachment_id;
const url = (await client.showAttachment(id)).view_url;
const reg = /attachments\/(.+)/;
const filename = reg.exec(url)[1];
await getAudio(url, filename);
await convertToMp3(filename);
msg.data = await createTranscription(filename);
await cleanFile(`./audiocach/${filename}.ogg`);
await cleanFile(`./audiocach/${filename}.mp3`);
}
console.log(msg.data);
if (["?", "?", "你好", "Hi"].includes(msg.data)) {
helpMsg = `🧑🏫 发送 / ,随机获取6个单词按钮,点击按钮,开始该单词的对话练习;\n📖 发送 /+内容,为仅使用翻译功能,比如发送: /您好;\n💡 发送 ? ,获取此帮助信息。`;
client.sendMessageText(msg.user_id, helpMsg);
} else if (msg.data === "/") {
sendRandomWords(msg.user_id);
} else if (msg.data.substring(0, 1) === "/") {
const rawData = msg.data.substring(1);
const lang = checkLanguage(rawData);
const rec = await translate(lang, rawData);
const message = rec.rec;
const cost = rec.cost;
console.log(cost);
client.sendMessageText(msg.user_id, `> ${rawData}\n< ${message}`);
} else {
const rawData = msg.data.toString();
const lang = checkLanguage(rawData);
let rawZhData = "";
let rawEnData = "";
let cost = 0;
//处理收到的消息
if (lang === "chinese") {
rawZhData = rawData; //中文
const rec = await translate(lang, rawData);
rawEnData = rec.rec; //英文
cost += rec.cost;
} else if (lang === "english") {
rawEnData = rawData; //英文
const rec = await translate(lang, rawData);
rawZhData = rec.rec; //中文
cost += rec.cost;
} else if (lang === "unknown") {
client.sendMessageText(
msg.user_id,
`Only English and Chinese are supported.\n仅支持英文或中文。`
);
}
updateWordsList(rawEnData);
//处理返回的消息
const conversationReturn = await conversation(
`Please start a conversation with "${rawEnData}".`
);
const returnEnData = conversationReturn.rec; //英文
const costOfReturnFromChatGPT = conversationReturn.cost;
cost += costOfReturnFromChatGPT;
const translateOfConversationReturn = await translate(
"english",
returnEnData
);
const returnZhData = translateOfConversationReturn.rec; //中文
const costOfTranslateEnToZh = translateOfConversationReturn.cost;
cost += costOfTranslateEnToZh;
//最终Cost
console.log(cost);
const rec = `> 用户\n英文:${rawEnData}\n中文:${rawZhData}\n\n< 助手\n英文:${returnEnData}\n中文:${returnZhData}`;
await client.sendMessageText(msg.user_id, rec);
}
} else {
client.sendMessageText(msg.user_id, "服务暂未对外开放。");
}
} else {
client.sendMessageText(
msg.user_id,
"Only supports text.\n仅支持文本或语音消息。"
);
}
},
onAckReceipt() {},
});
//Functions
async function cost(token) {
const cost = (0.002 / 1000) * token;
return cost;
}
function checkLanguage(text) {
// 判断第一个字符的编码范围来确定语言
const firstCharCode = text.charCodeAt(0);
if (firstCharCode >= 0x4e00 && firstCharCode <= 0x9fa5) {
return "chinese";
} else if (firstCharCode >= 0x00 && firstCharCode <= 0x7f) {
return "english";
} else {
// 其他语言,暂不处理
return "unknown";
}
}
async function translate(lang, text) {
if (lang === "chinese") {
msg = [
{
role: "system",
content:
"You are a helpful assistant that translates Chinese to English and replay the only reply the content translated.",
},
{
role: "user",
content: `Translate the following Chinese text to English.: ${text}`,
},
];
} else if (lang === "english") {
msg = [
{
role: "system",
content:
"You are a helpful assistant that translates English to Chinese and replay the only reply the content translated.",
},
{
role: "user",
content: `Translate the following English text to Chinese. : ${text}`,
},
];
}
const rec = (await queryChatGPT(msg)).rec;
const token = (await queryChatGPT(msg)).token;
const costs = await cost(token);
return { rec: rec, cost: costs };
}
async function conversation(text) {
msg = [
{
role: "system",
content:
"First of all, If the question is not a sentence or they are some seprecated words, please help to generate a sentence with the question to start a conversation.I want you to act as a spoken English teacher and improver. I will speak to you in English and you will reply to me in English to practice my spoken English. I want you to keep your reply neat, limiting the reply to 50 words. I want you to strictly correct my grammar mistakes, typos, and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.",
},
{
role: "user",
content: text,
},
];
const rec = (await queryChatGPT(msg)).rec;
const token = (await queryChatGPT(msg)).token;
const costs = await cost(token);
return { rec: rec, cost: costs };
}
async function queryChatGPT(msg) {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: msg,
});
const rec = completion.data.choices[0].message.content.replace(
/^"(.*)"$/,
"$1"
);
const token = completion.data.usage.total_tokens;
return { rec: rec, token: token };
}
function randomWords() {
const keys = Object.keys(words);
const selectedWords = [];
while (selectedWords.length < 6) {
const randomIndex = Math.floor(Math.random() * keys.length);
if (!selectedWords.includes(keys[randomIndex])) {
if (words[keys[randomIndex]].nextStudyTime <= Date.now()) {
selectedWords.push(keys[randomIndex]);
}
}
}
return selectedWords;
}
async function sendRandomWords(user_id) {
const selectedWords = randomWords();
await client.sendMessageText(user_id, "请挑选一个单词开始对话练习。");
await client.sendAppButtonMsg(user_id, [
{
label: selectedWords[0], // 按钮的文本内容
color: "#FF0000", // 16进制按钮的颜色,如: #FF0000
action: `input:${selectedWords[0]}`, // 按钮的跳转链接
},
{
label: selectedWords[1], // 按钮的文本内容
color: "#FF0000", // 16进制按钮的颜色,如: #FF0000
action: `input:${selectedWords[1]}`, // 按钮的跳转链接
},
{
label: selectedWords[2], // 按钮的文本内容
color: "#FF0000", // 16进制按钮的颜色,如: #FF0000
action: `input:${selectedWords[2]}`, // 按钮的跳转链接
},
{
label: selectedWords[3], // 按钮的文本内容
color: "#FF0000", // 16进制按钮的颜色,如: #FF0000
action: `input:${selectedWords[3]}`, // 按钮的跳转链接
},
{
label: selectedWords[4], // 按钮的文本内容
color: "#FF0000", // 16进制按钮的颜色,如: #FF0000
action: `input:${selectedWords[4]}`, // 按钮的跳转链接
},
{
label: selectedWords[5], // 按钮的文本内容
color: "#FF0000", // 16进制按钮的颜色,如: #FF0000
action: `input:${selectedWords[5]}`, // 按钮的跳转链接
},
]);
}
function getNextStudyTime(numOfStudies, lastStudyTime) {
// 计算学习间隔时间
const interval = (Date.now() - lastStudyTime) / (1000 * 60 * 60 * 24);
//interval = 6
let nextStudyTime;
// 根据记忆次数和学习间隔时间计算下一次学习时间
if (numOfStudies === 0) {
nextStudyTime = Date.now() + 24 * 60 * 60 * 1000; // 第一次学习间隔1天
} else if (numOfStudies === 1) {
nextStudyTime = Date.now() + 7 * 24 * 60 * 60 * 1000; // 第二次学习间隔1周
} else {
let days = 0;
const prevInterval = interval / numOfStudies;
if (prevInterval <= 1) {
days = 1;
} else if (prevInterval <= 2) {
days = 2;
} else if (prevInterval <= 4) {
days = 4;
} else if (prevInterval <= 7) {
days = 7;
} else if (prevInterval <= 15) {
days = 15;
} else {
days = 30;
}
nextStudyTime = Date.now() + days * 24 * 60 * 60 * 1000;
}
return nextStudyTime;
}
function checkWord(text) {
const keys = Object.keys(words);
if (keys.includes(text)) {
return true;
} else {
return false;
}
}
function updateWordsList(text) {
text = text.toLowerCase();
if (checkWord(text)) {
words[text].numOfStudies += 1;
words[text].lastStudyTime = Date.now();
words[text].nextStudyTime = getNextStudyTime(
words[text].numOfStudies,
words[text].lastStudyTime
);
}
console.log(words[text]);
}
async function getAudio(url, filename) {
// download the ogg file from the given URL
const response = await axios({
url,
method: "GET",
responseType: "stream",
});
const writer = fs.createWriteStream(`./audiocach/${filename}.ogg`);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
async function convertToMp3(filename) {
return new Promise((resolve, reject) => {
ffmpeg(`./audiocach/${filename}.ogg`)
.outputOptions("-acodec libmp3lame")
.on("end", resolve)
.on("error", reject)
.save(`./audiocach/${filename}.mp3`);
});
}
async function createTranscription(filename) {
const resp = await openai.createTranscription(
fs.createReadStream(`./audiocach/${filename}.mp3`),
"whisper-1"
);
return resp.data.text;
}
async function cleanFile(filename) {
fs.unlink(filename, (err) => {
if (err) throw err;
console.log("文件已删除");
});
}