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,764 @@
1
+ import { type CallbackQueryContext, type ChatTypeContext, type ChosenInlineResultContext, type CommandContext, Context, type GameQueryContext, type HearsContext, type InlineQueryContext, type MaybeArray, type PreCheckoutQueryContext, type ReactionContext, type ShippingQueryContext, type StringWithCommandSuggestions } from "./context.js";
2
+ import { type Filter, type FilterQuery } from "./filter.js";
3
+ import { type Chat, type ReactionType, type ReactionTypeEmoji } from "./types.js";
4
+ type MaybePromise<T> = T | Promise<T>;
5
+ /**
6
+ * A function of this type is passed as the second parameter to all middleware.
7
+ * Invoke it to call the downstream middleware and pass on the control flow.
8
+ *
9
+ * In other words, if your middleware is done handling the context object, and
10
+ * other middleware should take over, this function should be called and
11
+ * `await`ed.
12
+ *
13
+ * Once the `Promise` returned by this function resolves, the downstream
14
+ * middleware is done executing, hence returning the control.
15
+ */
16
+ export type NextFunction = () => Promise<void>;
17
+ /**
18
+ * Middleware in the form of a function.
19
+ */
20
+ export type MiddlewareFn<C extends Context = Context> = (ctx: C, next: NextFunction) => MaybePromise<unknown>;
21
+ /**
22
+ * Middleware in the form of a container for a function.
23
+ */
24
+ export interface MiddlewareObj<C extends Context = Context> {
25
+ /**
26
+ * Returns the contained middleware.
27
+ */
28
+ middleware: () => MiddlewareFn<C>;
29
+ }
30
+ /**
31
+ * Middleware for grammY, either as a function or as a container for a function.
32
+ *
33
+ * Simply put, middleware is just a fancy term for a _listener_. You can
34
+ * register middleware on a bot to listen for updates. Example:
35
+ *
36
+ * ```ts
37
+ * bot.on('message', ctx => ctx.reply('I got your message!'))
38
+ * // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39
+ * // ^
40
+ * // |
41
+ * // This is middleware!
42
+ * ```
43
+ *
44
+ * Middleware receives one object that we call the _context object_. This is
45
+ * another fancy term for a simple object that holds information about the
46
+ * update you're processing. For instance, the context object gives you access
47
+ * to the message that was sent to your bot (`ctx.message`), including the text
48
+ * (or photo or whatever message the user has sent). The context object is
49
+ * commonly named `ctx`.
50
+ *
51
+ * It also provides you with the `ctx.api` object that you also find on
52
+ * `bot.api`. As a result, you can call `ctx.api.sendMessage` instead of
53
+ * `bot.api.sendMessage`. This prevents you from having to pass around your
54
+ * `bot` instance all over your code.
55
+ *
56
+ * Most importantly, the context object gives you a handful of really useful
57
+ * shortcuts, such as a `reply` method (see above). This method is nothing else
58
+ * than a wrapper around `ctx.api.sendMessage`—but with some arguments
59
+ * pre-filled for you. As you can see above, you no longer have to specify a
60
+ * `chat_id` or anything; the context object knows which chat it belongs to, so
61
+ * when you call `reply`, the context will call `sendMessage` with the correct
62
+ * `chat_id`, namely the one for the same chat that the incoming message
63
+ * originates from. This makes it very convenient to reply to a message.
64
+ *
65
+ * Middleware is an extremely powerful concept and this short explanation only
66
+ * scratched the surface of what is possible with grammY. If you want to know
67
+ * more advanced things about middleware, check out the
68
+ * [documentation](https://grammy.dev/guide/middleware) on the website.
69
+ */
70
+ export type Middleware<C extends Context = Context> = MiddlewareFn<C> | MiddlewareObj<C>;
71
+ /**
72
+ * This error is thrown when middleware throws. It simply wraps the original
73
+ * error (accessible via the `error` property), but also provides access to the
74
+ * respective context object that was processed while the error occurred.
75
+ */
76
+ export declare class BotError<C extends Context = Context> extends Error {
77
+ readonly error: unknown;
78
+ readonly ctx: C;
79
+ constructor(error: unknown, ctx: C);
80
+ }
81
+ /**
82
+ * Runs some given middleware function with a given context object.
83
+ *
84
+ * @param middleware The middleware to run
85
+ * @param ctx The context to use
86
+ */
87
+ export declare function run<C extends Context>(middleware: MiddlewareFn<C>, ctx: C): Promise<void>;
88
+ /**
89
+ * The composer is the heart of the middleware system in grammY. It is also the
90
+ * superclass of `Bot`. Whenever you call `use` or `on` or some of the other
91
+ * methods on your bot, you are in fact using the underlying composer instance
92
+ * to register your middleware.
93
+ *
94
+ * If you're just getting started, you do not need to worry about what
95
+ * middleware is, or about how to use a composer.
96
+ *
97
+ * On the other hand, if you want to dig deeper into how grammY implements
98
+ * middleware, check out the
99
+ * [documentation](https://grammy.dev/advanced/middleware) on the website.
100
+ */
101
+ export declare class Composer<C extends Context> implements MiddlewareObj<C> {
102
+ private handler;
103
+ /**
104
+ * Constructs a new composer based on the provided middleware. If no
105
+ * middleware is given, the composer instance will simply make all context
106
+ * objects pass through without touching them.
107
+ *
108
+ * @param middleware The middleware to compose
109
+ */
110
+ constructor(...middleware: Array<Middleware<C>>);
111
+ middleware(): MiddlewareFn<C>;
112
+ /**
113
+ * Registers some middleware that receives all updates. It is installed by
114
+ * concatenating it to the end of all previously installed middleware.
115
+ *
116
+ * Often, this method is used to install middleware that behaves like a
117
+ * plugin, for example session middleware.
118
+ * ```ts
119
+ * bot.use(session())
120
+ * ```
121
+ *
122
+ * This method returns a new instance of composer. The returned instance can
123
+ * be further extended, and all changes will be regarded here. Confer the
124
+ * [documentation](https://grammy.dev/advanced/middleware) on the
125
+ * website if you want to know more about how the middleware system in
126
+ * grammY works, especially when it comes to chaining the method calls
127
+ * (`use( ... ).use( ... ).use( ... )`).
128
+ *
129
+ * @param middleware The middleware to register
130
+ */
131
+ use(...middleware: Array<Middleware<C>>): Composer<C>;
132
+ /**
133
+ * Registers some middleware that will only be executed for some specific
134
+ * updates, namely those matching the provided filter query. Filter queries
135
+ * are a concise way to specify which updates you are interested in.
136
+ *
137
+ * Here are some examples of valid filter queries:
138
+ * ```ts
139
+ * // All kinds of message updates
140
+ * bot.on('message', ctx => { ... })
141
+ *
142
+ * // Only text messages
143
+ * bot.on('message:text', ctx => { ... })
144
+ *
145
+ * // Only text messages with URL
146
+ * bot.on('message:entities:url', ctx => { ... })
147
+ *
148
+ * // Text messages and text channel posts
149
+ * bot.on(':text', ctx => { ... })
150
+ *
151
+ * // Messages with URL in text or caption (i.e. entities or caption entities)
152
+ * bot.on('message::url', ctx => { ... })
153
+ *
154
+ * // Messages or channel posts with URL in text or caption
155
+ * bot.on('::url', ctx => { ... })
156
+ * ```
157
+ *
158
+ * You can use autocomplete in VS Code to see all available filter queries.
159
+ * Check out the
160
+ * [documentation](https://grammy.dev/guide/filter-queries) on the
161
+ * website to learn more about filter queries in grammY.
162
+ *
163
+ * It is possible to pass multiple filter queries in an array, i.e.
164
+ * ```ts
165
+ * // Matches all text messages and edited text messages that contain a URL
166
+ * bot.on(['message:entities:url', 'edited_message:entities:url'], ctx => { ... })
167
+ * ```
168
+ *
169
+ * Your middleware will be executed if _any of the provided filter queries_
170
+ * matches (logical OR).
171
+ *
172
+ * If you instead want to match _all of the provided filter queries_
173
+ * (logical AND), you can chain the `.on` calls:
174
+ * ```ts
175
+ * // Matches all messages and channel posts that both a) contain a URL and b) are forwards
176
+ * bot.on('::url').on(':forward_origin', ctx => { ... })
177
+ * ```
178
+ *
179
+ * @param filter The filter query to use, may also be an array of queries
180
+ * @param middleware The middleware to register behind the given filter
181
+ */
182
+ on<Q extends FilterQuery>(filter: Q | Q[], ...middleware: Array<Middleware<Filter<C, Q>>>): Composer<Filter<C, Q>>;
183
+ /**
184
+ * Registers some middleware that will only be executed when the message
185
+ * contains some text. Is it possible to pass a regular expression to match:
186
+ * ```ts
187
+ * // Match some text (exact match)
188
+ * bot.hears('I love grammY', ctx => ctx.reply('And grammY loves you! <3'))
189
+ * // Match a regular expression
190
+ * bot.hears(/\/echo (.+)/, ctx => ctx.reply(ctx.match[1]))
191
+ * ```
192
+ * Note how `ctx.match` will contain the result of the regular expression.
193
+ * Here it is a `RegExpMatchArray` object, so `ctx.match[1]` refers to the
194
+ * part of the regex that was matched by `(.+)`, i.e. the text that comes
195
+ * after “/echo”.
196
+ *
197
+ * You can pass an array of triggers. Your middleware will be executed if at
198
+ * least one of them matches.
199
+ *
200
+ * Both text and captions of the received messages will be scanned. For
201
+ * example, when a photo is sent to the chat and its caption matches the
202
+ * trigger, your middleware will be executed.
203
+ *
204
+ * If you only want to match text messages and not captions, you can do
205
+ * this:
206
+ * ```ts
207
+ * // Only matches text messages (and channel posts) for the regex
208
+ * bot.on(':text').hears(/\/echo (.+)/, ctx => { ... })
209
+ * ```
210
+ *
211
+ * @param trigger The text to look for
212
+ * @param middleware The middleware to register
213
+ */
214
+ hears(trigger: MaybeArray<string | RegExp>, ...middleware: Array<HearsMiddleware<C>>): Composer<HearsContext<C>>;
215
+ /**
216
+ * Registers some middleware that will only be executed when a certain
217
+ * command is found.
218
+ * ```ts
219
+ * // Reacts to /start commands
220
+ * bot.command('start', ctx => { ... })
221
+ * // Reacts to /help commands
222
+ * bot.command('help', ctx => { ... })
223
+ * ```
224
+ *
225
+ * The rest of the message (excluding the command, and trimmed) is provided
226
+ * via `ctx.match`.
227
+ *
228
+ * > **Did you know?** You can use deep linking
229
+ * > (https://core.telegram.org/bots/features#deep-linking) to let users
230
+ * > start your bot with a custom payload. As an example, send someone the
231
+ * > link https://t.me/name-of-your-bot?start=custom-payload and register a
232
+ * > start command handler on your bot with grammY. As soon as the user
233
+ * > starts your bot, you will receive `custom-payload` in the `ctx.match`
234
+ * > property!
235
+ * > ```ts
236
+ * > bot.command('start', ctx => {
237
+ * > const payload = ctx.match // will be 'custom-payload'
238
+ * > })
239
+ * > ```
240
+ *
241
+ * Note that commands are not matched in captions or in the middle of the
242
+ * text.
243
+ * ```ts
244
+ * bot.command('start', ctx => { ... })
245
+ * // ... does not match:
246
+ * // A message saying: “some text /start some more text”
247
+ * // A photo message with the caption “/start”
248
+ * ```
249
+ *
250
+ * By default, commands are detected in channel posts, too. This means that
251
+ * `ctx.message` is potentially `undefined`, so you should use `ctx.msg`
252
+ * instead to grab both messages and channel posts. Alternatively, if you
253
+ * want to limit your bot to finding commands only in private and group
254
+ * chats, you can use `bot.on('message').command('start', ctx => { ... })`,
255
+ * or even store a message-only version of your bot in a variable like so:
256
+ * ```ts
257
+ * const m = bot.on('message')
258
+ *
259
+ * m.command('start', ctx => { ... })
260
+ * m.command('help', ctx => { ... })
261
+ * // etc
262
+ * ```
263
+ *
264
+ * If you need more freedom matching your commands, check out the `commands`
265
+ * plugin.
266
+ *
267
+ * @param command The command to look for
268
+ * @param middleware The middleware to register
269
+ */
270
+ command(command: MaybeArray<StringWithCommandSuggestions>, ...middleware: Array<CommandMiddleware<C>>): Composer<CommandContext<C>>;
271
+ /**
272
+ * Registers some middleware that will only be added when a new reaction of
273
+ * the given type is added to a message.
274
+ * ```ts
275
+ * // Reacts to new '👍' reactions
276
+ * bot.reaction('👍', ctx => { ... })
277
+ * // Reacts to new '👍' or '👎' reactions
278
+ * bot.reaction(['👍', '👎'], ctx => { ... })
279
+ * ```
280
+ *
281
+ * > Note that you have to enable `message_reaction` updates in
282
+ * `allowed_updates` if you want your bot to receive updates about message
283
+ * reactions.
284
+ *
285
+ * `bot.reaction` will trigger if:
286
+ * - a new emoji reaction is added to a message
287
+ * - a new custom emoji reaction is added a message
288
+ *
289
+ * `bot.reaction` will not trigger if:
290
+ * - a reaction is removed
291
+ * - an anonymous reaction count is updated, such as on channel posts
292
+ * - `message_reaction` updates are not enabled for your bot
293
+ *
294
+ * @param reaction The reaction to look for
295
+ * @param middleware The middleware to register
296
+ */
297
+ reaction(reaction: MaybeArray<ReactionTypeEmoji["emoji"] | ReactionType>, ...middleware: Array<ReactionMiddleware<C>>): Composer<ReactionContext<C>>;
298
+ /**
299
+ * Registers some middleware for certain chat types only. For example, you
300
+ * can use this method to only receive updates from private chats. The four
301
+ * chat types are `"channel"`, `"supergroup"`, `"group"`, and `"private"`.
302
+ * This is especially useful when combined with other filtering logic. For
303
+ * example, this is how can you respond to `/start` commands only from
304
+ * private chats:
305
+ * ```ts
306
+ * bot.chatType("private").command("start", ctx => { ... })
307
+ * ```
308
+ *
309
+ * Naturally, you can also use this method on its own.
310
+ * ```ts
311
+ * // Private chats only
312
+ * bot.chatType("private", ctx => { ... });
313
+ * // Channels only
314
+ * bot.chatType("channel", ctx => { ... });
315
+ * ```
316
+ *
317
+ * You can pass an array of chat types if you want your middleware to run
318
+ * for any of several provided chat types.
319
+ * ```ts
320
+ * // Groups and supergroups only
321
+ * bot.chatType(["group", "supergroup"], ctx => { ... });
322
+ * ```
323
+ * [Remember](https://grammy.dev/guide/context#shortcuts) also that you
324
+ * can access the chat type via `ctx.chat.type`.
325
+ *
326
+ * @param chatType The chat type
327
+ * @param middleware The middleware to register
328
+ */
329
+ chatType<T extends Chat["type"]>(chatType: MaybeArray<T>, ...middleware: Array<Middleware<ChatTypeContext<C, T>>>): Composer<ChatTypeContext<C, T>>;
330
+ /**
331
+ * Registers some middleware for callback queries, i.e. the updates that
332
+ * Telegram delivers to your bot when a user clicks an inline button (that
333
+ * is a button under a message).
334
+ *
335
+ * This method is essentially the same as calling
336
+ * ```ts
337
+ * bot.on('callback_query:data', ctx => { ... })
338
+ * ```
339
+ * but it also allows you to match the query data against a given text or
340
+ * regular expression.
341
+ *
342
+ * ```ts
343
+ * // Create an inline keyboard
344
+ * const keyboard = new InlineKeyboard().text('Go!', 'button-payload')
345
+ * // Send a message with the keyboard
346
+ * await bot.api.sendMessage(chat_id, 'Press a button!', {
347
+ * reply_markup: keyboard
348
+ * })
349
+ * // Listen to users pressing buttons with that specific payload
350
+ * bot.callbackQuery('button-payload', ctx => { ... })
351
+ *
352
+ * // Listen to users pressing any button your bot ever sent
353
+ * bot.on('callback_query:data', ctx => { ... })
354
+ * ```
355
+ *
356
+ * Always remember to call `answerCallbackQuery`—even if you don't perform
357
+ * any action: https://core.telegram.org/bots/api#answercallbackquery
358
+ * ```ts
359
+ * bot.on('callback_query:data', async ctx => {
360
+ * await ctx.answerCallbackQuery()
361
+ * })
362
+ * ```
363
+ *
364
+ * You can pass an array of triggers. Your middleware will be executed if at
365
+ * least one of them matches.
366
+ *
367
+ * @param trigger The string to look for in the payload
368
+ * @param middleware The middleware to register
369
+ */
370
+ callbackQuery(trigger: MaybeArray<string | RegExp>, ...middleware: Array<CallbackQueryMiddleware<C>>): Composer<CallbackQueryContext<C>>;
371
+ /**
372
+ * Registers some middleware for game queries, i.e. the updates that
373
+ * Telegram delivers to your bot when a user clicks an inline button for the
374
+ * HTML5 games platform on Telegram.
375
+ *
376
+ * This method is essentially the same as calling
377
+ * ```ts
378
+ * bot.on('callback_query:game_short_name', ctx => { ... })
379
+ * ```
380
+ * but it also allows you to match the query data against a given text or
381
+ * regular expression.
382
+ *
383
+ * You can pass an array of triggers. Your middleware will be executed if at
384
+ * least one of them matches.
385
+ *
386
+ * @param trigger The string to look for in the payload
387
+ * @param middleware The middleware to register
388
+ */
389
+ gameQuery(trigger: MaybeArray<string | RegExp>, ...middleware: Array<GameQueryMiddleware<C>>): Composer<GameQueryContext<C>>;
390
+ /**
391
+ * Registers middleware for inline queries. Telegram sends an inline query
392
+ * to your bot whenever a user types “@your_bot_name ...” into a text field
393
+ * in Telegram. You bot will then receive the entered search query and can
394
+ * respond with a number of results (text, images, etc) that the user can
395
+ * pick from to send a message _via_ your bot to the respective chat. Check
396
+ * out https://core.telegram.org/bots/inline to read more about inline bots.
397
+ *
398
+ * > Note that you have to enable inline mode for you bot by contacting
399
+ * > @BotFather first.
400
+ *
401
+ * ```ts
402
+ * // Listen for users typing “@your_bot_name query”
403
+ * bot.inlineQuery('query', async ctx => {
404
+ * // Answer the inline query, confer https://core.telegram.org/bots/api#answerinlinequery
405
+ * await ctx.answerInlineQuery( ... )
406
+ * })
407
+ * ```
408
+ *
409
+ * @param trigger The inline query text to match
410
+ * @param middleware The middleware to register
411
+ */
412
+ inlineQuery(trigger: MaybeArray<string | RegExp>, ...middleware: Array<InlineQueryMiddleware<C>>): Composer<InlineQueryContext<C>>;
413
+ /**
414
+ * Registers middleware for the ChosenInlineResult by the given id or ids.
415
+ * ChosenInlineResult represents a result of an inline query that was chosen
416
+ * by the user and sent to their chat partner. Check out
417
+ * https://core.telegram.org/bots/api#choseninlineresult to read more about
418
+ * chosen inline results.
419
+ *
420
+ * ```ts
421
+ * bot.chosenInlineResult('id', async ctx => {
422
+ * const id = ctx.result_id;
423
+ * // Your code
424
+ * })
425
+ * ```
426
+ *
427
+ * @param resultId An id or array of ids
428
+ * @param middleware The middleware to register
429
+ */
430
+ chosenInlineResult(resultId: MaybeArray<string | RegExp>, ...middleware: Array<ChosenInlineResultMiddleware<C>>): Composer<ChosenInlineResultContext<C>>;
431
+ /**
432
+ * Registers middleware for pre-checkout queries. Telegram sends a
433
+ * pre-checkout query to your bot whenever a user has confirmed their
434
+ * payment and shipping details. You bot will then receive all information
435
+ * about the order and has to respond within 10 seconds with a confirmation
436
+ * of whether everything is alright (goods are available, etc.) and the bot
437
+ * is ready to proceed with the order. Check out
438
+ * https://core.telegram.org/bots/api#precheckoutquery to read more about
439
+ * pre-checkout queries.
440
+ *
441
+ * ```ts
442
+ * bot.preCheckoutQuery('invoice_payload', async ctx => {
443
+ * // Answer the pre-checkout query, confer https://core.telegram.org/bots/api#answerprecheckoutquery
444
+ * await ctx.answerPreCheckoutQuery( ... )
445
+ * })
446
+ * ```
447
+ *
448
+ * @param trigger The string to look for in the invoice payload
449
+ * @param middleware The middleware to register
450
+ */
451
+ preCheckoutQuery(trigger: MaybeArray<string | RegExp>, ...middleware: Array<PreCheckoutQueryMiddleware<C>>): Composer<PreCheckoutQueryContext<C>>;
452
+ /**
453
+ * Registers middleware for shipping queries. If you sent an invoice
454
+ * requesting a shipping address and the parameter _is_flexible_ was
455
+ * specified, Telegram will send a shipping query to your bot whenever a
456
+ * user has confirmed their shipping details. You bot will then receive the
457
+ * shipping information and can respond with a confirmation of whether
458
+ * delivery to the specified address is possible. Check out
459
+ * https://core.telegram.org/bots/api#shippingquery to read more about
460
+ * shipping queries.
461
+ *
462
+ * ```ts
463
+ * bot.shippingQuery('invoice_payload', async ctx => {
464
+ * // Answer the shipping query, confer https://core.telegram.org/bots/api#answershippingquery
465
+ * await ctx.answerShippingQuery( ... )
466
+ * })
467
+ * ```
468
+ *
469
+ * @param trigger The string to look for in the invoice payload
470
+ * @param middleware The middleware to register
471
+ */
472
+ shippingQuery(trigger: MaybeArray<string | RegExp>, ...middleware: Array<ShippingQueryMiddleware<C>>): Composer<ShippingQueryContext<C>>;
473
+ /**
474
+ * > This is an advanced method of grammY.
475
+ *
476
+ * Registers middleware behind a custom filter function that operates on the
477
+ * context object and decides whether or not to execute the middleware. In
478
+ * other words, the middleware will only be executed if the given predicate
479
+ * returns `true` for the given context object. Otherwise, it will be
480
+ * skipped and the next middleware will be executed.
481
+ *
482
+ * This method has two signatures. The first one is straightforward, it is
483
+ * the one described above. Note that the predicate may be asynchronous,
484
+ * i.e. it can return a Promise of a boolean.
485
+ *
486
+ * Alternatively, you can pass a function that has a type predicate as
487
+ * return type. This will allow you to narrow down the context object. The
488
+ * installed middleware is then able to operate on this constrained context
489
+ * object.
490
+ * ```ts
491
+ * // NORMAL USAGE
492
+ * // Only process every second update
493
+ * bot.filter(ctx => ctx.update.update_id % 2 === 0, ctx => { ... })
494
+ *
495
+ * // TYPE PREDICATE USAGE
496
+ * function predicate(ctx): ctx is Context & { message: undefined } {
497
+ * return ctx.message === undefined
498
+ * }
499
+ * // Only process updates where `message` is `undefined`
500
+ * bot.filter(predicate, ctx => {
501
+ * const m = ctx.message // inferred as always undefined!
502
+ * const m2 = ctx.update.message // also inferred as always undefined!
503
+ * })
504
+ * ```
505
+ *
506
+ * @param predicate The predicate to check
507
+ * @param middleware The middleware to register
508
+ */
509
+ filter<D extends C>(predicate: (ctx: C) => ctx is D, ...middleware: Array<Middleware<D>>): Composer<D>;
510
+ filter(predicate: (ctx: C) => MaybePromise<boolean>, ...middleware: Array<Middleware<C>>): Composer<C>;
511
+ /**
512
+ * > This is an advanced method of grammY.
513
+ *
514
+ * Registers middleware behind a custom filter function that operates on the
515
+ * context object and decides whether or not to execute the middleware. In
516
+ * other words, the middleware will only be executed if the given predicate
517
+ * returns `false` for the given context object. Otherwise, it will be
518
+ * skipped and the next middleware will be executed. Note that the predicate
519
+ * may be asynchronous, i.e. it can return a Promise of a boolean.
520
+ *
521
+ * This method is the same using `filter` (normal usage) with a negated
522
+ * predicate.
523
+ *
524
+ * @param predicate The predicate to check
525
+ * @param middleware The middleware to register
526
+ */
527
+ drop(predicate: (ctx: C) => MaybePromise<boolean>, ...middleware: Array<Middleware<C>>): Composer<C>;
528
+ /**
529
+ * > This is an advanced method of grammY.
530
+ *
531
+ * Registers some middleware that runs concurrently to the executing
532
+ * middleware stack.
533
+ * ```ts
534
+ * bot.use( ... ) // will run first
535
+ * bot.fork( ... ) // will be started second, but run concurrently
536
+ * bot.use( ... ) // will also be run second
537
+ * ```
538
+ * In the first middleware, as soon as `next`'s Promise resolves, both forks
539
+ * have completed.
540
+ *
541
+ * Both the fork and the downstream middleware are awaited with
542
+ * `Promise.all`, so you will only be to catch up to one error (the one that
543
+ * is thrown first).
544
+ *
545
+ * In opposite to the other middleware methods on composer, `fork` does not
546
+ * return simply return the composer connected to the main middleware stack.
547
+ * Instead, it returns the created composer _of the fork_ connected to the
548
+ * middleware stack. This allows for the following pattern.
549
+ * ```ts
550
+ * // Middleware will be run concurrently!
551
+ * bot.fork().on('message', ctx => { ... })
552
+ * ```
553
+ *
554
+ * @param middleware The middleware to run concurrently
555
+ */
556
+ fork(...middleware: Array<Middleware<C>>): Composer<C>;
557
+ /**
558
+ * > This is an advanced method of grammY.
559
+ *
560
+ * Executes some middleware that can be generated on the fly for each
561
+ * context. Pass a factory function that creates some middleware (or a
562
+ * middleware array even). The factory function will be called once per
563
+ * context, and its result will be executed with the context object.
564
+ * ```ts
565
+ * // The middleware returned by `createMyMiddleware` will be used only once
566
+ * bot.lazy(ctx => createMyMiddleware(ctx))
567
+ * ```
568
+ *
569
+ * You may generate this middleware in an `async` fashion.
570
+ *
571
+ * You can decide to return an empty array (`[]`) if you don't want to run
572
+ * any middleware for a given context object. This is equivalent to
573
+ * returning an empty instance of `Composer`.
574
+ *
575
+ * @param middlewareFactory The factory function creating the middleware
576
+ */
577
+ lazy(middlewareFactory: (ctx: C) => MaybePromise<MaybeArray<Middleware<C>>>): Composer<C>;
578
+ /**
579
+ * > This is an advanced method of grammY.
580
+ *
581
+ * _Not to be confused with the `router` plugin._
582
+ *
583
+ * This method is an alternative to the `router` plugin. It allows you to
584
+ * branch between different middleware per context object. You can pass two
585
+ * things to it:
586
+ * 1. A routing function
587
+ * 2. Different middleware identified by key
588
+ *
589
+ * The routing function decides based on the context object which middleware
590
+ * to run. Each middleware is identified by a key, so the routing function
591
+ * simply returns the key of that middleware.
592
+ * ```ts
593
+ * // Define different route handlers
594
+ * const routeHandlers = {
595
+ * evenUpdates: (ctx: Context) => { ... }
596
+ * oddUpdates: (ctx: Context) => { ... }
597
+ * }
598
+ * // Decide for a context object which one to pick
599
+ * const router = (ctx: Context) => ctx.update.update_id % 2 === 0
600
+ * ? 'evenUpdates'
601
+ * : 'oddUpdates'
602
+ * // Route it!
603
+ * bot.route(router, routeHandlers)
604
+ * ```
605
+ *
606
+ * Optionally, you can pass a third option that is used as fallback
607
+ * middleware if your route function returns `undefined`, or if the key
608
+ * returned by your router has no middleware associated with it.
609
+ *
610
+ * This method may need less setup than first instantiating a `Router`, but
611
+ * for more complex setups, having a `Router` may be more readable.
612
+ *
613
+ * @param router The routing function to use
614
+ * @param routeHandlers Handlers for every route
615
+ * @param fallback Optional fallback middleware if no route matches
616
+ */
617
+ route<R extends Record<PropertyKey, Middleware<C>>>(router: (ctx: C) => MaybePromise<undefined | keyof R>, routeHandlers: R, fallback?: Middleware<C>): Composer<C>;
618
+ /**
619
+ * > This is an advanced method of grammY.
620
+ *
621
+ * Allows you to branch between two cases for a given context object.
622
+ *
623
+ * This method takes a predicate function that is tested once per context
624
+ * object. If it returns `true`, the first supplied middleware is executed.
625
+ * If it returns `false`, the second supplied middleware is executed. Note
626
+ * that the predicate may be asynchronous, i.e. it can return a Promise of a
627
+ * boolean.
628
+ *
629
+ * @param predicate The predicate to check
630
+ * @param trueMiddleware The middleware for the `true` case
631
+ * @param falseMiddleware The middleware for the `false` case
632
+ */
633
+ branch(predicate: (ctx: C) => MaybePromise<boolean>, trueMiddleware: MaybeArray<Middleware<C>>, falseMiddleware: MaybeArray<Middleware<C>>): Composer<C>;
634
+ /**
635
+ * > This is an advanced function of grammY.
636
+ *
637
+ * Installs an error boundary that catches errors that happen only inside
638
+ * the given middleware. This allows you to install custom error handlers
639
+ * that protect some parts of your bot. Errors will not be able to bubble
640
+ * out of this part of your middleware system, unless the supplied error
641
+ * handler rethrows them, in which case the next surrounding error boundary
642
+ * will catch the error.
643
+ *
644
+ * Example usage:
645
+ * ```ts
646
+ * function errHandler(err: BotError) {
647
+ * console.error('Error boundary caught error!', err)
648
+ * }
649
+ *
650
+ * const safe =
651
+ * // All passed middleware will be protected by the error boundary.
652
+ * bot.errorBoundary(errHandler, middleware0, middleware1, middleware2)
653
+ *
654
+ * // Those will also be protected!
655
+ * safe.on('message', middleware3)
656
+ *
657
+ * // No error from `middleware4` will reach the `errHandler` from above,
658
+ * // as errors are suppressed.
659
+ *
660
+ * // do nothing on error (suppress error), and run outside middleware
661
+ * const suppress = (_err: BotError, next: NextFunction) => { return next() }
662
+ * safe.errorBoundary(suppress).on('edited_message', middleware4)
663
+ * ```
664
+ *
665
+ * Check out the
666
+ * [documentation](https://grammy.dev/guide/errors#error-boundaries) on
667
+ * the website to learn more about error boundaries.
668
+ *
669
+ * @param errorHandler The error handler to use
670
+ * @param middleware The middleware to protect
671
+ */
672
+ errorBoundary(errorHandler: (error: BotError<C>, next: NextFunction) => MaybePromise<unknown>, ...middleware: Array<Middleware<C>>): Composer<C>;
673
+ }
674
+ /**
675
+ * Type of the middleware that can be passed to `bot.hears`.
676
+ *
677
+ * This helper type can be used to annotate middleware functions that are
678
+ * defined in one place, so that they have the correct type when passed to
679
+ * `bot.hears` in a different place. For instance, this allows for more modular
680
+ * code where handlers are defined in separate files.
681
+ */
682
+ export type HearsMiddleware<C extends Context> = Middleware<HearsContext<C>>;
683
+ /**
684
+ * Type of the middleware that can be passed to `bot.command`.
685
+ *
686
+ * This helper type can be used to annotate middleware functions that are
687
+ * defined in one place, so that they have the correct type when passed to
688
+ * `bot.command` in a different place. For instance, this allows for more
689
+ * modular code where handlers are defined in separate files.
690
+ */
691
+ export type CommandMiddleware<C extends Context> = Middleware<CommandContext<C>>;
692
+ /**
693
+ * Type of the middleware that can be passed to `bot.reaction`.
694
+ *
695
+ * This helper type can be used to annotate middleware functions that are
696
+ * defined in one place, so that they have the correct type when passed to
697
+ * `bot.reaction` in a different place. For instance, this allows for more
698
+ * modular code where handlers are defined in separate files.
699
+ */
700
+ export type ReactionMiddleware<C extends Context> = Middleware<ReactionContext<C>>;
701
+ /**
702
+ * Type of the middleware that can be passed to `bot.callbackQuery`.
703
+ *
704
+ * This helper type can be used to annotate middleware functions that are
705
+ * defined in one place, so that they have the correct type when passed to
706
+ * `bot.callbackQuery` in a different place. For instance, this allows for more
707
+ * modular code where handlers are defined in separate files.
708
+ */
709
+ export type CallbackQueryMiddleware<C extends Context> = Middleware<CallbackQueryContext<C>>;
710
+ /**
711
+ * Type of the middleware that can be passed to `bot.gameQuery`.
712
+ *
713
+ * This helper type can be used to annotate middleware functions that are
714
+ * defined in one place, so that they have the correct type when passed to
715
+ * `bot.gameQuery` in a different place. For instance, this allows for more
716
+ * modular code where handlers are defined in separate files.
717
+ */
718
+ export type GameQueryMiddleware<C extends Context> = Middleware<GameQueryContext<C>>;
719
+ /**
720
+ * Type of the middleware that can be passed to `bot.inlineQuery`.
721
+ *
722
+ * This helper type can be used to annotate middleware functions that are
723
+ * defined in one place, so that they have the correct type when passed to
724
+ * `bot.inlineQuery` in a different place. For instance, this allows for more
725
+ * modular code where handlers are defined in separate files.
726
+ */
727
+ export type InlineQueryMiddleware<C extends Context> = Middleware<InlineQueryContext<C>>;
728
+ /**
729
+ * Type of the middleware that can be passed to `bot.chosenInlineResult`.
730
+ *
731
+ * This helper type can be used to annotate middleware functions that are
732
+ * defined in one place, so that they have the correct type when passed to
733
+ * `bot.chosenInlineResult` in a different place. For instance, this allows for
734
+ * more modular code where handlers are defined in separate files.
735
+ */
736
+ export type ChosenInlineResultMiddleware<C extends Context> = Middleware<ChosenInlineResultContext<C>>;
737
+ /**
738
+ * Type of the middleware that can be passed to `bot.preCheckoutQuery`.
739
+ *
740
+ * This helper type can be used to annotate middleware functions that are
741
+ * defined in one place, so that they have the correct type when passed to
742
+ * `bot.preCheckoutQuery` in a different place. For instance, this allows for
743
+ * more modular code where handlers are defined in separate files.
744
+ */
745
+ export type PreCheckoutQueryMiddleware<C extends Context> = Middleware<PreCheckoutQueryContext<C>>;
746
+ /**
747
+ * Type of the middleware that can be passed to `bot.shippingQuery`.
748
+ *
749
+ * This helper type can be used to annotate middleware functions that are
750
+ * defined in one place, so that they have the correct type when passed to
751
+ * `bot.shippingQuery` in a different place. For instance, this allows for more
752
+ * modular code where handlers are defined in separate files.
753
+ */
754
+ export type ShippingQueryMiddleware<C extends Context> = Middleware<ShippingQueryContext<C>>;
755
+ /**
756
+ * Type of the middleware that can be passed to `bot.chatType`.
757
+ *
758
+ * This helper type can be used to annotate middleware functions that are
759
+ * defined in one place, so that they have the correct type when passed to
760
+ * `bot.chatType` in a different place. For instance, this allows for more
761
+ * modular code where handlers are defined in separate files.
762
+ */
763
+ export type ChatTypeMiddleware<C extends Context, T extends Chat["type"]> = Middleware<ChatTypeContext<C, T>>;
764
+ export {};