forked from kamnan43/line-bot-nodejs-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (98 loc) · 3.24 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
'use strict';
const line = require('@line/bot-sdk');
const express = require('express');
const config = require('./config.json');
// create LINE SDK client
const client = new line.Client(config);
const app = express();
// webhook callback
app.post('/webhook', line.middleware(config), (req, res) => {
// req.body.events should be an array of events
if (!Array.isArray(req.body.events)) {
return res.status(500).end();
}
// handle events separately
Promise.all(req.body.events.map(event => {
console.log('event', event);
// check verify webhook event
if (event.replyToken === '00000000000000000000000000000000' ||
event.replyToken === 'ffffffffffffffffffffffffffffffff') {
return;
}
return handleEvent(event);
}))
.then(() => res.end())
.catch((err) => {
console.error(err);
res.status(500).end();
});
});
// simple reply function
const replyText = (token, texts) => {
texts = Array.isArray(texts) ? texts : [texts];
return client.replyMessage(
token,
texts.map((text) => ({ type: 'text', text }))
);
};
// callback function to handle a single event
function handleEvent(event) {
switch (event.type) {
case 'message':
const message = event.message;
switch (message.type) {
case 'text':
return handleText(message, event.replyToken);
case 'image':
return handleImage(message, event.replyToken);
case 'video':
return handleVideo(message, event.replyToken);
case 'audio':
return handleAudio(message, event.replyToken);
case 'location':
return handleLocation(message, event.replyToken);
case 'sticker':
return handleSticker(message, event.replyToken);
default:
throw new Error(`Unknown message: ${JSON.stringify(message)}`);
}
case 'follow':
return replyText(event.replyToken, 'Got followed event');
case 'unfollow':
return console.log(`Unfollowed this bot: ${JSON.stringify(event)}`);
case 'join':
return replyText(event.replyToken, `Joined ${event.source.type}`);
case 'leave':
return console.log(`Left: ${JSON.stringify(event)}`);
case 'postback':
let data = event.postback.data;
return replyText(event.replyToken, `Got postback: ${data}`);
case 'beacon':
const dm = `${Buffer.from(event.beacon.dm || '', 'hex').toString('utf8')}`;
return replyText(event.replyToken, `${event.beacon.type} beacon hwid : ${event.beacon.hwid} with device message = ${dm}`);
default:
throw new Error(`Unknown event: ${JSON.stringify(event)}`);
}
}
function handleText(message, replyToken) {
return replyText(replyToken, message.text);
}
function handleImage(message, replyToken) {
return replyText(replyToken, 'Got Image');
}
function handleVideo(message, replyToken) {
return replyText(replyToken, 'Got Video');
}
function handleAudio(message, replyToken) {
return replyText(replyToken, 'Got Audio');
}
function handleLocation(message, replyToken) {
return replyText(replyToken, 'Got Location');
}
function handleSticker(message, replyToken) {
return replyText(replyToken, 'Got Sticker');
}
const port = config.port;
app.listen(port, () => {
console.log(`listening on ${port}`);
});