forked from suraj-shanbhag/ChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (60 loc) · 2.28 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
'use strict';
const
bodyParser = require('body-parser'),
config = require('config'),
express = require('express'),
data = require('./src/data.js'),
apiWebHook = require('./src/ApiWebHook.js'),
facebookReply = require('./src/FacebookReply.js');
const
fastrackSummaryDetails = require('./src/GetTrainlineScheduleView.js').fastrackSummaryDetails,
apiWebHookHandler = apiWebHook.apiWebHookHandler,
senderAction = facebookReply.senderAction;
const app = express();
const ClientValidationToken = process.env.validationToken || config.validationToken;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const server = app.listen(process.env.PORT || 5000, () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
app.get('/', (req, res) => {
res.send("The Server Is Hot");
});
app.get('/webhook', (req, res) => {
if (req.query['hub.mode'] && req.query['hub.verify_token'] === ClientValidationToken) {
res.status(200).send(req.query['hub.challenge']);
} else {
res.status(403).end();
}
});
app.get('/summary', function (req, res) {
let index = parseInt(req.query.q, 10);
let html_data = "<title>Journey Summary</title><form id='redir' action='https://et2-m-virgintrains.ttlnonprod.com/buy/purchase_ticket_request' method='POST'>" +
"<textarea rows='15' cols='50' name='journey' style='display: none'>" + data.getPostData(fastrackSummaryDetails.summaryList[index]).toString() +
"</textarea>" +
"</form>" +
"<script>document.getElementById('redir').submit()</script>";
res.send(html_data);
});
/* Handling all messenges */
app.post('/webhook', (req, res) => {
function getText(event) {
if (event.postback) {
return event.postback.payload;
}
return event.message.text;
}
if (req.body.object === 'page') {
req.body.entry.forEach((entry) => {
entry.messaging.forEach((event) => {
let sender = event.sender.id;
let text = getText(event);
if (text) {
senderAction(sender,text);
}
});
});
res.status(200).end();
}
});
app.post('/ai', apiWebHookHandler);