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

x/grammy_menu/mod.ts>MenuRange#text

Interactive menus for grammY.
Go to Latest
method MenuRange.prototype.text
import { MenuRange } from "https://deno.land/x/grammy_menu@v1.2.2/mod.ts";

Adds a new text button. You may pass any number of listeners. They will be called when the button is pressed.

menu.text('Hit me!', ctx => ctx.reply('Ouch!'))

If you pass several listeners, make sure that you understand what middleware is.

You can also use this method to register a button that depends on the current context.

function greetInstruction(ctx: Context): string {
  const username = ctx.from?.first_name
  return `Greet ${username ?? 'me'}!`,
}

const menu = new Menu('my-menu')
  .text(greetInstruction, ctx => ctx.reply("I'm too shy."))
bot.use(menu)

// This will send a menu with one text button, and the text has the name
// of the user that the bot is replying to.
bot.on('message', ctx => ctx.reply('What shall I do?', { reply_markup: menu }))

If you base the text on session data, you can easily create a settings panel with toggle buttons.

// Button will toggle between 'Yes' and 'No' when pressed
menu.text(ctx => ctx.session.flag ? 'Yes' : 'No', async ctx => {
  ctx.session.flag = !ctx.session.flag
  await ctx.menu.update()
})

Parameters

text: MaybeDynamicString<C>

The text to display, or a text with payload

...middleware: MenuMiddleware<C>[]

The listeners to call when the button is pressed

Parameters

text: TextAndPayload<C>
...middleware: MenuMiddleware<C & { match: string; }>[]

Parameters

text: MaybePayloadString<C>
...middleware: MenuMiddleware<C>[]