-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinebot.js
58 lines (55 loc) · 1.55 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const linebot = require('linebot');
const config = {
channelId: process.env.channelId,
channelSecret: process.env.channelSecret,
channelAccessToken: process.env.channelAccessToken,
};
const bot = linebot(config);
const botName = process.env.BOT_NAME;
const service = require('./service');
module.exports = bot;
bot.on('message', async function (event) {
try {
if (event.type === 'message' && event.message && event.message.text) {
const message = event.message.text.trim();
if (message === botName)
await event.reply('What trouble you bring now?');
else if (message.indexOf(botName) === 0) {
const command = parseCommand(message);
await service.handleCommand(command, event);
} else if (message.includes(botName))
await event.reply('Huh?');
}
} catch (error) {
console.log(error);
await event.reply('Unknown Error (x_x)');
}
});
// event
// {
// type: 'message',
// replyToken: 'replyToken',
// source: {
// userId: 'userId',
// groupId: 'groupId',
// type: 'group',
// profile: [Function],
// member: [Function]
// },
// timestamp: 1596785042340,
// mode: 'active',
// message: {
// type: 'text',
// id: '12456661773182',
// text: 'Hello',
// content: [Function]
// },
// reply: [Function]
// }
const symbols = [ '!', ',', ':', '!', ',', ':' ];
function parseCommand (message) {
let command = message.replace(botName, '').trim();
if (symbols.includes(command.charAt(0)))
command = command.substring(1);
return command.trim();
}