Composer class is the heart of grammY’s middleware system. It provides methods for composing and organizing middleware into a middleware stack. The Bot class extends Composer, so all methods are available on your bot instance.
Overview
A composer allows you to:- Register middleware that processes updates
- Filter updates based on various criteria
- Organize middleware into reusable components
- Control the flow of update processing
Constructor
Middleware<C>[]
Optional middleware functions to initialize the composer with. If no middleware is provided, the composer will pass all updates through unchanged.
Basic Methods
use
Registers middleware that receives all updates.Middleware<C>[]
required
The middleware function(s) to register
Composer<C>
A new composer instance that can be further extended
on
Registers middleware for specific update types using filter queries.FilterQuery | FilterQuery[]
required
The filter query or array of queries to match
Middleware<Filter<C, Q>>[]
required
Middleware to execute when the filter matches
hears
Registers middleware for messages matching text or regular expressions.string | RegExp | Array<string | RegExp>
required
The text or regex to match against message text or captions
command
Registers middleware for specific bot commands.string | string[]
required
The command(s) to match (without the leading
/)reaction
Registers middleware for message reaction updates.ReactionTypeEmoji['emoji'] | ReactionType | Array
required
The reaction emoji or type to match
chatType
Registers middleware for specific chat types.'private' | 'group' | 'supergroup' | 'channel' | Array
required
The chat type(s) to match
callbackQuery
Registers middleware for callback queries.inlineQuery
Registers middleware for inline queries.Advanced Methods
filter
Registers middleware behind a custom filter function.(ctx: C) => boolean | Promise<boolean>
required
Function that determines whether to execute the middleware
drop
Registers middleware that runs only if the predicate returnsfalse.
fork
Runs middleware concurrently with the main middleware stack.lazy
Executes dynamically generated middleware.(ctx: C) => Middleware<C> | Middleware<C>[]
required
Factory function that creates middleware for each context. Can be async. Return an empty array to skip middleware.
route
Branches between different middleware based on a routing function.(ctx: C) => keyof R | undefined
required
Function that returns the key of the middleware to execute
Record<string, Middleware<C>>
required
Object mapping route keys to middleware
Middleware<C>
Optional fallback middleware if no route matches
branch
Branches between two middleware paths based on a predicate.(ctx: C) => boolean | Promise<boolean>
required
Condition to test
Middleware<C> | Middleware<C>[]
required
Middleware to run if predicate returns
trueMiddleware<C> | Middleware<C>[]
required
Middleware to run if predicate returns
falseerrorBoundary
Installs an error boundary to catch errors in middleware.(error: BotError<C>, next: NextFunction) => unknown
required
Function to handle errors. Call
next() to continue to downstream middleware after handling the error.Middleware<C>[]
required
Middleware to protect with the error boundary
Helper Methods
gameQuery
Registers middleware for game queries.chosenInlineResult
Registers middleware for chosen inline results.preCheckoutQuery
Registers middleware for pre-checkout queries.shippingQuery
Registers middleware for shipping queries.Middleware Function
Get the composed middleware function.MiddlewareFn<C>
The composed middleware function
Complete Example
See Also
- Bot - The main Bot class that extends Composer
- Context - The context object passed to middleware
- Middleware - Understanding middleware in grammY
- Filter Queries - Filter query syntax reference