-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_webhook.js
65 lines (60 loc) · 1.45 KB
/
test_webhook.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
'use strict'
const line = require('./index')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
// need raw buffer for signature validation
app.use(bodyParser.json({
verify (req, res, buf) {
req.rawBody = buf
}
}))
// init with auth
line.init({
accessToken: 'YOUR_ACCESS_TOKEN',
// (Optional) for webhook signature validation
channelSecret: 'YOUR_CHANNEL_SECRET'
})
/**
* response example (https://devdocs.line.me/ja/#webhook):
* {
* "events": [
* {
* "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
* "type": "message",
* "timestamp": 1462629479859,
* "source": {
* "type": "user",
* "userId": "u206d25c2ea6bd87c17655609a1c37cb8"
* },
* "message": {
* "id": "325708",
* "type": "text",
* "text": "Hello, world"
* }
* }
* ]
* }
*/
app.post('/webhook/', line.validator.validateSignature(), (req, res, next) => {
// get content from request body
const promises = req.body.events.map(event => {
// reply message
return line.client
.replyMessage({
replyToken: event.replyToken,
messages: [
{
type: 'text',
text: event.message.text
}
]
})
})
Promise
.all(promises)
.then(() => res.json({success: true}))
})
app.listen(process.env.PORT || 3000, () => {
console.log('Example app listening on port 3000!')
})