-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (103 loc) · 3.61 KB
/
server.js
File metadata and controls
120 lines (103 loc) · 3.61 KB
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
// Core modules
var http = require('http');
// npm modules
var Bot = require('node-telegram-bot-api');
var config = require('config');
var Q = require('q');
var util = require('util');
// Custom modules
var Chat = require('./lib/chat');
var logger = require('./lib/logger');
(function () {
var telegramConfig = config.get('Server.telegram');
var bot = startBot();
var chat = new Chat(bot);
var steps = {
fixtures: [chat.sendFixtures],
results: [chat.sendResults],
table: [chat.sendTable],
matchstats: [chat.requestMatchName, chat.sendMatchStats],
teamstats: [chat.requestTeamName, chat.sendTeamStats]
};
var processMessage = function (message) {
var command = chat.getCommand(message);
if (!command) {
return;
}
logger.info(message);
// Append the current message if not another command
if (message.text && message.text.lastIndexOf('/', 0) !== 0) {
command.args.push(message.text);
chat.setCommand(message, command);
}
var commandSteps = steps[command.name];
// Execute the appropriate step
if (commandSteps && commandSteps.length > command.args.length) {
// call method is needed to execute in correct context
commandSteps[command.args.length].call(chat, message, command.args);
}
};
function startBot () {
return new Bot({
token: telegramConfig.token
})
.on('message', function (message) {
processMessage(message);
})
.on('start', function (message) {
bot.start();
})
.on('error', function (message) {
logger.warn('ERROR: ' + message);
})
.on('fixtures', function (message, args) {
chat.initCommand(message, args, 'fixtures');
})
.on('matchstats', function (message, args) {
chat.initCommand(message, args, 'matchstats');
})
.on('results', function (message, args) {
chat.initCommand(message, args, 'results');
})
.on('table', function (message, args) {
chat.initCommand(message, args, 'table');
})
.on('teamstats', function (message, args) {
chat.initCommand(message, args, 'teamstats');
})
.enableAnalytics(config.get('Server.botanio').token)
.start();
}
function throwError () { // eslint-disable-line no-unused-vars
var deferred = Q.defer();
deferred.reject(new Error('Fake error')); // rejects the promise with `er` as the reason
return deferred.promise; // the promise is returned
}
function queryBot (bot) {
logger.debug(util.inspect(bot, { showHidden: true, depth: 1 }));
var promise = bot.getMe();// throwError();
var timeout = 60000;
logger.debug(util.inspect(promise, { showHidden: false, depth: 1 }));
promise.then(function (data) {
logger.debug(util.inspect(promise, { showHidden: false, depth: 1 }));
setTimeout(function () { queryBot(bot); }, timeout); // queue for next ping in the next predefined interval
}, function (err) {
logger.debug(util.inspect(promise, { showHidden: false, depth: 1 }));
logger.warn(err);
bot = startBot();
setTimeout(function () { queryBot(bot); }, timeout); // queue for next ping in the next predefined interval
});
}
queryBot(bot);
})();
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 8090;
if (ipaddress !== '127.0.0.1') {
console.log(String.format('Starting server on {0}:{1}', ipaddress, port));
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Nothing to see here...\r\n');
}).listen(port, ipaddress);
}