forked from mrgrshift/Server-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram.js
179 lines (150 loc) · 4.42 KB
/
telegram.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
var https = require("https");
var fs = require("fs");
var config = require("./config");
var apiKey = config.telegram.apiKey;
var chat_id = config.telegram.chat_id;
var timeout = config.telegram.timeout;
var timer_j = 0, timer_m = 0, timer_t = 0;
var mainnet_forging = false;
var testnet_forging = false;
var mainnet_badMessage = false;
var testnet_badMessage = false;
var json_badMessage = false;
var mainnet_next_turn = "null";
var testnet_next_turn = "null";
function Monitor() {
var json_data = fs.readFileSync('data.json', 'utf8');
if(IsJsonString(json_data)) {
if(json_badMessage) {
let message = "%E2%9C%85 JSON file is read. *All is okay.*";
sendMessage(message);
json_badMessage = false;
console.log("Telegram: JSON file is read. All is okay.");
}
timer_j = 0;
json_data = JSON.parse(json_data);
var i=1;
for (var ss in json_data.servers) {
isForging(i, json_data.servers[ss].forging,json_data.servers[ss].testnet);
i++;
}
BadMessage(testnet_forging, mainnet_forging);
GoodMessage();
console.log(`Mainnet_forging: ${mainnet_forging} \nTestnet_forging: ${testnet_forging}`);
if(testnet_forging) {
testnet_next_turn = nextTurnTime(json_data.testnet.nextturn);
}
if(mainnet_forging) {
mainnet_next_turn = nextTurnTime(json_data.mainnet.nextturn);
}
mainnet_forging = false;
testnet_forging = false;
//console.log(`timeout: ${timeout}`);
//console.log(`timer_j: ${timer_j} timer_m: ${timer_m} timer_t: ${timer_t}`);
} else {
if(!json_badMessage) {
if(timer_j >= 2 && timer_j >= timeout){
let message = "%E2%9D%8C *Error!* Can't read JSON file!";
sendMessage(message);
json_badMessage = true;
timer_j = 0;
}
timer_j++;
}
console.log("Telegram: Can't read JSON file!");
}
}
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function isForging(server, forging, testnet) {
if(forging == true) {
if(testnet) {
testnet_forging = true;
timer_t = 0;
} else {
mainnet_forging = true;
timer_m = 0;
}
}
}
function BadMessage(testnet_forging, mainnet_forging) {
if(!testnet_badMessage) {
if(!testnet_forging) {
if(timer_t >= 2 && timer_t >= timeout){
let message = '%E2%80%BC *Testnet* are *not* forging!' +
`\n%F0%9F%95%90 Next turn in *${testnet_next_turn}*`;
sendMessage(message);
testnet_badMessage = true;
console.log('\nTelegram: Testnet are not forging!');
}
timer_t++;
}
}
if(!mainnet_badMessage) {
if(!mainnet_forging) {
if(timer_m >= 2 && timer_m >= timeout){
let message = '%E2%80%BC *Mainnet* are *not* forging!' +
`\n%F0%9F%95%90 Next turn in *${mainnet_next_turn}*`;
sendMessage(message);
mainnet_badMessage = true;
console.log('\nTelegram: Mainnet are not forging!');
}
timer_m++;
}
}
}
function GoodMessage() {
if(testnet_badMessage) {
if(testnet_forging) {
let message = '%E2%9C%85 *Testnet* are forging now! *All is okay.* %F0%9F%9A%80';
sendMessage(message);
testnet_badMessage = false;
console.log('Telegram: Testnet are forging now! All is okay.');
}
}
if(mainnet_badMessage) {
if(mainnet_forging) {
let message = '%E2%9C%85 *Mainnet* are forging now! *All is okay.* %F0%9F%9A%80';
sendMessage(message);
mainnet_badMessage = false;
console.log('Telegram: Mainnet are forging now! All is okay.');
}
}
}
function sendMessage(msg) {
var url = `https://api.telegram.org/bot${apiKey}/sendMessage?chat_id=${chat_id}&parse_mode=Markdown&text=${msg}`;
https.get(url, (res) => {
/*
let json = '';
res.on('data', (d) => {
json += d;
});
res.on('end', () => console.log(json));
*/
}).on('error', (e) => { console.error(e); });
}
function nextTurnTime(nextturn){
if(nextturn !== null) {
timeg = nextturn * 27;
time = (timeg/60);
minutes = Math.floor(timeg / 60);
seconds = Math.round((time - minutes) * 60);
var v_nextturn= '';
if(minutes == 0){
v_nextturn = timeg +" sec";
} else{
v_nextturn = minutes + " min "+ seconds + " sec";
}
return v_nextturn;
} else {
return v_nextturn = 'SOON';
}
}
exports.Monitor = Monitor;
exports.sendMessage = sendMessage;