-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathapp.js
48 lines (42 loc) · 1.17 KB
/
app.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
let express = require('express')
let bodyParser = require('body-parser')
let request = require('request')
let app = express()
const CHANNEL_ACCESS_TOKEN = 'Your_Channel_Access_Token'
const PORT = process.env.PORT || 3000
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.listen(PORT, function () {
console.log(`App listening on port ${PORT}!`)
})
// handler receiving messages
app.post('/', function (req, res) {
})
// generic function sending messages
function sendMessage(replyToken, text) {
let body = {
replyToken,
messages: [{
type: 'text',
text,
}],
};
let options = {
url: 'https://api.line.me/v2/bot/message/reply',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${CHANNEL_ACCESS_TOKEN}`,
},
body,
json: true,
};
request(options, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
}
else if (response.body.error) {
console.log('Error: ', response.body.error);
}
})
}