-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.54 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
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var { receivedMessage } = require('./MessageHandler')
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.listen((process.env.PORT || 3000))
// Server frontpage
app.get('/', function (req, res) {
res.send('This is TestBot Server')
})
// Facebook Webhook
app.get('/webhook', function (req, res) {
if (req.query['hub.verify_token'] === 'testbot_verify_token') {
res.send(req.query['hub.challenge'])
} else {
res.send('Invalid verify token')
}
})
// handler receiving messages
app.post('/webhook', function (req, res) {
var data = req.body
// Make sure this is a page subscription
if (data.object === 'page') {
// Iterate over each entry - there may be multiple if batched
data.entry.forEach((entry) => {
// var pageID = entry.id
// var timeOfEvent = entry.time
// Iterate over each messaging event
entry.messaging.map((event) => {
console.log('event', event)
if (event.message) {
receivedMessage(event)
.then((data) => console.log(data))
.catch((error) => console.error(error))
} else {
console.log('Webhook received unknown event: ', event)
}
})
})
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know
// you've successfully received the callback. Otherwise, the request
// will time out and we will keep trying to resend.
res.sendStatus(200)
}
})