forked from fbsamples/messenger-platform-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (58 loc) · 1.92 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright 2016-present, FacebookMessageSender, 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.
*
*/
/* jshint node: true, devel: true */
'use strict';
const
bodyParser = require('body-parser'),
express = require('express'),
https = require('https'),
MessageConsumer = require('./src/messageConsumer/MessageConsumer.js'),
BookService = require('./src/services/BookService.js'),
FacebookMessageSender = require('./src/messageSender/FacebookMessageSender.js');
/*
* Be sure to setup your config values before running this code. You can
* set them using environment variables or modifying the config file in /config.
*
*/
var facebook = new FacebookMessageSender();
var messageConsumer = new MessageConsumer(facebook);
var app = express();
/**
* Rest API that manage books
* @type {BookService}
*/
var bookService = new BookService(app);
app.set('port', process.env.PORT || 5000);
app.use(bodyParser.json({verify: facebook.verifyRequestSignature}));
app.use('/static', express.static('static'));
/*
* Use your own validation token. Check that the token used in the Webhook
* setup is the same token used here.
*
*/
app.get('/webhook', function(req, res) {
facebook.verifyToken(req, res);
});
/*
* 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/implementation#subscribe_app_pages
*
*/
app.post('/webhook', function (req, res) {
messageConsumer.consumeRequest(req,res);
});
// Start server
// Webhooks must be available via SSL with a certificate signed by a valid
// certificate authority.
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
module.exports = app;