grammy-tools 1.36.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +339 -0
  3. package/out/bot.d.ts +314 -0
  4. package/out/bot.js +669 -0
  5. package/out/composer.d.ts +764 -0
  6. package/out/composer.js +704 -0
  7. package/out/context.d.ts +1618 -0
  8. package/out/context.js +1976 -0
  9. package/out/convenience/constants.d.ts +98 -0
  10. package/out/convenience/constants.js +37 -0
  11. package/out/convenience/frameworks.d.ts +216 -0
  12. package/out/convenience/frameworks.js +370 -0
  13. package/out/convenience/inline_query.d.ts +401 -0
  14. package/out/convenience/inline_query.js +461 -0
  15. package/out/convenience/input_media.d.ts +72 -0
  16. package/out/convenience/input_media.js +82 -0
  17. package/out/convenience/keyboard.d.ts +763 -0
  18. package/out/convenience/keyboard.js +935 -0
  19. package/out/convenience/session.d.ts +345 -0
  20. package/out/convenience/session.js +455 -0
  21. package/out/convenience/webhook.d.ts +59 -0
  22. package/out/convenience/webhook.js +80 -0
  23. package/out/core/api.d.ts +1628 -0
  24. package/out/core/api.js +1907 -0
  25. package/out/core/client.d.ts +180 -0
  26. package/out/core/client.js +226 -0
  27. package/out/core/error.d.ts +53 -0
  28. package/out/core/error.js +87 -0
  29. package/out/core/payload.d.ts +39 -0
  30. package/out/core/payload.js +199 -0
  31. package/out/filter.d.ts +959 -0
  32. package/out/filter.js +378 -0
  33. package/out/mod.d.ts +14 -0
  34. package/out/mod.js +43 -0
  35. package/out/platform.node.d.ts +16 -0
  36. package/out/platform.node.js +43 -0
  37. package/out/shim.node.d.ts +2 -0
  38. package/out/shim.node.js +7 -0
  39. package/out/types.d.ts +1 -0
  40. package/out/types.js +17 -0
  41. package/out/types.node.d.ts +78 -0
  42. package/out/types.node.js +107 -0
  43. package/out/web.d.ts +1 -0
  44. package/out/web.mjs +5172 -0
  45. package/package.json +71 -0
@@ -0,0 +1,935 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InlineKeyboard = exports.Keyboard = void 0;
4
+ /**
5
+ * Use this class to simplify building a custom keyboard (something like this:
6
+ * https://core.telegram.org/bots/features#keyboards).
7
+ *
8
+ * ```ts
9
+ * // Build a custom keyboard:
10
+ * const keyboard = new Keyboard()
11
+ * .text('A').text('B').row()
12
+ * .text('C').text('D')
13
+ *
14
+ * // Now you can send it like so:
15
+ * await ctx.reply('Here is your custom keyboard!', {
16
+ * reply_markup: keyboard
17
+ * })
18
+ * ```
19
+ *
20
+ * If you already have some source data which you would like to turn into a
21
+ * keyboard button object, you can use the static equivalents which every button
22
+ * has. You can use them to create a two-dimensional keyboard button array. The
23
+ * resulting array can be turned into a keyboard instance.
24
+ *
25
+ * ```ts
26
+ * const button = Keyboard.text('push my buttons')
27
+ * const array = [[button]]
28
+ * const keyboard = Keyboard.from(array)
29
+ * ```
30
+ *
31
+ * If you want to create text buttons only, you can directly use a
32
+ * two-dimensional string array and turn it into a keyboard.
33
+ *
34
+ * ```ts
35
+ * const data = [['A', 'B'], ['C', 'D']]
36
+ * const keyboard = Keyboard.from(data)
37
+ * ```
38
+ *
39
+ * Be sure to check out the
40
+ * [documentation](https://grammy.dev/plugins/keyboard#custom-keyboards) on
41
+ * custom keyboards in grammY.
42
+ */
43
+ class Keyboard {
44
+ /**
45
+ * Initialize a new `Keyboard` with an optional two-dimensional array of
46
+ * `KeyboardButton` objects. This is the nested array that holds the custom
47
+ * keyboard. It will be extended every time you call one of the provided
48
+ * methods.
49
+ *
50
+ * @param keyboard An optional initial two-dimensional button array
51
+ */
52
+ constructor(keyboard = [[]]) {
53
+ this.keyboard = keyboard;
54
+ }
55
+ /**
56
+ * Allows you to add your own `KeyboardButton` objects if you already have
57
+ * them for some reason. You most likely want to call one of the other
58
+ * methods.
59
+ *
60
+ * @param buttons The buttons to add
61
+ */
62
+ add(...buttons) {
63
+ var _a;
64
+ (_a = this.keyboard[this.keyboard.length - 1]) === null || _a === void 0 ? void 0 : _a.push(...buttons);
65
+ return this;
66
+ }
67
+ /**
68
+ * Adds a 'line break'. Call this method to make sure that the next added
69
+ * buttons will be on a new row.
70
+ *
71
+ * You may pass a number of `KeyboardButton` objects if you already have the
72
+ * instances for some reason. You most likely don't want to pass any
73
+ * arguments to `row`.
74
+ *
75
+ * @param buttons A number of buttons to add to the next row
76
+ */
77
+ row(...buttons) {
78
+ this.keyboard.push(buttons);
79
+ return this;
80
+ }
81
+ /**
82
+ * Adds a new text button. This button will simply send the given text as a
83
+ * text message back to your bot if a user clicks on it.
84
+ *
85
+ * @param text The text to display
86
+ */
87
+ text(text) {
88
+ return this.add(Keyboard.text(text));
89
+ }
90
+ /**
91
+ * Creates a new text button. This button will simply send the given text as
92
+ * a text message back to your bot if a user clicks on it.
93
+ *
94
+ * @param text The text to display
95
+ */
96
+ static text(text) {
97
+ return { text };
98
+ }
99
+ /**
100
+ * Adds a new request users button. When the user presses the button, a list
101
+ * of suitable users will be opened. Tapping on any number of users will
102
+ * send their identifiers to the bot in a “users_shared” service message.
103
+ * Available in private chats only.
104
+ *
105
+ * @param text The text to display
106
+ * @param requestId A signed 32-bit identifier of the request
107
+ * @param options Options object for further requirements
108
+ */
109
+ requestUsers(text, requestId, options = {}) {
110
+ return this.add(Keyboard.requestUsers(text, requestId, options));
111
+ }
112
+ /**
113
+ * Creates a new request users button. When the user presses the button, a
114
+ * list of suitable users will be opened. Tapping on any number of users
115
+ * will send their identifiers to the bot in a “users_shared” service
116
+ * message. Available in private chats only.
117
+ *
118
+ * @param text The text to display
119
+ * @param requestId A signed 32-bit identifier of the request
120
+ * @param options Options object for further requirements
121
+ */
122
+ static requestUsers(text, requestId, options = {}) {
123
+ return { text, request_users: { request_id: requestId, ...options } };
124
+ }
125
+ /**
126
+ * Adds a new request chat button. When the user presses the button, a list
127
+ * of suitable users will be opened. Tapping on a chat will send its
128
+ * identifier to the bot in a “chat_shared” service message. Available in
129
+ * private chats only.
130
+ *
131
+ * @param text The text to display
132
+ * @param requestId A signed 32-bit identifier of the request
133
+ * @param options Options object for further requirements
134
+ */
135
+ requestChat(text, requestId, options = {
136
+ chat_is_channel: false,
137
+ }) {
138
+ return this.add(Keyboard.requestChat(text, requestId, options));
139
+ }
140
+ /**
141
+ * Creates a new request chat button. When the user presses the button, a
142
+ * list of suitable users will be opened. Tapping on a chat will send its
143
+ * identifier to the bot in a “chat_shared” service message. Available in
144
+ * private chats only.
145
+ *
146
+ * @param text The text to display
147
+ * @param requestId A signed 32-bit identifier of the request
148
+ * @param options Options object for further requirements
149
+ */
150
+ static requestChat(text, requestId, options = {
151
+ chat_is_channel: false,
152
+ }) {
153
+ return { text, request_chat: { request_id: requestId, ...options } };
154
+ }
155
+ /**
156
+ * Adds a new contact request button. The user's phone number will be sent
157
+ * as a contact when the button is pressed. Available in private chats only.
158
+ *
159
+ * @param text The text to display
160
+ */
161
+ requestContact(text) {
162
+ return this.add(Keyboard.requestContact(text));
163
+ }
164
+ /**
165
+ * Creates a new contact request button. The user's phone number will be
166
+ * sent as a contact when the button is pressed. Available in private chats
167
+ * only.
168
+ *
169
+ * @param text The text to display
170
+ */
171
+ static requestContact(text) {
172
+ return { text, request_contact: true };
173
+ }
174
+ /**
175
+ * Adds a new location request button. The user's current location will be
176
+ * sent when the button is pressed. Available in private chats only.
177
+ *
178
+ * @param text The text to display
179
+ */
180
+ requestLocation(text) {
181
+ return this.add(Keyboard.requestLocation(text));
182
+ }
183
+ /**
184
+ * Creates a new location request button. The user's current location will
185
+ * be sent when the button is pressed. Available in private chats only.
186
+ *
187
+ * @param text The text to display
188
+ */
189
+ static requestLocation(text) {
190
+ return { text, request_location: true };
191
+ }
192
+ /**
193
+ * Adds a new poll request button. The user will be asked to create a poll
194
+ * and send it to the bot when the button is pressed. Available in private
195
+ * chats only.
196
+ *
197
+ * @param text The text to display
198
+ * @param type The type of permitted polls to create, omit if the user may
199
+ * send a poll of any type
200
+ */
201
+ requestPoll(text, type) {
202
+ return this.add(Keyboard.requestPoll(text, type));
203
+ }
204
+ /**
205
+ * Creates a new poll request button. The user will be asked to create a
206
+ * poll and send it to the bot when the button is pressed. Available in
207
+ * private chats only.
208
+ *
209
+ * @param text The text to display
210
+ * @param type The type of permitted polls to create, omit if the user may
211
+ * send a poll of any type
212
+ */
213
+ static requestPoll(text, type) {
214
+ return { text, request_poll: { type } };
215
+ }
216
+ /**
217
+ * Adds a new web app button. The Web App that will be launched when the
218
+ * user presses the button. The Web App will be able to send a
219
+ * “web_app_data” service message. Available in private chats only.
220
+ *
221
+ * @param text The text to display
222
+ * @param url An HTTPS URL of a Web App to be opened with additional data
223
+ */
224
+ webApp(text, url) {
225
+ return this.add(Keyboard.webApp(text, url));
226
+ }
227
+ /**
228
+ * Creates a new web app button. The Web App that will be launched when the
229
+ * user presses the button. The Web App will be able to send a
230
+ * “web_app_data” service message. Available in private chats only.
231
+ *
232
+ * @param text The text to display
233
+ * @param url An HTTPS URL of a Web App to be opened with additional data
234
+ */
235
+ static webApp(text, url) {
236
+ return { text, web_app: { url } };
237
+ }
238
+ /**
239
+ * Make the current keyboard persistent. See
240
+ * https://grammy.dev/plugins/keyboard#persistent-keyboards for more
241
+ * details.
242
+ *
243
+ * Keyboards are not persistent by default, use this function to enable it
244
+ * (without any parameters or pass `true`). Pass `false` to force the
245
+ * keyboard to not persist.
246
+ *
247
+ * @param isEnabled `true` if the keyboard should persist, and `false` otherwise
248
+ */
249
+ persistent(isEnabled = true) {
250
+ this.is_persistent = isEnabled;
251
+ return this;
252
+ }
253
+ /**
254
+ * Make the current keyboard selective. See
255
+ * https://grammy.dev/plugins/keyboard#selectively-send-custom-keyboards
256
+ * for more details.
257
+ *
258
+ * Keyboards are non-selective by default, use this function to enable it
259
+ * (without any parameters or pass `true`). Pass `false` to force the
260
+ * keyboard to be non-selective.
261
+ *
262
+ * @param isEnabled `true` if the keyboard should be selective, and `false` otherwise
263
+ */
264
+ selected(isEnabled = true) {
265
+ this.selective = isEnabled;
266
+ return this;
267
+ }
268
+ /**
269
+ * Make the current keyboard one-time. See
270
+ * https://grammy.dev/plugins/keyboard#one-time-custom-keyboards for
271
+ * more details.
272
+ *
273
+ * Keyboards are non-one-time by default, use this function to enable it
274
+ * (without any parameters or pass `true`). Pass `false` to force the
275
+ * keyboard to be non-one-time.
276
+ *
277
+ * @param isEnabled `true` if the keyboard should be one-time, and `false` otherwise
278
+ */
279
+ oneTime(isEnabled = true) {
280
+ this.one_time_keyboard = isEnabled;
281
+ return this;
282
+ }
283
+ /**
284
+ * Make the current keyboard resized. See
285
+ * https://grammy.dev/plugins/keyboard#resize-custom-keyboard for more
286
+ * details.
287
+ *
288
+ * Keyboards are non-resized by default, use this function to enable it
289
+ * (without any parameters or pass `true`). Pass `false` to force the
290
+ * keyboard to be non-resized.
291
+ *
292
+ * @param isEnabled `true` if the keyboard should be resized, and `false` otherwise
293
+ */
294
+ resized(isEnabled = true) {
295
+ this.resize_keyboard = isEnabled;
296
+ return this;
297
+ }
298
+ /**
299
+ * Set the current keyboard's input field placeholder. See
300
+ * https://grammy.dev/plugins/keyboard#input-field-placeholder for more
301
+ * details.
302
+ *
303
+ * @param value The placeholder text
304
+ */
305
+ placeholder(value) {
306
+ this.input_field_placeholder = value;
307
+ return this;
308
+ }
309
+ /**
310
+ * Creates a new keyboard that contains the transposed grid of buttons of
311
+ * this keyboard. This means that the resulting keyboard has the rows and
312
+ * columns flipped.
313
+ *
314
+ * Note that buttons can only span multiple columns, but never multiple
315
+ * rows. This means that if the given arrays have different lengths, some
316
+ * buttons might flow up in the layout. In these cases, transposing a
317
+ * keyboard a second time will not undo the first transposition.
318
+ *
319
+ * Here are some examples.
320
+ *
321
+ * ```
322
+ * original transposed
323
+ * [ a ] ~> [ a ]
324
+ *
325
+ * [ a ]
326
+ * [a b c] ~> [ b ]
327
+ * [ c ]
328
+ *
329
+ * [ a b ] [a c e]
330
+ * [ c d ] ~> [ b d ]
331
+ * [ e ]
332
+ *
333
+ * [ a b ] [a c d]
334
+ * [ c ] ~> [ b e ]
335
+ * [d e f] [ f ]
336
+ * ```
337
+ */
338
+ toTransposed() {
339
+ const original = this.keyboard;
340
+ const transposed = transpose(original);
341
+ return this.clone(transposed);
342
+ }
343
+ /**
344
+ * Creates a new keyboard with the same buttons but reflowed into a given
345
+ * number of columns as if the buttons were text elements. Optionally, you
346
+ * can specify if the flow should make sure to fill up the last row.
347
+ *
348
+ * This method is idempotent, so calling it a second time will effectively
349
+ * clone this keyboard without reordering the buttons.
350
+ *
351
+ * Here are some examples.
352
+ *
353
+ * ```
354
+ * original flowed
355
+ * [ a ] ~> [ a ] (4 columns)
356
+ *
357
+ * [ a ]
358
+ * [a b c] ~> [ b ] (1 column)
359
+ * [ c ]
360
+ *
361
+ * [ a b ] [a b c]
362
+ * [ c d ] ~> [ d e ] (3 columns)
363
+ * [ e ]
364
+ *
365
+ * [ a b ] [abcde]
366
+ * [ c ] ~> [ f ] (5 columns)
367
+ * [d e f]
368
+ *
369
+ * [a b c] [ a ]
370
+ * [d e f] ~> [b c d] (3 colums, { fillLastRow: true })
371
+ * [g h i] [e f g]
372
+ * [ j ] [h i j]
373
+ * ```
374
+ *
375
+ * @param columns Maximum number of buttons per row
376
+ * @param options Optional flowing behavior
377
+ */
378
+ toFlowed(columns, options = {}) {
379
+ const original = this.keyboard;
380
+ const flowed = reflow(original, columns, options);
381
+ return this.clone(flowed);
382
+ }
383
+ /**
384
+ * Creates and returns a deep copy of this keyboard.
385
+ *
386
+ * Optionally takes a new grid of buttons to replace the current buttons. If
387
+ * specified, only the options will be cloned, and the given buttons will be
388
+ * used instead.
389
+ */
390
+ clone(keyboard = this.keyboard) {
391
+ const clone = new Keyboard(keyboard.map((row) => row.slice()));
392
+ clone.is_persistent = this.is_persistent;
393
+ clone.selective = this.selective;
394
+ clone.one_time_keyboard = this.one_time_keyboard;
395
+ clone.resize_keyboard = this.resize_keyboard;
396
+ clone.input_field_placeholder = this.input_field_placeholder;
397
+ return clone;
398
+ }
399
+ /**
400
+ * Appends the buttons of the given keyboards to this keyboard. If other
401
+ * options are specified in these keyboards, they will be ignored.
402
+ *
403
+ * @param sources A number of keyboards to append
404
+ */
405
+ append(...sources) {
406
+ for (const source of sources) {
407
+ const keyboard = Keyboard.from(source);
408
+ this.keyboard.push(...keyboard.keyboard.map((row) => row.slice()));
409
+ }
410
+ return this;
411
+ }
412
+ /**
413
+ * Returns the keyboard that was build. Note that it doesn't return
414
+ * `resize_keyboard` or other options that may be set. You don't usually
415
+ * need to call this method. It is no longer useful.
416
+ */
417
+ build() {
418
+ return this.keyboard;
419
+ }
420
+ /**
421
+ * Turns a two-dimensional keyboard button array into a keyboard instance.
422
+ * You can use the static button builder methods to create keyboard button
423
+ * objects.
424
+ *
425
+ * @param source A two-dimensional button array
426
+ */
427
+ static from(source) {
428
+ if (source instanceof Keyboard)
429
+ return source.clone();
430
+ function toButton(btn) {
431
+ return typeof btn === "string" ? Keyboard.text(btn) : btn;
432
+ }
433
+ return new Keyboard(source.map((row) => row.map(toButton)));
434
+ }
435
+ }
436
+ exports.Keyboard = Keyboard;
437
+ /**
438
+ * Use this class to simplify building an inline keyboard (something like this:
439
+ * https://core.telegram.org/bots/features#inline-keyboards).
440
+ *
441
+ * ```ts
442
+ * // Build an inline keyboard:
443
+ * const keyboard = new InlineKeyboard()
444
+ * .text('A').text('B', 'callback-data').row()
445
+ * .text('C').text('D').row()
446
+ * .url('Telegram', 'telegram.org')
447
+ *
448
+ * // Send the keyboard:
449
+ * await ctx.reply('Here is your inline keyboard!', {
450
+ * reply_markup: keyboard
451
+ * })
452
+ * ```
453
+ *
454
+ * If you already have some source data which you would like to turn into an
455
+ * inline button object, you can use the static equivalents which every inline
456
+ * button has. You can use them to create a two-dimensional inline button array.
457
+ * The resulting array can be turned into a keyboard instance.
458
+ *
459
+ * ```ts
460
+ * const button = InlineKeyboard.text('GO', 'go')
461
+ * const array = [[button]]
462
+ * const keyboard = InlineKeyboard.from(array)
463
+ * ```
464
+ *
465
+ * Be sure to to check the
466
+ * [documentation](https://grammy.dev/plugins/keyboard#inline-keyboards) on
467
+ * inline keyboards in grammY.
468
+ */
469
+ class InlineKeyboard {
470
+ /**
471
+ * Initialize a new `InlineKeyboard` with an optional two-dimensional array
472
+ * of `InlineKeyboardButton` objects. This is the nested array that holds
473
+ * the inline keyboard. It will be extended every time you call one of the
474
+ * provided methods.
475
+ *
476
+ * @param inline_keyboard An optional initial two-dimensional button array
477
+ */
478
+ constructor(inline_keyboard = [[]]) {
479
+ this.inline_keyboard = inline_keyboard;
480
+ }
481
+ /**
482
+ * Allows you to add your own `InlineKeyboardButton` objects if you already
483
+ * have them for some reason. You most likely want to call one of the other
484
+ * methods.
485
+ *
486
+ * @param buttons The buttons to add
487
+ */
488
+ add(...buttons) {
489
+ var _a;
490
+ (_a = this.inline_keyboard[this.inline_keyboard.length - 1]) === null || _a === void 0 ? void 0 : _a.push(...buttons);
491
+ return this;
492
+ }
493
+ /**
494
+ * Adds a 'line break'. Call this method to make sure that the next added
495
+ * buttons will be on a new row.
496
+ *
497
+ * You may pass a number of `InlineKeyboardButton` objects if you already
498
+ * have the instances for some reason. You most likely don't want to pass
499
+ * any arguments to `row`.
500
+ *
501
+ * @param buttons A number of buttons to add to the next row
502
+ */
503
+ row(...buttons) {
504
+ this.inline_keyboard.push(buttons);
505
+ return this;
506
+ }
507
+ /**
508
+ * Adds a new URL button. Telegram clients will open the provided URL when
509
+ * the button is pressed.
510
+ *
511
+ * @param text The text to display
512
+ * @param url HTTP or tg:// url to be opened when the button is pressed. Links tg://user?id=<user_id> can be used to mention a user by their ID without using a username, if this is allowed by their privacy settings.
513
+ */
514
+ url(text, url) {
515
+ return this.add(InlineKeyboard.url(text, url));
516
+ }
517
+ /**
518
+ * Creates a new URL button. Telegram clients will open the provided URL
519
+ * when the button is pressed.
520
+ *
521
+ * @param text The text to display
522
+ * @param url HTTP or tg:// url to be opened when the button is pressed. Links tg://user?id=<user_id> can be used to mention a user by their ID without using a username, if this is allowed by their privacy settings.
523
+ */
524
+ static url(text, url) {
525
+ return { text, url };
526
+ }
527
+ /**
528
+ * Adds a new callback query button. The button contains a text and a custom
529
+ * payload. This payload will be sent back to your bot when the button is
530
+ * pressed. If you omit the payload, the display text will be sent back to
531
+ * your bot.
532
+ *
533
+ * Your bot will receive an update every time a user presses any of the text
534
+ * buttons. You can listen to these updates like this:
535
+ * ```ts
536
+ * // Specific buttons:
537
+ * bot.callbackQuery('button-data', ctx => { ... })
538
+ * // Any button of any inline keyboard:
539
+ * bot.on('callback_query:data', ctx => { ... })
540
+ * ```
541
+ *
542
+ * @param text The text to display
543
+ * @param data The callback data to send back to your bot (default = text)
544
+ */
545
+ text(text, data = text) {
546
+ return this.add(InlineKeyboard.text(text, data));
547
+ }
548
+ /**
549
+ * Creates a new callback query button. The button contains a text and a
550
+ * custom payload. This payload will be sent back to your bot when the
551
+ * button is pressed. If you omit the payload, the display text will be sent
552
+ * back to your bot.
553
+ *
554
+ * Your bot will receive an update every time a user presses any of the text
555
+ * buttons. You can listen to these updates like this:
556
+ * ```ts
557
+ * // Specific buttons:
558
+ * bot.callbackQuery('button-data', ctx => { ... })
559
+ * // Any button of any inline keyboard:
560
+ * bot.on('callback_query:data', ctx => { ... })
561
+ * ```
562
+ *
563
+ * @param text The text to display
564
+ * @param data The callback data to send back to your bot (default = text)
565
+ */
566
+ static text(text, data = text) {
567
+ return { text, callback_data: data };
568
+ }
569
+ /**
570
+ * Adds a new web app button, confer https://core.telegram.org/bots/webapps
571
+ *
572
+ * @param text The text to display
573
+ * @param url An HTTPS URL of a Web App to be opened with additional data
574
+ */
575
+ webApp(text, url) {
576
+ return this.add(InlineKeyboard.webApp(text, url));
577
+ }
578
+ /**
579
+ * Creates a new web app button, confer https://core.telegram.org/bots/webapps
580
+ *
581
+ * @param text The text to display
582
+ * @param url An HTTPS URL of a Web App to be opened with additional data
583
+ */
584
+ static webApp(text, url) {
585
+ return { text, web_app: typeof url === "string" ? { url } : url };
586
+ }
587
+ /**
588
+ * Adds a new login button. This can be used as a replacement for the
589
+ * Telegram Login Widget. You must specify an HTTPS URL used to
590
+ * automatically authorize the user.
591
+ *
592
+ * @param text The text to display
593
+ * @param loginUrl The login URL as string or `LoginUrl` object
594
+ */
595
+ login(text, loginUrl) {
596
+ return this.add(InlineKeyboard.login(text, loginUrl));
597
+ }
598
+ /**
599
+ * Creates a new login button. This can be used as a replacement for the
600
+ * Telegram Login Widget. You must specify an HTTPS URL used to
601
+ * automatically authorize the user.
602
+ *
603
+ * @param text The text to display
604
+ * @param loginUrl The login URL as string or `LoginUrl` object
605
+ */
606
+ static login(text, loginUrl) {
607
+ return {
608
+ text,
609
+ login_url: typeof loginUrl === "string"
610
+ ? { url: loginUrl }
611
+ : loginUrl,
612
+ };
613
+ }
614
+ /**
615
+ * Adds a new inline query button. Telegram clients will let the user pick a
616
+ * chat when this button is pressed. This will start an inline query. The
617
+ * selected chat will be prefilled with the name of your bot. You may
618
+ * provide a text that is specified along with it.
619
+ *
620
+ * Your bot will in turn receive updates for inline queries. You can listen
621
+ * to inline query updates like this:
622
+ * ```ts
623
+ * bot.on('inline_query', ctx => { ... })
624
+ * ```
625
+ *
626
+ * @param text The text to display
627
+ * @param query The (optional) inline query string to prefill
628
+ */
629
+ switchInline(text, query = "") {
630
+ return this.add(InlineKeyboard.switchInline(text, query));
631
+ }
632
+ /**
633
+ * Creates a new inline query button. Telegram clients will let the user pick a
634
+ * chat when this button is pressed. This will start an inline query. The
635
+ * selected chat will be prefilled with the name of your bot. You may
636
+ * provide a text that is specified along with it.
637
+ *
638
+ * Your bot will in turn receive updates for inline queries. You can listen
639
+ * to inline query updates like this:
640
+ * ```ts
641
+ * bot.on('inline_query', ctx => { ... })
642
+ * ```
643
+ *
644
+ * @param text The text to display
645
+ * @param query The (optional) inline query string to prefill
646
+ */
647
+ static switchInline(text, query = "") {
648
+ return { text, switch_inline_query: query };
649
+ }
650
+ /**
651
+ * Adds a new inline query button that acts on the current chat. The
652
+ * selected chat will be prefilled with the name of your bot. You may
653
+ * provide a text that is specified along with it. This will start an inline
654
+ * query.
655
+ *
656
+ * Your bot will in turn receive updates for inline queries. You can listen
657
+ * to inline query updates like this:
658
+ * ```ts
659
+ * bot.on('inline_query', ctx => { ... })
660
+ * ```
661
+ *
662
+ * @param text The text to display
663
+ * @param query The (optional) inline query string to prefill
664
+ */
665
+ switchInlineCurrent(text, query = "") {
666
+ return this.add(InlineKeyboard.switchInlineCurrent(text, query));
667
+ }
668
+ /**
669
+ * Creates a new inline query button that acts on the current chat. The
670
+ * selected chat will be prefilled with the name of your bot. You may
671
+ * provide a text that is specified along with it. This will start an inline
672
+ * query.
673
+ *
674
+ * Your bot will in turn receive updates for inline queries. You can listen
675
+ * to inline query updates like this:
676
+ * ```ts
677
+ * bot.on('inline_query', ctx => { ... })
678
+ * ```
679
+ *
680
+ * @param text The text to display
681
+ * @param query The (optional) inline query string to prefill
682
+ */
683
+ static switchInlineCurrent(text, query = "") {
684
+ return { text, switch_inline_query_current_chat: query };
685
+ }
686
+ /**
687
+ * Adds a new inline query button. Telegram clients will let the user pick a
688
+ * chat when this button is pressed. This will start an inline query. The
689
+ * selected chat will be prefilled with the name of your bot. You may
690
+ * provide a text that is specified along with it.
691
+ *
692
+ * Your bot will in turn receive updates for inline queries. You can listen
693
+ * to inline query updates like this:
694
+ * ```ts
695
+ * bot.on('inline_query', ctx => { ... })
696
+ * ```
697
+ *
698
+ * @param text The text to display
699
+ * @param query The query object describing which chats can be picked
700
+ */
701
+ switchInlineChosen(text, query = {}) {
702
+ return this.add(InlineKeyboard.switchInlineChosen(text, query));
703
+ }
704
+ /**
705
+ * Creates a new inline query button. Telegram clients will let the user pick a
706
+ * chat when this button is pressed. This will start an inline query. The
707
+ * selected chat will be prefilled with the name of your bot. You may
708
+ * provide a text that is specified along with it.
709
+ *
710
+ * Your bot will in turn receive updates for inline queries. You can listen
711
+ * to inline query updates like this:
712
+ * ```ts
713
+ * bot.on('inline_query', ctx => { ... })
714
+ * ```
715
+ *
716
+ * @param text The text to display
717
+ * @param query The query object describing which chats can be picked
718
+ */
719
+ static switchInlineChosen(text, query = {}) {
720
+ return { text, switch_inline_query_chosen_chat: query };
721
+ }
722
+ /**
723
+ * Adds a new copy text button. When clicked, the specified text will be
724
+ * copied to the clipboard.
725
+ *
726
+ * @param text The text to display
727
+ * @param copyText The text to be copied to the clipboard
728
+ */
729
+ copyText(text, copyText) {
730
+ return this.add(InlineKeyboard.copyText(text, copyText));
731
+ }
732
+ /**
733
+ * Creates a new copy text button. When clicked, the specified text will be
734
+ * copied to the clipboard.
735
+ *
736
+ * @param text The text to display
737
+ * @param copyText The text to be copied to the clipboard
738
+ */
739
+ static copyText(text, copyText) {
740
+ return {
741
+ text,
742
+ copy_text: typeof copyText === "string"
743
+ ? { text: copyText }
744
+ : copyText,
745
+ };
746
+ }
747
+ /**
748
+ * Adds a new game query button, confer
749
+ * https://core.telegram.org/bots/api#games
750
+ *
751
+ * This type of button must always be the first button in the first row.
752
+ *
753
+ * @param text The text to display
754
+ */
755
+ game(text) {
756
+ return this.add(InlineKeyboard.game(text));
757
+ }
758
+ /**
759
+ * Creates a new game query button, confer
760
+ * https://core.telegram.org/bots/api#games
761
+ *
762
+ * This type of button must always be the first button in the first row.
763
+ *
764
+ * @param text The text to display
765
+ */
766
+ static game(text) {
767
+ return { text, callback_game: {} };
768
+ }
769
+ /**
770
+ * Adds a new payment button, confer
771
+ * https://core.telegram.org/bots/api#payments
772
+ *
773
+ * This type of button must always be the first button in the first row and
774
+ * can only be used in invoice messages.
775
+ *
776
+ * @param text The text to display. Substrings “⭐” and “XTR” in the buttons's text will be replaced with a Telegram Star icon.
777
+ */
778
+ pay(text) {
779
+ return this.add(InlineKeyboard.pay(text));
780
+ }
781
+ /**
782
+ * Create a new payment button, confer
783
+ * https://core.telegram.org/bots/api#payments
784
+ *
785
+ * This type of button must always be the first button in the first row and
786
+ * can only be used in invoice messages.
787
+ *
788
+ * @param text The text to display. Substrings “⭐” and “XTR” in the buttons's text will be replaced with a Telegram Star icon.
789
+ */
790
+ static pay(text) {
791
+ return { text, pay: true };
792
+ }
793
+ /**
794
+ * Creates a new inline keyboard that contains the transposed grid of
795
+ * buttons of this inline keyboard. This means that the resulting inline
796
+ * keyboard has the rows and columns flipped.
797
+ *
798
+ * Note that inline buttons can only span multiple columns, but never
799
+ * multiple rows. This means that if the given arrays have different
800
+ * lengths, some buttons might flow up in the layout. In these cases,
801
+ * transposing an inline keyboard a second time will not undo the first
802
+ * transposition.
803
+ *
804
+ * Here are some examples.
805
+ *
806
+ * ```
807
+ * original transposed
808
+ * [ a ] ~> [ a ]
809
+ *
810
+ * [ a ]
811
+ * [a b c] ~> [ b ]
812
+ * [ c ]
813
+ *
814
+ * [ a b ] [a c e]
815
+ * [ c d ] ~> [ b d ]
816
+ * [ e ]
817
+ *
818
+ * [ a b ] [a c d]
819
+ * [ c ] ~> [ b e ]
820
+ * [d e f] [ f ]
821
+ * ```
822
+ */
823
+ toTransposed() {
824
+ const original = this.inline_keyboard;
825
+ const transposed = transpose(original);
826
+ return new InlineKeyboard(transposed);
827
+ }
828
+ /**
829
+ * Creates a new inline keyboard with the same buttons but reflowed into a
830
+ * given number of columns as if the buttons were text elements. Optionally,
831
+ * you can specify if the flow should make sure to fill up the last row.
832
+ *
833
+ * This method is idempotent, so calling it a second time will effectively
834
+ * clone this inline keyboard without reordering the buttons.
835
+ *
836
+ * Here are some examples.
837
+ *
838
+ * ```
839
+ * original flowed
840
+ * [ a ] ~> [ a ] (4 columns)
841
+ *
842
+ * [ a ]
843
+ * [a b c] ~> [ b ] (1 column)
844
+ * [ c ]
845
+ *
846
+ * [ a b ] [a b c]
847
+ * [ c d ] ~> [ d e ] (3 columns)
848
+ * [ e ]
849
+ *
850
+ * [ a b ] [abcde]
851
+ * [ c ] ~> [ f ] (5 columns)
852
+ * [d e f]
853
+ *
854
+ * [a b c] [ a ]
855
+ * [d e f] ~> [b c d] (3 colums, { fillLastRow: true })
856
+ * [g h i] [e f g]
857
+ * [ j ] [h i j]
858
+ * ```
859
+ *
860
+ * @param columns Maximum number of buttons per row
861
+ * @param options Optional flowing behavior
862
+ */
863
+ toFlowed(columns, options = {}) {
864
+ const original = this.inline_keyboard;
865
+ const flowed = reflow(original, columns, options);
866
+ return new InlineKeyboard(flowed);
867
+ }
868
+ /**
869
+ * Creates and returns a deep copy of this inline keyboard.
870
+ */
871
+ clone() {
872
+ return new InlineKeyboard(this.inline_keyboard.map((row) => row.slice()));
873
+ }
874
+ /**
875
+ * Appends the buttons of the given inline keyboards to this keyboard.
876
+ *
877
+ * @param sources A number of inline keyboards to append
878
+ */
879
+ append(...sources) {
880
+ for (const source of sources) {
881
+ const keyboard = InlineKeyboard.from(source);
882
+ this.inline_keyboard.push(...keyboard.inline_keyboard.map((row) => row.slice()));
883
+ }
884
+ return this;
885
+ }
886
+ /**
887
+ * Turns a two-dimensional inline button array into an inline keyboard
888
+ * instance. You can use the static button builder methods to create inline
889
+ * button objects.
890
+ *
891
+ * @param source A two-dimensional inline button array
892
+ */
893
+ static from(source) {
894
+ if (source instanceof InlineKeyboard)
895
+ return source.clone();
896
+ return new InlineKeyboard(source.map((row) => row.slice()));
897
+ }
898
+ }
899
+ exports.InlineKeyboard = InlineKeyboard;
900
+ function transpose(grid) {
901
+ var _a;
902
+ const transposed = [];
903
+ for (let i = 0; i < grid.length; i++) {
904
+ const row = grid[i];
905
+ for (let j = 0; j < row.length; j++) {
906
+ const button = row[j];
907
+ ((_a = transposed[j]) !== null && _a !== void 0 ? _a : (transposed[j] = [])).push(button);
908
+ }
909
+ }
910
+ return transposed;
911
+ }
912
+ function reflow(grid, columns, { fillLastRow = false }) {
913
+ var _a;
914
+ let first = columns;
915
+ if (fillLastRow) {
916
+ const buttonCount = grid
917
+ .map((row) => row.length)
918
+ .reduce((a, b) => a + b, 0);
919
+ first = buttonCount % columns;
920
+ }
921
+ const reflowed = [];
922
+ for (const row of grid) {
923
+ for (const button of row) {
924
+ const at = Math.max(0, reflowed.length - 1);
925
+ const max = at === 0 ? first : columns;
926
+ let next = ((_a = reflowed[at]) !== null && _a !== void 0 ? _a : (reflowed[at] = []));
927
+ if (next.length === max) {
928
+ next = [];
929
+ reflowed.push(next);
930
+ }
931
+ next.push(button);
932
+ }
933
+ }
934
+ return reflowed;
935
+ }