-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommets-index.js
56 lines (47 loc) · 1.56 KB
/
commets-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
require('dotenv').config();
const line = require('@line/bot-sdk');
const express = require('express');
const { Configuration, OpenAIApi } = require("openai");
// Initialize express app
const app = express();
// Get config values from environment variables
const config = {
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET,
};
// Initialize line client
const client = new line.Client(config);
// Initialize openai api
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY,
}));
// Handle post requests to /webhook
app.post('/webhook', line.middleware(config), async (req, res) => {
try {
// Map handleEvent function over all events and wait for all promises to resolve
const results = await Promise.all(req.body.events.map(handleEvent));
res.json(results);
} catch (err) {
console.error(err);
res.status(500).end();
}
});
// Handle a single event
const handleEvent = async (event) => {
// Only handle message events of type text
if (event.type !== 'message' || event.message.type !== 'text') {
return null;
}
// Get choices from openai response
const { data: { choices } } = await openai.createCompletion({
prompt: event.message.text,
model: "text-davinci-003",
max_tokens: 1000
});
// Send the first choice as a message reply
const message = choices[0].text.trim;
return client.replyMessage(event.replyToken, { type: 'text', text: message });
};
// Start listening on port 3000
app.listen(3000);
console.log('Linebot is running on 3000 port');