-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (96 loc) · 3.65 KB
/
index.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
'use strict';
const logger = require("winston"),
config = require('config');
// Logging levels: error, warn, info, verbose, debug, silly
const LOGGER_LEVEL = process.env.LOGGER_LEVEL || config.get('loggerLevel');
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
"timestamp": true,
"level": LOGGER_LEVEL,
});
logger.info('Initializing app');
if (process.env.NODE_ENV === 'production' && process.env.NEW_RELIC_LICENSE_KEY) {
logger.info('Setting up New Relic.');
require('newrelic');
}
const express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()),
path = require('path');
const moment = require('moment'),
dbUtils = require('./app/db_utils'),
bot = require('./app/bot'),
contestsChecker = require('./app/contests_checker');
moment.locale('ro');
moment.tz.setDefault('UTC');
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => logger.info('Webhook is listening...'));
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
const body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
if (entry.messaging) {
const webhookEvent = entry.messaging[0];
// Get the sender PSID
const senderPSID = webhookEvent.sender.id;
logger.info(`Received message from ${ senderPSID }!`);
logger.verbose(webhookEvent);
// Check if the event is a message or postback and
// pass the event to the appropriate handler function
if (webhookEvent.message) {
bot.handleMessage(senderPSID, webhookEvent.message);
} else if (webhookEvent.postback) {
bot.handlePostback(senderPSID, webhookEvent.postback);
}
} else {
logger.warn('Empty message?');
}
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
const VERIFY_TOKEN = process.env.VERIFY_TOKEN || config.get('validationToken');
// Parse the query params
const mode = req.query['hub.mode'];
const token = req.query['hub.verify_token'];
const challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
logger.info('Webhook verified!');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
app.get('/', (req, res) => {
dbUtils.getSubscribers().then((subscribers) => {
res.render('index', {
subscribersCount: subscribers.length,
});
});
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine','ejs');
app.engine('html', require('ejs').renderFile);
const CONTESTS_FETCH_INTERVAL = process.env.CONTESTS_FETCH_INTERVAL || config.get('contestsFetchInterval');
contestsChecker.checkContestReminders();
setInterval(() => {
contestsChecker.checkContestReminders();
}, CONTESTS_FETCH_INTERVAL);