-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
46 lines (39 loc) · 1.29 KB
/
bot.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
'use strict'
const express = require('express'),
bodyParser = require('body-parser'),
validator = require('./validator.js'),
postback = require('./webhook/postback.js'),
senderActions = require('./sender_actions.js'),
message = require('./webhook/message.js')
const app = express()
app.use(bodyParser.json())
app.set('port', (process.env.PORT || 5000))
app.get('/webhook', validator.validateWebhook)
/*
* It is extremely important to return a 200 OK HTTP as fast as possible.
* Facebook will wait for a 200 before sending you the next message. In high
* volume bots, a delay in returning a 200 can cause significant delays in
* Facebook delivering messages to your webhook.
*/
app.post('/webhook', function(request, response) {
let body = request.body
if (body && body.object === 'page' && body.entry) {
for (let entry of body.entry) {
for (let event of entry.messaging) {
senderActions.markAsSeen(event.sender, () => {
if (event.postback) {
postback.handle(event)
} else if (event.message) {
message.handle(event)
}
})
}
}
response.sendStatus(200)
} else {
response.sendStatus(400)
}
})
app.listen(app.get('port'), function() {
console.log('Node app running on port ', app.get('port'))
})