-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
339 lines (304 loc) · 12.8 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var TelegramBot = require('node-telegram-bot-api');
var CronJob = require('cron').CronJob;
var request = require('request');
var fs = require('fs-extra');
var config = require('./config');
var token = config.botToken;
var steamStatusUrl = 'https://steamgaug.es/api/v2';
var oldStatus = '';
var statusFile = './status.json';
try {
fs.ensureFileSync(statusFile);
oldStatus = fs.readFileSync(statusFile, {encoding: 'utf8'});
} catch(error) {
oldStatus = '';
}
var redis = require("redis");
var client = redis.createClient();
client.select(config.redisTableNum);
client.on("error", function(err) {
console.log("[redis] Error " + err);
});
var bot = new TelegramBot(token, {
polling: true
});
new CronJob('*/5 * * * *', function() { // every 5 minutes
requestSteamStatusUpdate();
}, null, true, 'America/Los_Angeles');
bot.getMe().then(function(me) {
console.log('[Bot] ' + me.username + ' logged in successfully.');
requestSteamStatusUpdate();
});
bot.on('message', function(msg) {
if (typeof msg.text !== 'undefined' && msg.text) {
if (msg.from.id == config.adminId) {
if (msg.text.indexOf('/announce ') >= 0) {
var annMsg = msg.text.replace('/announce ', '');
broadcast(annMsg);
return;
}
switch (msg.text.toLowerCase().trim()) {
case '/users':
getTotalUsersCount(msg.chat.id);
break;
}
}
if (typeof msg.from.username !== 'undefined') {
console.log('[Chat] @' + msg.from.username + ': ' + msg.text);
} else {
console.log('[Chat] @' + msg.from.first_name + ': ' + msg.text);
}
switch (msg.text.toLowerCase().trim()) {
case '/subscribe':
subscribe(msg);
break;
case '/unsubscribe':
unsubscribe(msg);
break;
case '/start':
bot.sendMessage(msg.chat.id, 'Type /subscribe to begin.');
break;
case '/status':
sendSteamStatuses(msg.chat.id);
break;
case '/ping':
bot.sendMessage(msg.chat.id, 'pong');
break;
}
}
});
function sendSteamStatuses(chatId) {
console.log('User requested all statuses.');
var messages = [];
if (oldStatus !== '') {
var data = JSON.parse(oldStatus);
if (typeof data['ISteamClient'] !== 'undefined') { // make sure everything is okay
Object.keys(data).forEach(function(element) {
if (typeof data[element].online !== 'undefined') {
if (data[element].online == 2) {
messages.push(element + ' is *offline*.');
} else if (data[element].online == 1) {
messages.push(element + ' is online.');
}
} else {
if (element == 'ISteamGameCoordinator') {
Object.keys(data['ISteamGameCoordinator']).forEach(function(element) {
if (data['ISteamGameCoordinator'][element].online == 2) {
messages.push('GameCoordinator for ' + getGameNameFromId(element) + ' is *offline*. ');
} else if (data['ISteamGameCoordinator'][element].online == 1) {
messages.push('GameCoordinator for ' + getGameNameFromId(element) + ' is online.');
}
});
} else if (element == 'IEconItems') {
Object.keys(data['IEconItems']).forEach(function(element) {
if (data['IEconItems'][element].online == 2) {
messages.push('IEconItems for ' + getGameNameFromId(element) + ' is *offline*. ');
} else if (data['IEconItems'][element].online == 1) {
messages.push('IEconItems for ' + getGameNameFromId(element) + ' is online.');
}
});
}
}
});
if (messages.length > 0) {
var fileInfo = fs.statSync(statusFile);
messages.push('\n\nLast checked: ' + fileInfo.mtime);
bot.sendMessage(chatId, messages.join("\n"), {
parse_mode: 'markdown'
}).then(function(result) {
}).catch(function(error) {
console.log(error);
});
}
} else {
console.log('Something wrong with json at [sendSteamStatuses]');
}
} else {
// need to get new data
console.log('TODO: Need to get new data :/');
}
}
function requestSteamStatusUpdate() {
request({
url: steamStatusUrl
}, function(error, response, body) {
if (error) {
console.log(error);
return;
}
fs.writeFile(statusFile, body, function(err) {
if(err) {
return console.log(err);
}
console.log("[status.json] The file was saved!");
compareResults(body);
});
});
}
function compareResults(data) {
console.log('Comparing results');
var messages = [];
if (isValidJson(data)) {
var data = JSON.parse(data);
if (oldStatus === '') {
if (typeof data['ISteamClient'] !== 'undefined') { // make sure everything is okay
Object.keys(data).forEach(function(element) {
if (typeof data[element].online !== 'undefined') {
if (data[element].online == 2) {
messages.push(element + ' is offline. ');
}
} else {
if (element == 'ISteamGameCoordinator') {
Object.keys(data['ISteamGameCoordinator']).forEach(function(element) {
if (data['ISteamGameCoordinator'][element].online == 2) {
messages.push('GameCoordinator for ' + getGameNameFromId(element) + ' is offline. ');
}
});
} else if (element == 'IEconItems') {
Object.keys(data['IEconItems']).forEach(function(element) {
if (data['IEconItems'][element].online == 2) {
messages.push('IEconItems for ' + getGameNameFromId(element) + ' is offline. ');
}
});
}
}
});
} else {
console.log('json fucked up [compareResults]');
}
} else if (oldStatus !== '' && JSON.stringify(data) != oldStatus) {
var oldResults = JSON.parse(oldStatus);
if (typeof data['ISteamClient'] !== 'undefined' && typeof oldResults['ISteamClient'] !== 'undefined') { // make sure everything is okay
Object.keys(data).forEach(function(element) {
if (typeof data[element].online !== 'undefined' && typeof oldResults[element].online !== 'undefined') {
if (oldResults[element].online == 1 && data[element].online == 2) {
messages.push(element + ' is offline. ');
} else if (oldResults[element].online == 2 && data[element].online == 1) {
messages.push(element + ' is back online.');
}
} else {
if (element =='ISteamGameCoordinator') {
Object.keys(data['ISteamGameCoordinator']).forEach(function(element) {
if (oldResults['ISteamGameCoordinator'][element].online == 1 && data['ISteamGameCoordinator'][element].online == 2) {
messages.push('GameCoordinator for ' + getGameNameFromId(element) + ' is offline. ');
} else if (oldResults['ISteamGameCoordinator'][element].online == 2 && data['ISteamGameCoordinator'][element].online == 1) {
messages.push('GameCoordinator for ' + getGameNameFromId(element) + ' is back online.');
}
});
} else if (element == 'IEconItems') {
Object.keys(data['IEconItems']).forEach(function(element) {
if (oldResults['IEconItems'][element].online == 1 && data['IEconItems'][element].online == 2) {
messages.push('IEconItems for ' + getGameNameFromId(element) + ' is offline. ');
} else if (oldResults['IEconItems'][element].online == 2 && data['IEconItems'][element].online == 1) {
messages.push('IEconItems for ' + getGameNameFromId(element) + ' is back online.');
}
});
}
}
});
} else {
console.log('something fucked up as well [compareResults]');
}
}
oldStatus = JSON.stringify(data);
if (messages.length > 0) { // something changed
var offlineMsg = messages.join("\n");
// get list of subscribers
client.hgetall('subscriptions', function(err, result) {
if (err) {
console.log('[redis]: ' + err);
return;
}
if (result !== null) { // if there are subscribers
if (Object.keys(result).length > 0) {
Object.keys(result).forEach(function(element) {
bot.sendMessage(element, offlineMsg).then(function(data) {
}).catch(function(e) {
console.log(e);
if (e.indexOf('403') >= 0) { // user deleted bot
client.hdel("subscriptions", element);
}
});
});
}
}
});
}
} else {
console.log('Unable to parse JSON.');
}
}
function broadcast(message) {
client.hgetall('subscriptions', function(err, result) {
if (err) {
console.log('[redis]: ' + err);
return;
}
if (result !== null) { // if there are subscribers
if (Object.keys(result).length > 0) {
Object.keys(result).forEach(function(element) {
bot.sendMessage(element, message);
});
}
}
});
}
function getTotalUsersCount(chatId) {
client.hgetall('subscriptions', function(err, result) {
if (err) {
console.log('[redis]: ' + err);
return;
}
if (result !== null) { // if there are subscribers
if (Object.keys(result).length > 0) {
bot.sendMessage(chatId, Object.keys(result).length + ' total subscribers.');
}
}
});
}
function subscribe(msg) {
client.hget("subscriptions", msg.chat.id, function(err, result) {
if (err) {
console.log('[Subscribe] Error:' + err + "|" + result);
} else {
client.hset("subscriptions", msg.chat.id, '', function(error, result) {
if (error) {
bot.sendMessage(msg.chat.id, 'Error subscribing. Contact admin: ' + error);
return;
}
bot.sendMessage(msg.chat.id, 'Subscribed to Steam status updates.');
});
}
});
}
function unsubscribe(msg) {
client.hdel("subscriptions", msg.chat.id, function(error, response) {
if (error) {
bot.sendMessage(msg.chat.id, 'Error unsubscribing. Contact admin: ' + error);
return;
}
bot.sendMessage(msg.chat.id, 'Unsubscribed to Steam status updates.');
});
}
function getGameNameFromId(id) {
if (typeof config.games[id] !== 'undefined') {
return config.games[id];
}
return id;
}
function isValidJson (jsonString){
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns 'null', and typeof null === "object",
// so we must check for that, too.
//if (o && typeof o === "object" && o !== null) {
return true;
//}
}
catch (e) {
console.log(e);
}
return false;
}