import { MenuRange } from "https://deno.land/x/grammy_menu@v1.2.2/mod.ts";
A menu range is a two-dimensional array of menu buttons.
This array is a part of the total two-dimensional array of menu buttons. This is mostly useful if you want to dynamically generate the structure of the menu on the fly.
Type Parameters
Properties
Internal list of range generator functions
Methods
This method is used internally whenever new buttons are added. Adds the buttons to the current row.
This method is used internally whenever a new range is added.
Appends a given range to this range. This will effectively replay all operations of the given range onto this range.
Adds a text button that performs a navigation to the parent menu via
ctx.menu.back()
.
This is a dynamic way to initialize menu. A typical use case is when you
want to create an arbitrary menu, using the data from ctx.session
:
const menu = new Menu('root')
menu.dynamic(ctx => ctx.session.data.reduce((range, entry) => range.text(entry)), new Menu.Builder())
bot.command("start", async (ctx) => {
await ctx.reply("Menu", {
reply_markup: menu,
});
});
Adds a new game query button, confer https://core.telegram.org/bots/api#games
This type of button must always be the first button in the first row.
Adds a new login button. This can be used as a replacement for the Telegram Login Widget. You must specify an HTTPS URL used to automatically authorize the user.
Adds a new payment button, confer https://core.telegram.org/bots/api#payments
This type of button must always be the first button in the first row and can only be used in invoice messages.
Adds a 'line break'. Call this method to make sure that the next added buttons will be on a new row.
Adds a new inline query button. Telegram clients will let the user pick a chat when this button is pressed. This will start an inline query. The selected chat will be prefilled with the name of your bot. You may provide a text that is specified along with it.
Your bot will in turn receive updates for inline queries. You can listen to inline query updates like this:
// Listen for specifc query
bot.inlineQuery('my-query', ctx => { ... })
// Listen for any query
bot.on('inline_query', ctx => { ... })
Adds a new inline query button. Telegram clients will let the user pick a chat when this button is pressed. This will start an inline query. The selected chat will be prefilled with the name of your bot. You may provide a text that is specified along with it.
Your bot will in turn receive updates for inline queries. You can listen to inline query updates like this:
bot.on('inline_query', ctx => { ... })
Adds a new inline query button that acts on the current chat. The selected chat will be prefilled with the name of your bot. You may provide a text that is specified along with it. This will start an inline query.
Your bot will in turn receive updates for inline queries. You can listen to inline query updates like this:
// Listen for specifc query
bot.inlineQuery('my-query', ctx => { ... })
// Listen for any query
bot.on('inline_query', ctx => { ... })
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()
})
Adds a new URL button. Telegram clients will open the provided URL when the button is pressed. Note that they will not notify your bot when that happens, so you cannot react to this button.
Adds a new web app button, confer https://core.telegram.org/bots/webapps