UNPKG

41.2 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Context = void 0;
7const debug_1 = __importDefault(require("debug"));
8const debug = (0, debug_1.default)('telegraf:context');
9class Context {
10 constructor(update, telegram, botInfo) {
11 this.update = update;
12 this.telegram = telegram;
13 this.botInfo = botInfo;
14 // eslint-disable-next-line @typescript-eslint/no-explicit-any
15 this.state = {};
16 }
17 get updateType() {
18 for (const key in this.update) {
19 if (typeof this.update[key] === 'object')
20 return key;
21 }
22 throw new Error(`Cannot determine \`updateType\` of ${JSON.stringify(this.update)}`);
23 }
24 get me() {
25 var _a;
26 return (_a = this.botInfo) === null || _a === void 0 ? void 0 : _a.username;
27 }
28 /**
29 * @deprecated Use ctx.telegram instead
30 */
31 get tg() {
32 return this.telegram;
33 }
34 get message() {
35 return this.update.message;
36 }
37 get editedMessage() {
38 return this.update.edited_message;
39 }
40 get inlineQuery() {
41 return this.update.inline_query;
42 }
43 get shippingQuery() {
44 return this.update.shipping_query;
45 }
46 get preCheckoutQuery() {
47 return this.update.pre_checkout_query;
48 }
49 get chosenInlineResult() {
50 return this.update.chosen_inline_result;
51 }
52 get channelPost() {
53 return this.update.channel_post;
54 }
55 get editedChannelPost() {
56 return this.update.edited_channel_post;
57 }
58 get callbackQuery() {
59 return this.update.callback_query;
60 }
61 get poll() {
62 return this.update.poll;
63 }
64 get pollAnswer() {
65 return this.update.poll_answer;
66 }
67 get myChatMember() {
68 return this.update.my_chat_member;
69 }
70 get chatMember() {
71 return this.update.chat_member;
72 }
73 get chatJoinRequest() {
74 return this.update.chat_join_request;
75 }
76 get chat() {
77 var _a, _b, _c, _d;
78 return (_d = ((_c = (_b = (_a = this.chatMember) !== null && _a !== void 0 ? _a : this.myChatMember) !== null && _b !== void 0 ? _b : this.chatJoinRequest) !== null && _c !== void 0 ? _c : getMessageFromAnySource(this))) === null || _d === void 0 ? void 0 : _d.chat;
79 }
80 get senderChat() {
81 var _a;
82 return (_a = getMessageFromAnySource(this)) === null || _a === void 0 ? void 0 : _a.sender_chat;
83 }
84 get from() {
85 var _a, _b, _c, _d, _e, _f, _g, _h, _j;
86 return (_j = ((_h = (_g = (_f = (_e = (_d = (_c = (_b = (_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineQuery) !== null && _b !== void 0 ? _b : this.shippingQuery) !== null && _c !== void 0 ? _c : this.preCheckoutQuery) !== null && _d !== void 0 ? _d : this.chosenInlineResult) !== null && _e !== void 0 ? _e : this.chatMember) !== null && _f !== void 0 ? _f : this.myChatMember) !== null && _g !== void 0 ? _g : this.chatJoinRequest) !== null && _h !== void 0 ? _h : getMessageFromAnySource(this))) === null || _j === void 0 ? void 0 : _j.from;
87 }
88 get inlineMessageId() {
89 var _a, _b;
90 return (_b = ((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.chosenInlineResult)) === null || _b === void 0 ? void 0 : _b.inline_message_id;
91 }
92 get passportData() {
93 var _a;
94 if (this.message == null)
95 return undefined;
96 if (!('passport_data' in this.message))
97 return undefined;
98 return (_a = this.message) === null || _a === void 0 ? void 0 : _a.passport_data;
99 }
100 get webAppData() {
101 if (!('message' in this.update &&
102 this.update.message &&
103 'web_app_data' in this.update.message))
104 return undefined;
105 const { data, button_text } = this.update.message.web_app_data;
106 return {
107 data: {
108 json() {
109 return JSON.parse(data);
110 },
111 text() {
112 return data;
113 },
114 },
115 button_text,
116 };
117 }
118 /**
119 * @deprecated use {@link Telegram.webhookReply}
120 */
121 get webhookReply() {
122 return this.telegram.webhookReply;
123 }
124 set webhookReply(enable) {
125 this.telegram.webhookReply = enable;
126 }
127 /**
128 * @internal
129 */
130 assert(value, method) {
131 if (value === undefined) {
132 throw new TypeError(`Telegraf: "${method}" isn't available for "${this.updateType}"`);
133 }
134 }
135 has(filters) {
136 if (!Array.isArray(filters))
137 filters = [filters];
138 for (const filter of filters)
139 if (
140 // TODO: this should change to === 'function' once TS bug is fixed
141 // https://github.com/microsoft/TypeScript/pull/51502
142 typeof filter !== 'string'
143 ? // filter is a type guard
144 filter(this.update)
145 : // check if filter is the update type
146 filter in this.update)
147 return true;
148 return false;
149 }
150 /**
151 * @see https://core.telegram.org/bots/api#answerinlinequery
152 */
153 answerInlineQuery(...args) {
154 this.assert(this.inlineQuery, 'answerInlineQuery');
155 return this.telegram.answerInlineQuery(this.inlineQuery.id, ...args);
156 }
157 /**
158 * @see https://core.telegram.org/bots/api#answercallbackquery
159 */
160 answerCbQuery(...args) {
161 this.assert(this.callbackQuery, 'answerCbQuery');
162 return this.telegram.answerCbQuery(this.callbackQuery.id, ...args);
163 }
164 /**
165 * @see https://core.telegram.org/bots/api#answercallbackquery
166 */
167 answerGameQuery(...args) {
168 this.assert(this.callbackQuery, 'answerGameQuery');
169 return this.telegram.answerGameQuery(this.callbackQuery.id, ...args);
170 }
171 /**
172 * @see https://core.telegram.org/bots/api#answershippingquery
173 */
174 answerShippingQuery(...args) {
175 this.assert(this.shippingQuery, 'answerShippingQuery');
176 return this.telegram.answerShippingQuery(this.shippingQuery.id, ...args);
177 }
178 /**
179 * @see https://core.telegram.org/bots/api#answerprecheckoutquery
180 */
181 answerPreCheckoutQuery(...args) {
182 this.assert(this.preCheckoutQuery, 'answerPreCheckoutQuery');
183 return this.telegram.answerPreCheckoutQuery(this.preCheckoutQuery.id, ...args);
184 }
185 /**
186 * @see https://core.telegram.org/bots/api#editmessagetext
187 */
188 editMessageText(text, extra) {
189 var _a, _b, _c, _d;
190 this.assert((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineMessageId, 'editMessageText');
191 return this.telegram.editMessageText((_b = this.chat) === null || _b === void 0 ? void 0 : _b.id, (_d = (_c = this.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.message_id, this.inlineMessageId, text, extra);
192 }
193 /**
194 * @see https://core.telegram.org/bots/api#editmessagecaption
195 */
196 editMessageCaption(caption, extra) {
197 var _a, _b, _c, _d;
198 this.assert((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineMessageId, 'editMessageCaption');
199 return this.telegram.editMessageCaption((_b = this.chat) === null || _b === void 0 ? void 0 : _b.id, (_d = (_c = this.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.message_id, this.inlineMessageId, caption, extra);
200 }
201 /**
202 * @see https://core.telegram.org/bots/api#editmessagemedia
203 */
204 editMessageMedia(media, extra) {
205 var _a, _b, _c, _d;
206 this.assert((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineMessageId, 'editMessageMedia');
207 return this.telegram.editMessageMedia((_b = this.chat) === null || _b === void 0 ? void 0 : _b.id, (_d = (_c = this.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.message_id, this.inlineMessageId, media, extra);
208 }
209 /**
210 * @see https://core.telegram.org/bots/api#editmessagereplymarkup
211 */
212 editMessageReplyMarkup(markup) {
213 var _a, _b, _c, _d;
214 this.assert((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineMessageId, 'editMessageReplyMarkup');
215 return this.telegram.editMessageReplyMarkup((_b = this.chat) === null || _b === void 0 ? void 0 : _b.id, (_d = (_c = this.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.message_id, this.inlineMessageId, markup);
216 }
217 /**
218 * @see https://core.telegram.org/bots/api#editmessagelivelocation
219 */
220 editMessageLiveLocation(latitude, longitude, extra) {
221 var _a, _b, _c, _d;
222 this.assert((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineMessageId, 'editMessageLiveLocation');
223 return this.telegram.editMessageLiveLocation((_b = this.chat) === null || _b === void 0 ? void 0 : _b.id, (_d = (_c = this.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.message_id, this.inlineMessageId, latitude, longitude, extra);
224 }
225 /**
226 * @see https://core.telegram.org/bots/api#stopmessagelivelocation
227 */
228 stopMessageLiveLocation(markup) {
229 var _a, _b, _c, _d;
230 this.assert((_a = this.callbackQuery) !== null && _a !== void 0 ? _a : this.inlineMessageId, 'stopMessageLiveLocation');
231 return this.telegram.stopMessageLiveLocation((_b = this.chat) === null || _b === void 0 ? void 0 : _b.id, (_d = (_c = this.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.message_id, this.inlineMessageId, markup);
232 }
233 /**
234 * @see https://core.telegram.org/bots/api#sendmessage
235 */
236 sendMessage(text, extra) {
237 this.assert(this.chat, 'sendMessage');
238 return this.telegram.sendMessage(this.chat.id, text, {
239 message_thread_id: getThreadId(this),
240 ...extra,
241 });
242 }
243 /**
244 * @see https://core.telegram.org/bots/api#sendmessage
245 */
246 reply(...args) {
247 return this.sendMessage(...args);
248 }
249 /**
250 * @see https://core.telegram.org/bots/api#getchat
251 */
252 getChat(...args) {
253 this.assert(this.chat, 'getChat');
254 return this.telegram.getChat(this.chat.id, ...args);
255 }
256 /**
257 * @see https://core.telegram.org/bots/api#exportchatinvitelink
258 */
259 exportChatInviteLink(...args) {
260 this.assert(this.chat, 'exportChatInviteLink');
261 return this.telegram.exportChatInviteLink(this.chat.id, ...args);
262 }
263 /**
264 * @see https://core.telegram.org/bots/api#createchatinvitelink
265 */
266 createChatInviteLink(...args) {
267 this.assert(this.chat, 'createChatInviteLink');
268 return this.telegram.createChatInviteLink(this.chat.id, ...args);
269 }
270 /**
271 * @see https://core.telegram.org/bots/api#editchatinvitelink
272 */
273 editChatInviteLink(...args) {
274 this.assert(this.chat, 'editChatInviteLink');
275 return this.telegram.editChatInviteLink(this.chat.id, ...args);
276 }
277 /**
278 * @see https://core.telegram.org/bots/api#revokechatinvitelink
279 */
280 revokeChatInviteLink(...args) {
281 this.assert(this.chat, 'revokeChatInviteLink');
282 return this.telegram.revokeChatInviteLink(this.chat.id, ...args);
283 }
284 /**
285 * @see https://core.telegram.org/bots/api#banchatmember
286 */
287 banChatMember(...args) {
288 this.assert(this.chat, 'banChatMember');
289 return this.telegram.banChatMember(this.chat.id, ...args);
290 }
291 /**
292 * @see https://core.telegram.org/bots/api#banchatmember
293 * @deprecated since API 5.3. Use {@link Context.banChatMember}
294 */
295 get kickChatMember() {
296 return this.banChatMember;
297 }
298 /**
299 * @see https://core.telegram.org/bots/api#unbanchatmember
300 */
301 unbanChatMember(...args) {
302 this.assert(this.chat, 'unbanChatMember');
303 return this.telegram.unbanChatMember(this.chat.id, ...args);
304 }
305 /**
306 * @see https://core.telegram.org/bots/api#restrictchatmember
307 */
308 restrictChatMember(...args) {
309 this.assert(this.chat, 'restrictChatMember');
310 return this.telegram.restrictChatMember(this.chat.id, ...args);
311 }
312 /**
313 * @see https://core.telegram.org/bots/api#promotechatmember
314 */
315 promoteChatMember(...args) {
316 this.assert(this.chat, 'promoteChatMember');
317 return this.telegram.promoteChatMember(this.chat.id, ...args);
318 }
319 /**
320 * @see https://core.telegram.org/bots/api#setchatadministratorcustomtitle
321 */
322 setChatAdministratorCustomTitle(...args) {
323 this.assert(this.chat, 'setChatAdministratorCustomTitle');
324 return this.telegram.setChatAdministratorCustomTitle(this.chat.id, ...args);
325 }
326 /**
327 * @see https://core.telegram.org/bots/api#setchatphoto
328 */
329 setChatPhoto(...args) {
330 this.assert(this.chat, 'setChatPhoto');
331 return this.telegram.setChatPhoto(this.chat.id, ...args);
332 }
333 /**
334 * @see https://core.telegram.org/bots/api#deletechatphoto
335 */
336 deleteChatPhoto(...args) {
337 this.assert(this.chat, 'deleteChatPhoto');
338 return this.telegram.deleteChatPhoto(this.chat.id, ...args);
339 }
340 /**
341 * @see https://core.telegram.org/bots/api#setchattitle
342 */
343 setChatTitle(...args) {
344 this.assert(this.chat, 'setChatTitle');
345 return this.telegram.setChatTitle(this.chat.id, ...args);
346 }
347 /**
348 * @see https://core.telegram.org/bots/api#setchatdescription
349 */
350 setChatDescription(...args) {
351 this.assert(this.chat, 'setChatDescription');
352 return this.telegram.setChatDescription(this.chat.id, ...args);
353 }
354 /**
355 * @see https://core.telegram.org/bots/api#pinchatmessage
356 */
357 pinChatMessage(...args) {
358 this.assert(this.chat, 'pinChatMessage');
359 return this.telegram.pinChatMessage(this.chat.id, ...args);
360 }
361 /**
362 * @see https://core.telegram.org/bots/api#unpinchatmessage
363 */
364 unpinChatMessage(...args) {
365 this.assert(this.chat, 'unpinChatMessage');
366 return this.telegram.unpinChatMessage(this.chat.id, ...args);
367 }
368 /**
369 * @see https://core.telegram.org/bots/api#unpinallchatmessages
370 */
371 unpinAllChatMessages(...args) {
372 this.assert(this.chat, 'unpinAllChatMessages');
373 return this.telegram.unpinAllChatMessages(this.chat.id, ...args);
374 }
375 /**
376 * @see https://core.telegram.org/bots/api#leavechat
377 */
378 leaveChat(...args) {
379 this.assert(this.chat, 'leaveChat');
380 return this.telegram.leaveChat(this.chat.id, ...args);
381 }
382 /**
383 * @see https://core.telegram.org/bots/api#setchatpermissions
384 */
385 setChatPermissions(...args) {
386 this.assert(this.chat, 'setChatPermissions');
387 return this.telegram.setChatPermissions(this.chat.id, ...args);
388 }
389 /**
390 * @see https://core.telegram.org/bots/api#getchatadministrators
391 */
392 getChatAdministrators(...args) {
393 this.assert(this.chat, 'getChatAdministrators');
394 return this.telegram.getChatAdministrators(this.chat.id, ...args);
395 }
396 /**
397 * @see https://core.telegram.org/bots/api#getchatmember
398 */
399 getChatMember(...args) {
400 this.assert(this.chat, 'getChatMember');
401 return this.telegram.getChatMember(this.chat.id, ...args);
402 }
403 /**
404 * @see https://core.telegram.org/bots/api#getchatmembercount
405 */
406 getChatMembersCount(...args) {
407 this.assert(this.chat, 'getChatMembersCount');
408 return this.telegram.getChatMembersCount(this.chat.id, ...args);
409 }
410 /**
411 * @see https://core.telegram.org/bots/api#setpassportdataerrors
412 */
413 setPassportDataErrors(errors) {
414 this.assert(this.from, 'setPassportDataErrors');
415 return this.telegram.setPassportDataErrors(this.from.id, errors);
416 }
417 /**
418 * @see https://core.telegram.org/bots/api#sendphoto
419 */
420 sendPhoto(photo, extra) {
421 this.assert(this.chat, 'sendPhoto');
422 return this.telegram.sendPhoto(this.chat.id, photo, {
423 message_thread_id: getThreadId(this),
424 ...extra,
425 });
426 }
427 /**
428 * @see https://core.telegram.org/bots/api#sendphoto
429 */
430 replyWithPhoto(...args) {
431 return this.sendPhoto(...args);
432 }
433 /**
434 * @see https://core.telegram.org/bots/api#sendmediagroup
435 */
436 sendMediaGroup(media, extra) {
437 this.assert(this.chat, 'sendMediaGroup');
438 return this.telegram.sendMediaGroup(this.chat.id, media, {
439 message_thread_id: getThreadId(this),
440 ...extra,
441 });
442 }
443 /**
444 * @see https://core.telegram.org/bots/api#sendmediagroup
445 */
446 replyWithMediaGroup(...args) {
447 return this.sendMediaGroup(...args);
448 }
449 /**
450 * @see https://core.telegram.org/bots/api#sendaudio
451 */
452 sendAudio(audio, extra) {
453 this.assert(this.chat, 'sendAudio');
454 return this.telegram.sendAudio(this.chat.id, audio, {
455 message_thread_id: getThreadId(this),
456 ...extra,
457 });
458 }
459 /**
460 * @see https://core.telegram.org/bots/api#sendaudio
461 */
462 replyWithAudio(...args) {
463 return this.sendAudio(...args);
464 }
465 /**
466 * @see https://core.telegram.org/bots/api#senddice
467 */
468 sendDice(extra) {
469 this.assert(this.chat, 'sendDice');
470 return this.telegram.sendDice(this.chat.id, {
471 message_thread_id: getThreadId(this),
472 ...extra,
473 });
474 }
475 /**
476 * @see https://core.telegram.org/bots/api#senddice
477 */
478 replyWithDice(...args) {
479 return this.sendDice(...args);
480 }
481 /**
482 * @see https://core.telegram.org/bots/api#senddocument
483 */
484 sendDocument(document, extra) {
485 this.assert(this.chat, 'sendDocument');
486 return this.telegram.sendDocument(this.chat.id, document, {
487 message_thread_id: getThreadId(this),
488 ...extra,
489 });
490 }
491 /**
492 * @see https://core.telegram.org/bots/api#senddocument
493 */
494 replyWithDocument(...args) {
495 return this.sendDocument(...args);
496 }
497 /**
498 * @see https://core.telegram.org/bots/api#sendsticker
499 */
500 sendSticker(sticker, extra) {
501 this.assert(this.chat, 'sendSticker');
502 return this.telegram.sendSticker(this.chat.id, sticker, {
503 message_thread_id: getThreadId(this),
504 ...extra,
505 });
506 }
507 /**
508 * @see https://core.telegram.org/bots/api#sendsticker
509 */
510 replyWithSticker(...args) {
511 return this.sendSticker(...args);
512 }
513 /**
514 * @see https://core.telegram.org/bots/api#sendvideo
515 */
516 sendVideo(video, extra) {
517 this.assert(this.chat, 'sendVideo');
518 return this.telegram.sendVideo(this.chat.id, video, {
519 message_thread_id: getThreadId(this),
520 ...extra,
521 });
522 }
523 /**
524 * @see https://core.telegram.org/bots/api#sendvideo
525 */
526 replyWithVideo(...args) {
527 return this.sendVideo(...args);
528 }
529 /**
530 * @see https://core.telegram.org/bots/api#sendanimation
531 */
532 sendAnimation(animation, extra) {
533 this.assert(this.chat, 'sendAnimation');
534 return this.telegram.sendAnimation(this.chat.id, animation, {
535 message_thread_id: getThreadId(this),
536 ...extra,
537 });
538 }
539 /**
540 * @see https://core.telegram.org/bots/api#sendanimation
541 */
542 replyWithAnimation(...args) {
543 return this.sendAnimation(...args);
544 }
545 /**
546 * @see https://core.telegram.org/bots/api#sendvideonote
547 */
548 sendVideoNote(videoNote, extra) {
549 this.assert(this.chat, 'sendVideoNote');
550 return this.telegram.sendVideoNote(this.chat.id, videoNote, {
551 message_thread_id: getThreadId(this),
552 ...extra,
553 });
554 }
555 /**
556 * @see https://core.telegram.org/bots/api#sendvideonote
557 */
558 replyWithVideoNote(...args) {
559 return this.sendVideoNote(...args);
560 }
561 /**
562 * @see https://core.telegram.org/bots/api#sendinvoice
563 */
564 sendInvoice(invoice, extra) {
565 this.assert(this.chat, 'sendInvoice');
566 return this.telegram.sendInvoice(this.chat.id, invoice, {
567 message_thread_id: getThreadId(this),
568 ...extra,
569 });
570 }
571 /**
572 * @see https://core.telegram.org/bots/api#sendinvoice
573 */
574 replyWithInvoice(...args) {
575 return this.sendInvoice(...args);
576 }
577 /**
578 * @see https://core.telegram.org/bots/api#sendgame
579 */
580 sendGame(game, extra) {
581 this.assert(this.chat, 'sendGame');
582 return this.telegram.sendGame(this.chat.id, game, {
583 message_thread_id: getThreadId(this),
584 ...extra,
585 });
586 }
587 /**
588 * @see https://core.telegram.org/bots/api#sendgame
589 */
590 replyWithGame(...args) {
591 return this.sendGame(...args);
592 }
593 /**
594 * @see https://core.telegram.org/bots/api#sendvoice
595 */
596 sendVoice(voice, extra) {
597 this.assert(this.chat, 'sendVoice');
598 return this.telegram.sendVoice(this.chat.id, voice, {
599 message_thread_id: getThreadId(this),
600 ...extra,
601 });
602 }
603 /**
604 * @see https://core.telegram.org/bots/api#sendvoice
605 */
606 replyWithVoice(...args) {
607 return this.sendVoice(...args);
608 }
609 /**
610 * @see https://core.telegram.org/bots/api#sendpoll
611 */
612 sendPoll(poll, options, extra) {
613 this.assert(this.chat, 'sendPoll');
614 return this.telegram.sendPoll(this.chat.id, poll, options, {
615 message_thread_id: getThreadId(this),
616 ...extra,
617 });
618 }
619 /**
620 * @see https://core.telegram.org/bots/api#sendpoll
621 */
622 replyWithPoll(...args) {
623 return this.sendPoll(...args);
624 }
625 /**
626 * @see https://core.telegram.org/bots/api#sendpoll
627 */
628 sendQuiz(quiz, options, extra) {
629 this.assert(this.chat, 'sendQuiz');
630 return this.telegram.sendQuiz(this.chat.id, quiz, options, {
631 message_thread_id: getThreadId(this),
632 ...extra,
633 });
634 }
635 /**
636 * @see https://core.telegram.org/bots/api#sendpoll
637 */
638 replyWithQuiz(...args) {
639 return this.sendQuiz(...args);
640 }
641 /**
642 * @see https://core.telegram.org/bots/api#stoppoll
643 */
644 stopPoll(...args) {
645 this.assert(this.chat, 'stopPoll');
646 return this.telegram.stopPoll(this.chat.id, ...args);
647 }
648 /**
649 * @see https://core.telegram.org/bots/api#sendchataction
650 */
651 sendChatAction(action, extra) {
652 this.assert(this.chat, 'sendChatAction');
653 return this.telegram.sendChatAction(this.chat.id, action, {
654 message_thread_id: getThreadId(this),
655 ...extra,
656 });
657 }
658 /**
659 * @see https://core.telegram.org/bots/api#sendchataction
660 *
661 * Sends the sendChatAction request repeatedly, with a delay between requests,
662 * as long as the provided callback function is being processed.
663 *
664 * The sendChatAction errors should be ignored, because the goal is the actual long process completing and performing an action.
665 *
666 * @param action - chat action type.
667 * @param callback - a function to run along with the chat action.
668 * @param extra - extra parameters for sendChatAction.
669 * @param {number} [extra.intervalDuration=8000] - The duration (in milliseconds) between subsequent sendChatAction requests.
670 */
671 async persistentChatAction(action, callback, { intervalDuration, ...extra } = {}) {
672 await this.sendChatAction(action, { ...extra });
673 const timer = setInterval(() => this.sendChatAction(action, { ...extra }).catch((err) => {
674 debug('Ignored error while persisting sendChatAction:', err);
675 }), intervalDuration !== null && intervalDuration !== void 0 ? intervalDuration : 4000);
676 try {
677 await callback();
678 }
679 finally {
680 clearInterval(timer);
681 }
682 }
683 /**
684 * @deprecated use {@link Context.sendChatAction} instead
685 * @see https://core.telegram.org/bots/api#sendchataction
686 */
687 replyWithChatAction(...args) {
688 return this.sendChatAction(...args);
689 }
690 /**
691 * @see https://core.telegram.org/bots/api#sendlocation
692 */
693 sendLocation(latitude, longitude, extra) {
694 this.assert(this.chat, 'sendLocation');
695 return this.telegram.sendLocation(this.chat.id, latitude, longitude, {
696 message_thread_id: getThreadId(this),
697 ...extra,
698 });
699 }
700 /**
701 * @see https://core.telegram.org/bots/api#sendlocation
702 */
703 replyWithLocation(...args) {
704 return this.sendLocation(...args);
705 }
706 /**
707 * @see https://core.telegram.org/bots/api#sendvenue
708 */
709 sendVenue(latitude, longitude, title, address, extra) {
710 this.assert(this.chat, 'sendVenue');
711 return this.telegram.sendVenue(this.chat.id, latitude, longitude, title, address, { message_thread_id: getThreadId(this), ...extra });
712 }
713 /**
714 * @see https://core.telegram.org/bots/api#sendvenue
715 */
716 replyWithVenue(...args) {
717 return this.sendVenue(...args);
718 }
719 /**
720 * @see https://core.telegram.org/bots/api#sendcontact
721 */
722 sendContact(phoneNumber, firstName, extra) {
723 this.assert(this.chat, 'sendContact');
724 return this.telegram.sendContact(this.chat.id, phoneNumber, firstName, {
725 message_thread_id: getThreadId(this),
726 ...extra,
727 });
728 }
729 /**
730 * @see https://core.telegram.org/bots/api#sendcontact
731 */
732 replyWithContact(...args) {
733 return this.sendContact(...args);
734 }
735 /**
736 * @deprecated use {@link Telegram.getStickerSet}
737 * @see https://core.telegram.org/bots/api#getstickerset
738 */
739 getStickerSet(setName) {
740 return this.telegram.getStickerSet(setName);
741 }
742 /**
743 * @see https://core.telegram.org/bots/api#setchatstickerset
744 */
745 setChatStickerSet(setName) {
746 this.assert(this.chat, 'setChatStickerSet');
747 return this.telegram.setChatStickerSet(this.chat.id, setName);
748 }
749 /**
750 * @see https://core.telegram.org/bots/api#deletechatstickerset
751 */
752 deleteChatStickerSet() {
753 this.assert(this.chat, 'deleteChatStickerSet');
754 return this.telegram.deleteChatStickerSet(this.chat.id);
755 }
756 /**
757 * Use this method to create a topic in a forum supergroup chat. The bot must be an administrator in the chat for this
758 * to work and must have the can_manage_topics administrator rights. Returns information about the created topic as a
759 * ForumTopic object.
760 *
761 * @see https://core.telegram.org/bots/api#createforumtopic
762 */
763 createForumTopic(...args) {
764 this.assert(this.chat, 'createForumTopic');
765 return this.telegram.createForumTopic(this.chat.id, ...args);
766 }
767 /**
768 * Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be an administrator in
769 * the chat for this to work and must have can_manage_topics administrator rights, unless it is the creator of the
770 * topic. Returns True on success.
771 *
772 * @see https://core.telegram.org/bots/api#editforumtopic
773 */
774 editForumTopic(extra) {
775 var _a;
776 this.assert(this.chat, 'editForumTopic');
777 this.assert((_a = this.message) === null || _a === void 0 ? void 0 : _a.message_thread_id, 'editForumTopic');
778 return this.telegram.editForumTopic(this.chat.id, this.message.message_thread_id, extra);
779 }
780 /**
781 * Use this method to close an open topic in a forum supergroup chat. The bot must be an administrator in the chat
782 * for this to work and must have the can_manage_topics administrator rights, unless it is the creator of the topic.
783 * Returns True on success.
784 *
785 * @see https://core.telegram.org/bots/api#closeforumtopic
786 */
787 closeForumTopic() {
788 var _a;
789 this.assert(this.chat, 'closeForumTopic');
790 this.assert((_a = this.message) === null || _a === void 0 ? void 0 : _a.message_thread_id, 'closeForumTopic');
791 return this.telegram.closeForumTopic(this.chat.id, this.message.message_thread_id);
792 }
793 /**
794 * Use this method to reopen a closed topic in a forum supergroup chat. The bot must be an administrator in the chat
795 * for this to work and must have the can_manage_topics administrator rights, unless it is the creator of the topic.
796 * Returns True on success.
797 *
798 * @see https://core.telegram.org/bots/api#reopenforumtopic
799 */
800 reopenForumTopic() {
801 var _a;
802 this.assert(this.chat, 'reopenForumTopic');
803 this.assert((_a = this.message) === null || _a === void 0 ? void 0 : _a.message_thread_id, 'reopenForumTopic');
804 return this.telegram.reopenForumTopic(this.chat.id, this.message.message_thread_id);
805 }
806 /**
807 * Use this method to delete a forum topic along with all its messages in a forum supergroup chat. The bot must be an
808 * administrator in the chat for this to work and must have the can_delete_messages administrator rights.
809 * Returns True on success.
810 *
811 * @see https://core.telegram.org/bots/api#deleteforumtopic
812 */
813 deleteForumTopic() {
814 var _a;
815 this.assert(this.chat, 'deleteForumTopic');
816 this.assert((_a = this.message) === null || _a === void 0 ? void 0 : _a.message_thread_id, 'deleteForumTopic');
817 return this.telegram.deleteForumTopic(this.chat.id, this.message.message_thread_id);
818 }
819 /**
820 * Use this method to clear the list of pinned messages in a forum topic. The bot must be an administrator in the chat
821 * for this to work and must have the can_pin_messages administrator right in the supergroup. Returns True on success.
822 *
823 * @see https://core.telegram.org/bots/api#unpinallforumtopicmessages
824 */
825 unpinAllForumTopicMessages() {
826 var _a;
827 this.assert(this.chat, 'unpinAllForumTopicMessages');
828 this.assert((_a = this.message) === null || _a === void 0 ? void 0 : _a.message_thread_id, 'unpinAllForumTopicMessages');
829 return this.telegram.unpinAllForumTopicMessages(this.chat.id, this.message.message_thread_id);
830 }
831 /**
832 * Use this method to edit the name of the 'General' topic in a forum supergroup chat. The bot must be an administrator
833 * in the chat for this to work and must have can_manage_topics administrator rights. Returns True on success.
834 *
835 * @see https://core.telegram.org/bots/api#editgeneralforumtopic
836 */
837 editGeneralForumTopic(name) {
838 this.assert(this.chat, 'editGeneralForumTopic');
839 return this.telegram.editGeneralForumTopic(this.chat.id, name);
840 }
841 /**
842 * Use this method to close an open 'General' topic in a forum supergroup chat. The bot must be an administrator in the
843 * chat for this to work and must have the can_manage_topics administrator rights. Returns True on success.
844 *
845 * @see https://core.telegram.org/bots/api#closegeneralforumtopic
846 */
847 closeGeneralForumTopic() {
848 this.assert(this.chat, 'closeGeneralForumTopic');
849 return this.telegram.closeGeneralForumTopic(this.chat.id);
850 }
851 /**
852 * Use this method to reopen a closed 'General' topic in a forum supergroup chat. The bot must be an administrator in
853 * the chat for this to work and must have the can_manage_topics administrator rights. The topic will be automatically
854 * unhidden if it was hidden. Returns True on success.
855 *
856 * @see https://core.telegram.org/bots/api#reopengeneralforumtopic
857 */
858 reopenGeneralForumTopic() {
859 this.assert(this.chat, 'reopenGeneralForumTopic');
860 return this.telegram.reopenGeneralForumTopic(this.chat.id);
861 }
862 /**
863 * Use this method to hide the 'General' topic in a forum supergroup chat. The bot must be an administrator in the chat
864 * for this to work and must have the can_manage_topics administrator rights. The topic will be automatically closed
865 * if it was open. Returns True on success.
866 *
867 * @see https://core.telegram.org/bots/api#hidegeneralforumtopic
868 */
869 hideGeneralForumTopic() {
870 this.assert(this.chat, 'hideGeneralForumTopic');
871 return this.telegram.hideGeneralForumTopic(this.chat.id);
872 }
873 /**
874 * Use this method to unhide the 'General' topic in a forum supergroup chat. The bot must be an administrator in the
875 * chat for this to work and must have the can_manage_topics administrator rights. Returns True on success.
876 *
877 * @see https://core.telegram.org/bots/api#unhidegeneralforumtopic
878 */
879 unhideGeneralForumTopic() {
880 this.assert(this.chat, 'unhideGeneralForumTopic');
881 return this.telegram.unhideGeneralForumTopic(this.chat.id);
882 }
883 /**
884 * Use this method to clear the list of pinned messages in a General forum topic.
885 * The bot must be an administrator in the chat for this to work and must have the can_pin_messages administrator
886 * right in the supergroup.
887 *
888 * @param chat_id Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
889 *
890 * @see https://core.telegram.org/bots/api#unpinallgeneralforumtopicmessages
891 */
892 unpinAllGeneralForumTopicMessages() {
893 this.assert(this.chat, 'unpinAllGeneralForumTopicMessages');
894 return this.telegram.unpinAllGeneralForumTopicMessages(this.chat.id);
895 }
896 /**
897 * @deprecated use {@link Telegram.setStickerPositionInSet}
898 * @see https://core.telegram.org/bots/api#setstickerpositioninset
899 */
900 setStickerPositionInSet(sticker, position) {
901 return this.telegram.setStickerPositionInSet(sticker, position);
902 }
903 /**
904 * @deprecated use {@link Telegram.setStickerSetThumbnail}
905 * @see https://core.telegram.org/bots/api#setstickersetthumbnail
906 */
907 setStickerSetThumb(...args) {
908 return this.telegram.setStickerSetThumbnail(...args);
909 }
910 setStickerSetThumbnail(...args) {
911 return this.telegram.setStickerSetThumbnail(...args);
912 }
913 setStickerMaskPosition(...args) {
914 return this.telegram.setStickerMaskPosition(...args);
915 }
916 setStickerKeywords(...args) {
917 return this.telegram.setStickerKeywords(...args);
918 }
919 setStickerEmojiList(...args) {
920 return this.telegram.setStickerEmojiList(...args);
921 }
922 deleteStickerSet(...args) {
923 return this.telegram.deleteStickerSet(...args);
924 }
925 setStickerSetTitle(...args) {
926 return this.telegram.setStickerSetTitle(...args);
927 }
928 setCustomEmojiStickerSetThumbnail(...args) {
929 return this.telegram.setCustomEmojiStickerSetThumbnail(...args);
930 }
931 /**
932 * @deprecated use {@link Telegram.deleteStickerFromSet}
933 * @see https://core.telegram.org/bots/api#deletestickerfromset
934 */
935 deleteStickerFromSet(sticker) {
936 return this.telegram.deleteStickerFromSet(sticker);
937 }
938 /**
939 * @see https://core.telegram.org/bots/api#uploadstickerfile
940 */
941 uploadStickerFile(...args) {
942 this.assert(this.from, 'uploadStickerFile');
943 return this.telegram.uploadStickerFile(this.from.id, ...args);
944 }
945 /**
946 * @see https://core.telegram.org/bots/api#createnewstickerset
947 */
948 createNewStickerSet(...args) {
949 this.assert(this.from, 'createNewStickerSet');
950 return this.telegram.createNewStickerSet(this.from.id, ...args);
951 }
952 /**
953 * @see https://core.telegram.org/bots/api#addstickertoset
954 */
955 addStickerToSet(...args) {
956 this.assert(this.from, 'addStickerToSet');
957 return this.telegram.addStickerToSet(this.from.id, ...args);
958 }
959 /**
960 * @deprecated use {@link Telegram.getMyCommands}
961 * @see https://core.telegram.org/bots/api#getmycommands
962 */
963 getMyCommands() {
964 return this.telegram.getMyCommands();
965 }
966 /**
967 * @deprecated use {@link Telegram.setMyCommands}
968 * @see https://core.telegram.org/bots/api#setmycommands
969 */
970 setMyCommands(commands) {
971 return this.telegram.setMyCommands(commands);
972 }
973 /**
974 * @deprecated use {@link Context.replyWithMarkdownV2}
975 * @see https://core.telegram.org/bots/api#sendmessage
976 */
977 replyWithMarkdown(markdown, extra) {
978 return this.reply(markdown, { parse_mode: 'Markdown', ...extra });
979 }
980 /**
981 * @see https://core.telegram.org/bots/api#sendmessage
982 */
983 replyWithMarkdownV2(markdown, extra) {
984 return this.reply(markdown, { parse_mode: 'MarkdownV2', ...extra });
985 }
986 /**
987 * @see https://core.telegram.org/bots/api#sendmessage
988 */
989 replyWithHTML(html, extra) {
990 return this.reply(html, { parse_mode: 'HTML', ...extra });
991 }
992 /**
993 * @see https://core.telegram.org/bots/api#deletemessage
994 */
995 deleteMessage(messageId) {
996 this.assert(this.chat, 'deleteMessage');
997 if (typeof messageId !== 'undefined') {
998 return this.telegram.deleteMessage(this.chat.id, messageId);
999 }
1000 const message = getMessageFromAnySource(this);
1001 this.assert(message, 'deleteMessage');
1002 return this.telegram.deleteMessage(this.chat.id, message.message_id);
1003 }
1004 /**
1005 * @see https://core.telegram.org/bots/api#forwardmessage
1006 */
1007 forwardMessage(chatId, extra) {
1008 const message = getMessageFromAnySource(this);
1009 this.assert(message, 'forwardMessage');
1010 return this.telegram.forwardMessage(chatId, message.chat.id, message.message_id, extra);
1011 }
1012 /**
1013 * @see https://core.telegram.org/bots/api#copymessage
1014 */
1015 copyMessage(chatId, extra) {
1016 const message = getMessageFromAnySource(this);
1017 this.assert(message, 'copyMessage');
1018 return this.telegram.copyMessage(chatId, message.chat.id, message.message_id, extra);
1019 }
1020 /**
1021 * @see https://core.telegram.org/bots/api#approvechatjoinrequest
1022 */
1023 approveChatJoinRequest(userId) {
1024 this.assert(this.chat, 'approveChatJoinRequest');
1025 return this.telegram.approveChatJoinRequest(this.chat.id, userId);
1026 }
1027 /**
1028 * @see https://core.telegram.org/bots/api#declinechatjoinrequest
1029 */
1030 declineChatJoinRequest(userId) {
1031 this.assert(this.chat, 'declineChatJoinRequest');
1032 return this.telegram.declineChatJoinRequest(this.chat.id, userId);
1033 }
1034 /**
1035 * @see https://core.telegram.org/bots/api#banchatsenderchat
1036 */
1037 banChatSenderChat(senderChatId) {
1038 this.assert(this.chat, 'banChatSenderChat');
1039 return this.telegram.banChatSenderChat(this.chat.id, senderChatId);
1040 }
1041 /**
1042 * @see https://core.telegram.org/bots/api#unbanchatsenderchat
1043 */
1044 unbanChatSenderChat(senderChatId) {
1045 this.assert(this.chat, 'unbanChatSenderChat');
1046 return this.telegram.unbanChatSenderChat(this.chat.id, senderChatId);
1047 }
1048 /**
1049 * Use this method to change the bot's menu button in the current private chat. Returns true on success.
1050 * @see https://core.telegram.org/bots/api#setchatmenubutton
1051 */
1052 setChatMenuButton(menuButton) {
1053 this.assert(this.chat, 'setChatMenuButton');
1054 return this.telegram.setChatMenuButton({ chatId: this.chat.id, menuButton });
1055 }
1056 /**
1057 * Use this method to get the current value of the bot's menu button in the current private chat. Returns MenuButton on success.
1058 * @see https://core.telegram.org/bots/api#getchatmenubutton
1059 */
1060 getChatMenuButton() {
1061 this.assert(this.chat, 'getChatMenuButton');
1062 return this.telegram.getChatMenuButton({ chatId: this.chat.id });
1063 }
1064 /**
1065 * @see https://core.telegram.org/bots/api#setmydefaultadministratorrights
1066 */
1067 setMyDefaultAdministratorRights(extra) {
1068 return this.telegram.setMyDefaultAdministratorRights(extra);
1069 }
1070 /**
1071 * @see https://core.telegram.org/bots/api#getmydefaultadministratorrights
1072 */
1073 getMyDefaultAdministratorRights(extra) {
1074 return this.telegram.getMyDefaultAdministratorRights(extra);
1075 }
1076}
1077exports.Context = Context;
1078exports.default = Context;
1079function getMessageFromAnySource(ctx) {
1080 var _a, _b, _c, _d, _e;
1081 return ((_e = (_d = (_b = (_a = ctx.message) !== null && _a !== void 0 ? _a : ctx.editedMessage) !== null && _b !== void 0 ? _b : (_c = ctx.callbackQuery) === null || _c === void 0 ? void 0 : _c.message) !== null && _d !== void 0 ? _d : ctx.channelPost) !== null && _e !== void 0 ? _e : ctx.editedChannelPost);
1082}
1083const getThreadId = (ctx) => {
1084 const msg = getMessageFromAnySource(ctx);
1085 return (msg === null || msg === void 0 ? void 0 : msg.is_topic_message) ? msg.message_thread_id : undefined;
1086};