-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (49 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
56
57
58
59
60
61
'use strict'
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const Bot = require('./bot/platform');
require('dotenv').config();
const app = express();
app.set('port', (process.env.PORT || 5000));
// data processing
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
// routes
app.get('/', function(req, res) {
res.send('Hi I am a chatbot');
})
app.get('/webhook', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === process.env.VALIDATION_TOKEN) {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.log("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
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(function(pageEntry) {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
pageEntry.messaging.forEach(function(messagingEvent) {
if (messagingEvent.message) {
Bot.receivedMessage(messagingEvent);
} else {
console.log("Webhook received unknown messagingEvent: ", messagingEvent);
}
});
});
res.sendStatus(200);
}
});
// start server
app.listen(app.get('port'), function() {
console.log("running on port "+app.get('port'));
})