Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/grammy/composer.ts>Composer#route

The Telegram Bot Framework.
Very Popular
Latest
method Composer.prototype.route
import { Composer } from "https://deno.land/x/grammy@v1.30.1/composer.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:

  1. A routing function
  2. 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

R extends Record<PropertyKey, Middleware<C>>

Parameters

router: (ctx: C) => MaybePromise<undefined | keyof R>

The routing function to use

routeHandlers: R

Handlers for every route

optional
fallback: Middleware<C> = [UNSUPPORTED]

Optional fallback middleware if no route matches