Skip to main content
Module

x/grammy_menu/menu.ts>Menu

Easily create interactive menus with grammY.
Go to Latest
class Menu
import { Menu } from "https://deno.land/x/grammy_menu@v1.1.2/menu.ts";

A menu is a set of interactive buttons that is displayed beneath a message. It uses an inline keyboard for that, so in a sense, a menu is just an inline keyboard spiced up with interactivity (such as navigation between multiple pages).

// Creating a simple menu
const menu = new Menu('my-menu-identifier')
  .text('A', ctx => ctx.reply('You pressed A!')).row()
  .text('B', ctx => ctx.reply('You pressed B!'))

// Make it interactive
bot.use(menu)

bot.command('start', async ctx => {
  // Send the menu:
  await ctx.reply('Check out this menu:', {
    reply_markup: menu
  })
})

Sending the menu is not directly possible via bot.api.sendMessage, only via the context object, at least not yet.

Check out the official documentation to see how you can create menus that span several pages, how to navigate between them, and more.

Constructors

new
Menu(id: string, options?: MenuOptions<C>)

Creates a new menu with the given identifier.

Check out the official documentation to see how you can create menus that span several pages, how to navigate between them, and more.

Type Parameters

optional
C extends Context = Context

Properties

private
index: Map<string, Menu<C>>
private
readonly
options: Required<MenuOptions<C> & { onMenuOutdated: string | false | MenuMiddleware<C>; }>
private
parent: string | undefined
readonly
inline_keyboard: Proxy

Used internally by the menu, do not touch or you'll burn yourself.

Methods

private
freeze()

Prevents this menu from being modified further in the future.

private
makeNavInstaller<C extends Context>(menu: Menu<C>): Middleware<C>
private
prepare(payload: Record<string, unknown>, ctx: C)

Replaces known menu instances in the payload by their rendered versions. A menu is known if it is contained in this menu's index. Only the reply_markup property of the given object is checked for containing a menu.

private
render(ctx: C)

Renders the menu to a static object of inline keyboard buttons by concatenating all ranges, and applying the given context object to all functions.

at(id: string)

Returns the menu instance for the given identifier. If the identifier is the same as this menu's identifier, this is returned.

register(menus: Menu<C> | Menu<C>[], parent?)

Registers a submenu. This makes it accessible for navigation, and sets its parent menu to this menu.

Optionally, you can specify the identifier of a different parent menu as a second argument. The parent menu is the menu that is targeted when backwards navigation is performed.

Note that once you registered a submenu, it is sufficient to call bot.use(menu) for the parent menu only. You do not need to make all submenus interactive by passing them to bot.use.