-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample-bot.js
More file actions
139 lines (129 loc) · 3.4 KB
/
sample-bot.js
File metadata and controls
139 lines (129 loc) · 3.4 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* This is a sample Line bot.
*
* # RUN THIS BOT:
*
* 1. Setup your Line account
*
* 2. Run from command line:
*
* CHANNEL_ID=<YOUR_CHANNEL_ID> CHANNEL_SECRET=<YOUR_CHANNEL_SECRET> CHANNEL_ACCESS_TOKEN=<YOUR_CHANNEL_ACCESS_TOKEN> node sample-bot.js
*
* 3. Listen to port 3000
*
* # This sample is modified from https://github.com/boybundit/linebot
*
*/
const linebot = require('./LineBot.js');
const bot = linebot({
channelId: process.env.CHANNEL_ID,
channelSecret: process.env.CHANNEL_SECRET,
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
verify: true // default=true
});
bot.on('message', function (event) {
switch (event.message.type) {
case 'text':
switch (event.message.text) {
case 'Me':
event.source.profile().then(function (profile) {
return event.reply('Hello ' + profile.displayName + ' ' + profile.userId);
});
break;
case 'Picture':
event.reply({
type: 'image',
originalContentUrl: 'https://d.line-scdn.net/stf/line-lp/family/en-US/190X190_line_me.png',
previewImageUrl: 'https://d.line-scdn.net/stf/line-lp/family/en-US/190X190_line_me.png'
});
break;
case 'Location':
event.reply({
type: 'location',
title: 'LINE Plus Corporation',
address: '1 Empire tower, Sathorn, Bangkok 10120, Thailand',
latitude: 13.7202068,
longitude: 100.5298698
});
break;
case 'Confirm':
event.reply({
type: 'template',
altText: 'this is a confirm template',
template: {
type: 'confirm',
text: 'Are you sure?',
actions: [{
type: 'message',
label: 'Yes',
text: 'yes'
}, {
type: 'message',
label: 'No',
text: 'no'
}]
}
});
break;
case 'Multiple':
return event.reply(['Line 1', 'Line 2', 'Line 3', 'Line 4', 'Line 5']);
break;
default:
event.reply(event.message.text).then(function (data) {
console.log('Success', data);
}).catch(function (error) {
console.log('Error', error);
});
break;
}
break;
case 'image':
event.message.content().then(function (data) {
const s = data.toString('base64').substring(0, 30);
return event.reply('Nice picture! ' + s);
}).catch(function (err) {
return event.reply(err.toString());
});
break;
case 'video':
event.reply('Nice movie!');
break;
case 'audio':
event.reply('Nice song!');
break;
case 'location':
event.reply(['That\'s a good location!', 'Lat:' + event.message.latitude, 'Long:' + event.message.longitude]);
break;
case 'sticker':
event.reply({
type: 'sticker',
packageId: 1,
stickerId: 1
});
break;
default:
event.reply('Unknow message: ' + JSON.stringify(event));
break;
}
});
bot.on('follow', function (event) {
event.reply('follow: ' + event.source.userId);
});
bot.on('unfollow', function (event) {
event.reply('unfollow: ' + event.source.userId);
});
bot.on('join', function (event) {
event.reply('join: ' + event.source.groupId);
});
bot.on('leave', function (event) {
event.reply('leave: ' + event.source.groupId);
});
bot.on('postback', function (event) {
event.reply('postback: ' + event.postback.data);
});
bot.on('beacon', function (event) {
event.reply('beacon: ' + event.beacon.hwid);
});
bot.listen('/linewebhook', process.env.PORT || 3000, function () {
console.log('LineBot is running.');
});