-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
204 lines (181 loc) · 6.13 KB
/
server.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
/**
* Server to display rewards and listen for incoming messages
* from Facebook Messenger.
*/
(function () {
'use strict';
const express = require('express');
const helmet = require('helmet');
const FB = require('./connectors/facebook');
const database = require('./connectors/database');
const Bot = require('./bot');
/**
* Setup the express server.
*/
const app = express();
app.use(helmet());
app.set('views', './views');
app.set('view engine', 'pug');
app.use(require('body-parser').json());
/**
* Setup the development environment.
* Load the local .env file, that sets process.env.
* and process SCSS on page load.
*/
if (process.env.NODE_ENV !== 'production') {
require('dotenv').load();
app.get('/sass', (req, res) => {
require('./routes/process-sass').srcToDist('reward-style', 'reward-style');
require('./routes/process-sass').srcToDist('main', 'main');
const r = (new Date()).toUTCString() + ' | Processed sass files.';
console.log(r);
res.send(`<h1>${r}<h1>`);
});
}
/**
* Setup the server instance and start listening on the port.
*/
const serverInstance = app.listen(process.env.PORT, () => {
console.log('> Running on port', process.env.PORT);
});
/**
* Setup the static folder for the rewards website,
* and route the email and rewards routes.
*/
app.use(express.static('./public'));
app.use('/rewards', require('./routes/rewards'));
app.use('/email', require('./routes/email'));
/**
* Manually send reminders at specific times of the day
* useful for debugging.
*/
app.get(['/reminders/:timeOfDay', '/reminders'], (req, res) => {
if (!req.query.secret || req.query.secret !== process.env.API_SECRET) {
res.send('Invalid secret.');
return;
}
if (req.params.timeOfDay) {
console.log('Time of day: ' + req.params.timeOfDay);
require('./routes/reminders').sendReminders(req.params.timeOfDay, success => {
console.log(success);
});
} else {
require('./routes/reminders').sendReminders(null, success => {
console.log(success);
});
}
});
/**
* Send all participants the final questionnaire.
*/
app.get('/finalsurvey', (req, res) => {
if (!req.query.secret || req.query.secret !== process.env.API_SECRET) {
res.send('Invalid secret.');
return;
}
require('./bin/full-survey-message').startFullSurvey(() => {
console.log('Sent final survey messages');
});
});
/**
* Deprecated: send stats to people (used to be a post request).
*/
app.get('/stats', (req, res) => {
if (!req.query.secret || req.query.secret !== process.env.API_SECRET) {
res.send('Invalid secret.');
return;
}
res.send('deprecated');
// require('./routes/stats').sendStats(success => {
// res.send(success);
// });
});
/**
* Debug page to display version information.
*/
app.get('/', (req, res) => {
res.render('index', {
version: process.env.npm_package_version,
name: process.env.npm_package_name_friendly
});
});
/**
* Facebook Messenger webhook, to receive messages via Messenger.
*/
app.post('/webhooks', (req, res) => {
console.log('> Receiving Message');
// Extract message content
const entry = FB.getMessageEntry(req.body);
console.log(entry);
// If the message is valid
if (entry && entry.message) {
database.getGlobals(globals => {
console.log(globals);
if (!globals.studyActive) {
console.log('Study is inactive, replying stock message.');
FB.newMessage(entry.sender.id, { text: 'Sorry the study is over. For further questions email [email protected]' }, (msg, data) => {
if (data.error) {
console.log('Error sending stock study inactive message')
console.error(data);
} else {
console.log(msg, data);
}
});
} else {
if (entry.message && entry.message.quick_reply) {
console.log('QR> ' + entry.message.quick_reply.payload);
} else {
console.log('MSG> ' + entry.message.text);
}
// Process the message and decide on the response
Bot.read(entry.sender.id, entry.message, (senderFbid, reply, anotherReply, thirdReply, fourthReply, fifthReply) => {
console.log('-- from bot to user vv --');
console.log(JSON.stringify(reply));
FB.send(senderFbid, reply, 500, () => {
if (typeof anotherReply !== 'undefined' && anotherReply !== null) {
FB.send(senderFbid, anotherReply, 500, () => {
if (typeof thirdReply !== 'undefined' && thirdReply !== null) {
FB.send(senderFbid, thirdReply, 500, () => {
if (typeof fourthReply !== 'undefined' && fourthReply !== null) {
FB.send(senderFbid, thirdReply, 500, () => {
if (typeof fifthReply !== 'undefined' && fifthReply !== null) {
FB.send(senderFbid, fifthReply, 500, () => {
console.log('Sent all messages to user');
});
}
});
}
});
}
});
}
});
});
}
});
} else {
console.log('Invalid entry/message or attachment found.');
console.log(JSON.stringify(entry));
console.log(JSON.stringify(req.body));
}
res.sendStatus(200);
});
/**
* Facebook verification middleware.
*/
app.get('/webhooks', (req, res) => {
if (req.query['hub.verify_token'] === process.env.FB_VERIFY_TOKEN) {
res.status(200).send(req.query['hub.challenge']);
} else {
console.log('Error wrong fb verify token, make sure validation tokens match.');
res.status(403).send('> Error, wrong fb verify token');
}
});
module.exports = {
shutdown() {
console.log('Server shutting down');
serverInstance.close();
}
};
})();