-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
146 lines (127 loc) · 3.93 KB
/
app.js
File metadata and controls
146 lines (127 loc) · 3.93 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const
bodyParser = require('body-parser'),
crypto = require('crypto'),
express = require('express');
const
APP_SECRET = process.env.APP_SECRET,
VERIFY_TOKEN = process.env.VERIFY_TOKEN,
ACCESS_TOKEN = process.env.ACCESS_TOKEN;
// if (!(APP_SECRET && VERIFY_TOKEN && ACCESS_TOKEN)) {
// console.error('Missing config values');
// process.exit(1);
// }
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.json({ verify: verifyRequestSignature }));
/*
* Verify that the callback from Facebook.
*/
function verifyRequestSignature(req, res, buf) {
var signature = req.headers['x-hub-signature'];
console.log('Verify Signature call');
if (!signature) {
// For testing, let's log an error. In production, you should throw an
// error.
console.error('Couldn\'t validate the signature.');
} else {
var elements = signature.split('=');
var signatureHash = elements[1];
var expectedHash = crypto.createHmac('sha1', APP_SECRET).update(buf).digest('hex');
if (signatureHash != expectedHash) {
throw new Error('Couldn\'t validate the request signature.');
}
}
}
app.set('port', process.env.PORT || 5000);
app.get('/hello', function(req, res) {
console.log('Hello console');
res.send('Hello World!')
});
/*
*
* Check that the verify token in the webhook setup is the same token
*
*/
app.get('/', function(req, res) {
console.log('Verify Token get');
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === VERIFY_TOKEN) {
console.log('Validating webhook');
res.status(200).send(req.query['hub.challenge']);
} else {
console.error('Falha na validação. Verifique a validação ed token.');
res.sendStatus(403);
}
});
app.post('/', function (req, res) {
console.log('Post webhook call --');
try {
var data = req.body;
switch (data.object) {
case 'page':
processPageEvents(data);
break;
case 'group':
processGroupEvents(data);
break;
case 'user':
processUserEvents(data);
break;
case 'workplace_security':
processWorkplaceSecurityEvents(data);
break;
default:
console.log('Unhandled Webhook Object', data.object);
}
} catch (e) {
console.error(e);
} finally {
console.log('Finally post call');
res.sendStatus(200);
}
});
function processPageEvents(data) {
data.entry.forEach(function(entry){
let page_id = entry.id;
// Chat messages sent to the page
if(entry.messaging) {
entry.messaging.forEach(function(messaging_event){
console.log('Page Messaging Event',page_id,messaging_event);
});
}
// Related in pages or changes mentions of page
if(entry.changes) {
entry.changes.forEach(function(change){
console.log('Page Change',page_id,change);
});
}
});
}
function processGroupEvents(data) {
data.entry.forEach(function(entry){
let group_id = entry.id;
entry.changes.forEach(function(change){
console.log('Group Change',group_id,change);
});
});
}
function processUserEvents(data) {
data.entry.forEach(function(entry){
let group_id = entry.id;
entry.changes.forEach(function(change){
console.log('User Change',group_id,change);
});
});
}
function processWorkplaceSecurityEvents(data) {
data.entry.forEach(function(entry){
let group_id = entry.id;
entry.changes.forEach(function(change){
console.log('Workplace change security',group_id,change);
});
});
}
//Start server
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});