-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
232 lines (227 loc) · 4.78 KB
/
app.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
const https=require('https');
const EventEmitter = require('events');
const util = require('util');
const querystring =require('querystring');
const urlapi=require('url');
function TelegramInterface(token)
{
EventEmitter.call(this);
this._token = token;
this._lastupdate =0-1;
}
util.inherits(TelegramInterface, EventEmitter);
var pt = TelegramInterface.prototype;
pt.set_last_update=function(new_update)
{
var that=this;
if(new_update<0)
that._lastupdate = -1;
else
that._lastupdate = Math.max(that._lastupdate,new_update);
}
pt.matrify=function(item)
{
var item_row = Math.trunc(Math.sqrt(item.length))
var matrix=[]
var row=[]
for(var i = 0; i < item.length;i++)
{
row.push(item[i]);
if( row.length== item_row)
{
matrix.push(row)
row=[]
}
}
if(row.length > 0)
matrix.push(row)
return matrix;
}
pt.send_choice=function(chat_id,text,alternatives)
{
var that = this;
that.send_message(chat_id,text,JSON.stringify({'inline_keyboard':that.matrify(alternatives.map((x)=>{return {'text':x,'callback_data':x};}))}));
}
pt.send_message=function(chat_id,text,reply_markup)
{
var data = {'chat_id':chat_id,'text':text,'parse_mode':'Markdown'};
if(reply_markup)
data.reply_markup=reply_markup;
else
data.reply_markup = JSON.stringify({'hide_keyboard':true});
this.post('sendMessage',data ,(e,d)=>{
if(e)
{
this.emit('error',e);
}
this.emit('message-sent',chat_id,text);
});
}
pt.get_request_url=function(method_name)
{
return `https://api.telegram.org/bot${this._token}/${method_name}`;
}
pt.is_command=function(text)
{
if(text)
{
if(text !=="")
{
return text[0] ==="/";
}
}
return false;
}
pt.process_message=function(update_id,message)
{
var date= new Date(message.date * 1000);
var chat_id = message.chat.id;
var text = message.text;
if(this.is_command(text))
{
var pieces=text.split(/\s+/);
var command=pieces[0].substring(1);
pieces.splice(0,1);
var args = pieces;
var eventName=`command-${command}`;
if(this.listenerCount(eventName)> 0)
{
this.emit(eventName,chat_id,date,message.from,args);
}
else
{
this.emit('command-unknow',chat_id,date,message.from,command);
}
}
else
{
this.emit('message',chat_id,date,message.from,message.text);
}
}
pt.process_update=function(update)
{
var id = update.update_id;
if(update.message)
{
this.process_message(id,update.message);
}
else if(update.callback_query)
{
var chat_id = update.callback_query.message.chat.id;
var data = update.callback_query.data;
this.send_message(chat_id,"YOU CHOOSE: "+data,null);
}
this.set_last_update(id);
}
pt.process_updates=function(error,data)
{
var that=this;
if(error)
{
this.emit('error',error);
return;
}
if(!data.ok)
{
this.emit('error',data.description);
return;
}
data.result.forEach((e)=>{
that.process_update(e);
});
}
pt.get_updates=function(callback,timeout){
this.get('getUpdates',{'offset':this._lastupdate+1,'timeout':timeout} ,callback);
}
pt.updates_loop=function(timeout){
var that = this;
that.get_updates(function(e,data){
that.process_updates(e,data);
that.updates_loop(timeout);
return;
},timeout);
}
pt.post=function(method,arg,callback)
{
var that=this;
if(!callback)
callback=that.process_updates.bind(this);
var url = this.get_request_url(method);
var parsed = urlapi.parse(url)
var post_options = {
host: parsed.host,
port: '443',
path: parsed.path,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var req = https.request(post_options,(res)=>{
var data="";
res.on('data', (chunk) => {
data = data +chunk;
});
res.on('end', () => {
console.log(data);
e=null;
try
{
data = JSON.parse(data);
if(!data.ok)
console.log("ERROR: "+JSON.stringify(data));
}
catch(ex)
{
e=ex;
}
if(callback)
callback(e,data);
})
});
req.write(JSON.stringify(arg));
console.log(JSON.stringify(arg));
req.on('error', (e) => {
callback(e,null);
});
req.end();
}
pt.get=function(method,arg,callback)
{
var that=this;
if(!callback)
callback=that.process_updates.bind(this);
var url = this.get_request_url(method);
if(arg)
{
url = url+'?'+querystring.stringify(arg);
}
console.log("URL: "+url);
var req = https.request(url,(res)=>{
var data="";
res.on('data', (chunk) => {
data = data +chunk;
});
res.on('end', () => {
e=null;
try
{
data = JSON.parse(data);
}
catch(ex)
{
e=ex;
}
if(callback)
callback(e,data);
})
});
req.on('error', (e) => {
callback(e,null);
});
req.end();
}
module.exports=function(token)
{
return new TelegramInterface(token);
}