-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (51 loc) · 1.9 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
56
57
58
59
60
'use strict'
require('dotenv').config()
const { VERIFY_TOKEN } = process.env
const handleMessage = require('./lib/handleMessage')
const handlePostback = require('./lib/handlePostback')
const express = require('express')
const bodyParser = require('body-parser')
const app = express().use(bodyParser.json()) // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'))
app.post('/webhook', (req, res) => {
let body = req.body
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
let webhook_event = entry.messaging[0]
console.log('webhook event', webhook_event)
let sender_psid = webhook_event.sender.id
console.log('Sender PSID: ' + sender_psid)
if (webhook_event.message) {
handleMessage(sender_psid, webhook_event.message)
} else if (webhook_event.postback) {
handlePostback(sender_psid, webhook_event.postback)
}
})
res.status(200).send('EVENT_RECEIVED')
} else {
res.sendStatus(404)
}
})
// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
// Parse the query params
let mode = req.query['hub.mode']
let token = req.query['hub.verify_token']
let challenge = req.query['hub.challenge']
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED')
res.status(200).send(challenge)
} else {
console.log('Wrong Token')
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403)
}
}
})