import { Composer } from "https://deno.land/x/grammy@v1.30.1/mod.ts";
This is an advanced method of grammY.
Not to be confused with the router
plugin.
This method is an alternative to the router
plugin. It allows you to
branch between different middleware per context object. You can pass two
things to it:
- A routing function
- Different middleware identified by key
The routing function decides based on the context object which middleware to run. Each middleware is identified by a key, so the routing function simply returns the key of that middleware.
// Define different route handlers
const routeHandlers = {
evenUpdates: (ctx: Context) => { ... }
oddUpdates: (ctx: Context) => { ... }
}
// Decide for a context object which one to pick
const router = (ctx: Context) => ctx.update.update_id % 2 === 0
? 'evenUpdates'
: 'oddUpdates'
// Route it!
bot.route(router, routeHandlers)
Optionally, you can pass a third option that is used as fallback
middleware if your route function returns undefined
, or if the key
returned by your router has no middleware associated with it.
This method may need less setup than first instantiating a Router
, but
for more complex setups, having a Router
may be more readable.
Type Parameters
Parameters
The routing function to use
Handlers for every route
Optional fallback middleware if no route matches