forked from fbsamples/graph-api-webhooks-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.56 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
62
/**
* Copyright 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var xhub = require('express-x-hub');
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'));
app.use(xhub({ algorithm: 'sha1', secret: process.env.APP_SECRET }));
app.use(bodyParser.json());
var token = process.env.TOKEN || 'token';
var received_updates = [];
app.get('/', function(req, res) {
console.log(req);
res.send('<pre>' + JSON.stringify(received_updates, null, 2) + '</pre>');
});
app.get(['/facebook', '/instagram'], function(req, res) {
if (
req.query['hub.mode'] == 'subscribe' &&
req.query['hub.verify_token'] == token
) {
res.send(req.query['hub.challenge']);
} else {
res.sendStatus(400);
}
});
app.post('/facebook', function(req, res) {
console.log('Facebook request body:', req.body);
if (!req.isXHubValid()) {
console.log('Warning - request header X-Hub-Signature not present or invalid');
res.sendStatus(401);
return;
}
console.log('request header X-Hub-Signature validated');
// Process the Facebook updates here
received_updates.unshift(req.body);
res.sendStatus(200);
});
app.post('/instagram', function(req, res) {
console.log('Instagram request body:');
console.log(req.body);
// Process the Instagram updates here
received_updates.unshift(req.body);
res.sendStatus(200);
});
app.listen();