-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
341 lines (276 loc) · 9.63 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
340
341
'use strict';
const fetch = require('node-fetch');
const FormData = require('form-data');
class TelegramBotAPI {
constructor(token, options) {
if (!token) {
throw new Error('"token" must be specified');
}
this.token = token;
this.options = Object.assign({ endpoint: 'https://api.telegram.org' }, options);
}
endpoint(method) {
return `${this.options.endpoint}/bot${this.token}/${method}`;
}
request(method, options) {
return Promise.resolve(options)
.then(this._prepareRequestBody)
.then(opts => fetch(this.endpoint(method), opts))
.then(this._checkResponseType)
.then(response => response.json())
.then(this._checkResponseBody);
}
_required(parameters, required) {
if (!parameters) {
throw new Error('\'parameters\' object is required.');
}
required.forEach(prop => {
if (!parameters[prop]) {
throw new Error(`'${prop}' parameter is required.`);
}
});
}
_prepareRequestBody(options) {
const opts = Object.assign({ method: 'POST', headers: {} }, options);
if (opts.body && opts.body.reply_markup && typeof opts.body.reply_markup !== 'string') {
opts.body.reply_markup = JSON.stringify(opts.body.reply_markup);
}
if (opts.formData && !(opts.body instanceof FormData)) {
const formData = new FormData();
Object.keys(opts.body).forEach(field => {
formData.append(field, opts.body[field]);
});
delete opts.formData;
opts.body = formData;
Object.assign(opts.headers, formData.getHeaders());
} else if (opts.body && typeof opts.body !== 'string') {
opts.body = JSON.stringify(opts.body);
opts.headers['content-type'] = 'application/json';
}
return opts;
}
_checkResponseType(response) {
const contentType = response.headers.get('content-type');
if (contentType !== 'application/json') {
throw new Error(`Telegram API wrong type of response: '${contentType}'`);
}
return response;
}
_checkResponseBody(body) {
if (body.ok !== true) {
if (body.description != null && body.error_code != null) {
const err = new Error(`Telegram API: '${body.description}'`);
err.code = body.error_code;
throw err;
} else {
throw new Error(`Telegram API error: '${JSON.stringify(body)}'`);
}
}
return body;
}
getMe(options) {
const opts = Object.assign({ method: 'GET' }, options);
return this.request('getMe', opts);
}
sendMessage(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'text']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('sendMessage', opts));
}
forwardMessage(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'from_chat_id', 'message_id']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('forwardMessage', opts));
}
sendPhoto(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'photo']);
const opts = Object.assign({
formData: (typeof parameters.photo !== 'string'),
}, options, { body: parameters });
resolve(opts);
})
.then(opts => this.request('sendPhoto', opts));
}
sendAudio(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'audio']);
const opts = Object.assign({
formData: (typeof parameters.audio !== 'string'),
}, options, { body: parameters });
resolve(opts);
})
.then(opts => this.request('sendAudio', opts));
}
sendDocument(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'document']);
const opts = Object.assign({
formData: (typeof parameters.document !== 'string'),
}, options, { body: parameters });
resolve(opts);
})
.then(opts => this.request('sendDocument', opts));
}
sendSticker(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'sticker']);
const opts = Object.assign({
formData: (typeof parameters.sticker !== 'string'),
}, options, { body: parameters });
resolve(opts);
})
.then(opts => this.request('sendSticker', opts));
}
sendVideo(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'video']);
const opts = Object.assign({
formData: (typeof parameters.video !== 'string'),
}, options, { body: parameters });
resolve(opts);
})
.then(opts => this.request('sendVideo', opts));
}
sendVoice(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'voice']);
const opts = Object.assign({
formData: (typeof parameters.vioce !== 'string'),
}, options, { body: parameters });
resolve(opts);
})
.then(opts => this.request('sendVoice', opts));
}
sendLocation(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'latitude', 'longitude']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('sendLocation', opts));
}
sendVenue(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'latitude', 'longitude', 'title', 'address']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('sendVenue', opts));
}
sendContact(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'phone_number', 'first_name']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('sendContact', opts));
}
sendChatAction(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'action']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('sendChatAction', opts));
}
getUserProfilePhotos(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['user_id']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('getUserProfilePhotos', opts));
}
getFile(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['file_id']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('getFile', opts))
.then(response => {
const url = `${this.options.endpoint}/file/bot${this.token}/${response.result.file_path}`;
response.result.file_url = url;
return response;
});
}
kickChatMember(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'user_id']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('kickChatMember', opts));
}
unbanChatMember(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['chat_id', 'user_id']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('unbanChatMember', opts));
}
getUpdates(parameters, options) {
const opts = Object.assign({}, options, { body: parameters });
return this.request('getUpdates', opts);
}
setWebhook(parameters, options) {
return new Promise((resolve) => {
const opts = Object.assign({}, options, { body: parameters });
if (parameters && parameters.certificate) {
opts.formData = true;
}
resolve(opts);
})
.then(opts => this.request('setWebhook', opts));
}
/* TODO tests for callback queries */
answerCallbackQuery(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['callback_query_id']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('answerCallbackQuery', opts));
}
/* TODO tests for inline queries */
answerInlineQuery(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['inline_query_id', 'results']);
if (typeof parameters.results !== 'string') {
parameters.results = JSON.stringify(parameters.results);
}
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('answerInlineQuery', opts));
}
/**
* Updating messages
* https://core.telegram.org/bots/api#updating-messages
*/
/**
* https://core.telegram.org/bots/api#editmessagetext
*/
editMessageText(parameters, options) {
return new Promise((resolve) => {
this._required(parameters, ['text']);
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('editMessageText', opts));
}
/**
* https://core.telegram.org/bots/api#editmessagecaption
*/
editMessageCaption(parameters, options) {
return new Promise((resolve) => {
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('editMessageCaption', opts));
}
/**
* https://core.telegram.org/bots/api#editmessagereplymarkup
*/
editMessageReplyMarkup(parameters, options) {
return new Promise((resolve) => {
resolve(Object.assign({}, options, { body: parameters }));
})
.then(opts => this.request('editMessageReplyMarkup', opts));
}
}
module.exports = TelegramBotAPI;