Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (48 loc) · 1.58 KB
/
Copy pathapp.js
File metadata and controls
62 lines (48 loc) · 1.58 KB
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
import express from "express";
import { config } from "dotenv";
import bodyParser from 'body-parser';
import bodyParserXml from 'body-parser-xml';
import getAIChat from "./openai.js";
import { Message } from "./message.js";
import { initAccessToken } from "./config.js";
config();
bodyParserXml(bodyParser);
const app = express();
const PORT = process.env.PORT;
const message = new Message();
/*message.log();*/
/*config parser for body*/
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.xml());
/*health check for render*/
app.get('/healthz', function (req, res, next) {
res.status(200).end();
});
/*receive server url setting*/
app.get('/message', function (req, res, next) {
message.urlSetting(req, res);
});
/*passive message response*/
app.post('/message', function (req, res, next) {
message.getMsgObj(req).then(result => {
const question = result?.Content[0];
//const question = "what's the day today?";
console.log(question);
const toUser = result.FromUserName[0];
message.reply(res, { type: 'text', content: '正在生成回答' }, toUser);
getAIChat(question).then(result => {
const content = result?.data?.choices[0]?.message?.content;
if(!!content) {
const answer = Buffer.from(content, 'utf-8').toString();
console.log(answer);
message.sendMsg(answer, toUser);
}
})
})
});
/*init access_token*/
initAccessToken();
app.listen(PORT, () => {
console.log(`Server Running on Port:${PORT}`);
});