-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrefactor-index.js
47 lines (41 loc) · 1.22 KB
/
refactor-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
require('dotenv').config();
const line = require('@line/bot-sdk');
const express = require('express');
const { Configuration, OpenAIApi } = require("openai");
const app = express();
const client = new line.Client({
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET,
});
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY,
}));
app.post('/webhook', line.middleware({
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET,
}), async (req, res) => {
try {
const results = await Promise.all(req.body.events.map(handleEvent));
res.json(results);
} catch (err) {
console.error(err);
res.status(500).end();
}
});
const handleEvent = async (event) => {
if (event.type !== 'message' || event.message.type !== 'text') {
return null;
}
const { data: { choices } } = await openai.createCompletion({
prompt: event.message.text,
model: "text-davinci-003",
max_tokens: 1000
});
const message = choices[0].text.trim();
return client.replyMessage(event.replyToken, {
type: 'text',
text: message
});
};
app.listen(3000);
console.log('Linebot is running on 3000 port');