-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
126 lines (110 loc) · 3.52 KB
/
server.ts
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
import bodyParser from 'body-parser';
import crypto from 'crypto';
import express from 'express';
import { Config } from './config';
import { MessageHandler } from './message-handler';
var app = express();
app.set('view engine', 'ejs');
app.use(
bodyParser.json({
verify: verifyRequestSignature,
})
);
app.use(express.static('public'));
//tokens for facebook
const VALIDATION_TOKEN = Config.getValidationToken();
const APP_SECRET = Config.getAppSecret();
console.log('validation token is ' + VALIDATION_TOKEN);
//webhook endpoints
app.get('/flights', function (req, res) {
if (
req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === VALIDATION_TOKEN
) {
console.log('Validating webhook');
res.status(200).send(req.query['hub.challenge']);
} else {
console.error('Failed validation. Make sure the validation tokens match.');
res.sendStatus(403);
}
});
/*
* All callbacks for Messenger are POST-ed. They will be sent to the same
* webhook. Be sure to subscribe your app to your page to receive callbacks
* for your page.
* https://developers.facebook.com/docs/messenger-platform/product-overview/setup#subscribe_app
*
*/
app.post('/flights', function (req, res) {
var data = req.body;
console.log('a request has come!');
console.log('request ' + req);
console.log('data ' + data);
// Make sure this is a page subscription
if (data.object == 'page') {
// Iterate over each entry
// There may be multiple if batched
data.entry.forEach((pageEntry: any) => {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
// Iterate over each messaging event
pageEntry.messaging.forEach((messagingEvent: any) => {
console.log(
'Webhook received unknown messagingEvent: ',
messagingEvent
);
if (messagingEvent.message) {
MessageHandler.Instance.receivedMessage(messagingEvent);
}
if (messagingEvent.optin) {
MessageHandler.Instance.receivedAuthentication(messagingEvent);
}
});
});
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know you've
// successfully received the callback. Otherwise, the request will time out.
res.sendStatus(200);
}
});
function verifyRequestSignature(req: any, res: any, buf: any) {
var signature = req.headers['x-hub-signature'];
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 method = elements[0];
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.get('/policy', function (req, res) {
res.render('policy');
res.status(200).json({
success: true,
});
});
var server = app.listen(process.env.PORT || 8080, function () {
if (!server) {
console.log('Failed to start application...');
return;
}
let addressStr = '';
if (server.address() && (typeof server.address()) == 'string') {
const address = server.address() as string;
addressStr = `http:\\\\${address}`;
} else if (server.address()) {
const address = server.address() as any;
addressStr = `http:\\\\${address.address}:${address.port}`;
}
console.log('App is listening at http://%s', addressStr);
});