-
Notifications
You must be signed in to change notification settings - Fork 2
/
linebot.js
41 lines (34 loc) · 1.02 KB
/
linebot.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
const line = require('@line/bot-sdk');
const express = require('express');
const config = require('./config.js');
const mrDecisionBot = require('./mrDecisionBot.js');
const conf = {
channelAccessToken: config.CHANNEL_ACCESS_TOKEN,
channelSecret: config.CHANNEL_SECRET,
};
const client = new line.Client(conf);
const app = express();
app.use(express.static('./public'));
app.post('/', line.middleware(conf), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.dir(err);
res.status(500).end();
});
});
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
let response = mrDecisionBot.process(event);
if(response !== null) {
const echo = { type: 'text', text: response.a};
return client.replyMessage(event.replyToken, echo);
}
}
const port = 3000;
app.listen(port, () => {
console.log(`listening on ${port}`);
});