-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (106 loc) · 3.2 KB
/
index.js
File metadata and controls
137 lines (106 loc) · 3.2 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const request = require('request-promise');
const extend = require('extend');
const chalk = require('chalk');
const Clui = require('clui');
const Spinner = Clui.Spinner;
const EventEmitter = require('events').EventEmitter || require('events');
const emitter = new EventEmitter();
const Promise = require('bluebird');
const config = {
updateUrl: 'https://api.telegram.org/bot<bot-token>/getUpdates',
hookUrl: '',
offset: 0,
settings: {
interval: 1000,
timeout: 0
}
}
const checkUpdates = (processed) => {
return ( new Promise((resolve,reject) => {
let formData = {};
if (config.settings.timeout !== undefined) formData.timeout = config.settings.timeout;
if (config.offset !== undefined) formData.offset = config.offset;
if (config.limit !== undefined) formData.limit = config.limit;
request({
method: 'GET',
json: true,
formData: formData,
uri: config.updateUrl
})
.then((data) => {
resolve(data.result);
})
.catch((error) => {
reject(error);
});
}));
}
module.exports = (botToken, hookUrl, options) => {
try {
if(!botToken){
throw 'Please add your bot token.';
}
if(!hookUrl){
throw 'Please add your hook url.';
}
config.updateUrl = 'https://api.telegram.org/bot' + botToken + '/getUpdates';
config.hookUrl = hookUrl;
config.settings = extend( true, config.settings, options);
let status = new Spinner( chalk.blue('Listening...'), ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);
Promise.resolve([])
.then( function loop(processed) {
status.start();
return checkUpdates(processed)
.then((results) => {
if(results.length > 0){
status.stop();
status.message( chalk.blue('Forwarding ' + results.length + ' message(s)') );
status.start();
let forwards = Promise.each(results, item => {
status.stop();
emitter.emit('data', item);
status.start();
// console.log(item.update_id);
if(item.update_id >= config.offset){
config.offset = item.update_id + 1;
}
return request({
method: 'POST',
url: config.hookUrl,
json: true,
body: item
})
.then((data) => {
status.stop();
emitter.emit('forwarded', data);
status.message( chalk.blue('Listening...') );
status.start();
return true;
})
.catch((error) => {
status.stop();
emitter.emit('forward.error', error);
status.message( chalk.blue('Listening...') );
status.start();
return true;
});
});
return forwards;
} else {
return [];
}
})
.catch((error) => {
status.stop();
emitter.emit('error', error);
status.start();
return processed;
})
.delay(config.settings.interval)
.then(loop);
});
} catch (err) {
console.error(err);
}
return emitter;
};