-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram-retriever.js
69 lines (54 loc) · 1.63 KB
/
telegram-retriever.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
const request = require('request');
const config = require('./config/config.js');
const utils = require("./utils");
const logPrefix = "TGR-RTR";
const mainIntervalDuration = 2000;
var mainInterval;
var lastUpdateId = 0;
var timedOutError = false;
process.on('message',function(msg){
this.start = function(){
utils.makeLog("Starting...", logPrefix);
mainInterval = setInterval(function() { retrieveUpdates(); }, mainIntervalDuration);
}
this.stop = function(){
utils.makeLog("Stopping...");
clearInterval(mainInterval, logPrefix);
}
this._init = function(){
utils.makeLog("_init", logPrefix);
switch(msg){
case 'start':
this.start();
break;
case 'stop':
this.stop();
break;
}
}.bind(this)()
});
function retrieveUpdates(){
request('https://api.telegram.org/bot' + config.telegramToken + '/getUpdates?offset=' + (lastUpdateId+1), { 'timeout': (mainIntervalDuration - (mainIntervalDuration/5)) }, function (error, response, body) {
if (!error && response.statusCode === 200) {
timedOutError = false;
var updates = JSON.parse(body);
for(var i = 0; i < updates.result.length; i++){
var elm = updates.result[i];
if(elm.update_id > lastUpdateId){
lastUpdateId = elm.update_id;
}
try{
process.send(elm);
}catch(err){
utils.makeLog("Disconnecting the process..." + err, logPrefix);
process.disconnect();
}
}
}else{
if(!timedOutError) {
timedOutError = true;
utils.makeLog("Unable to retrieve telegram updates.\n" + error + "\nResponse: " + response + "\nBody: " + body, logPrefix);
}
}
});
}