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

x/grammy/mod.ts>InlineKeyboard

The Telegram Bot Framework.
Very Popular
Latest
class InlineKeyboard
import { InlineKeyboard } from "https://deno.land/x/grammy@v1.31.0/mod.ts";

Use this class to simplify building an inline keyboard (something like this: https://core.telegram.org/bots/features#inline-keyboards).

// Build an inline keyboard:
const keyboard = new InlineKeyboard()
  .text('A').text('B', 'callback-data').row()
  .text('C').text('D').row()
  .url('Telegram', 'telegram.org')

// Send the keyboard:
await ctx.reply('Here is your inline keyboard!', {
  reply_markup: keyboard
})

If you already have some source data which you would like to turn into an inline button object, you can use the static equivalents which every inline button has. You can use them to create a two-dimensional inline button array. The resulting array can be turned into a keyboard instance.

const button = InlineKeyboard.text('GO', 'go')
const array = [[button]]
const keyboard = InlineKeyboard.from(array)

Be sure to to check the documentation on inline keyboards in grammY.

Constructors

new
InlineKeyboard(inline_keyboard?: InlineKeyboardButton[][])

Initialize a new InlineKeyboard with an optional two-dimensional array of InlineKeyboardButton objects. This is the nested array that holds the inline keyboard. It will be extended every time you call one of the provided methods.

Methods

add(...buttons: InlineKeyboardButton[])

Allows you to add your own InlineKeyboardButton objects if you already have them for some reason. You most likely want to call one of the other methods.

append(...sources: InlineKeyboardSource[])

Appends the buttons of the given inline keyboards to this keyboard.

Creates and returns a deep copy of this inline keyboard.

copyText(text: string, copyText: string | CopyTextButton)

Adds a new copy text button. When clicked, the specified text will be copied to the clipboard.

game(text: string)

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.

login(text: string, loginUrl: string | LoginUrl)

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.

pay(text: string)

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.

row(...buttons: InlineKeyboardButton[])

Adds a 'line break'. Call this method to make sure that the next added buttons will be on a new row.

You may pass a number of InlineKeyboardButton objects if you already have the instances for some reason. You most likely don't want to pass any arguments to row.

switchInline(text: string, query?)

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. 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 => { ... })
switchInlineCurrent(text: string, query?)

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:

bot.on('inline_query', ctx => { ... })
text(text: string, data?)

Adds a new callback query button. The button contains a text and a custom payload. This payload will be sent back to your bot when the button is pressed. If you omit the payload, the display text will be sent back to your bot.

Your bot will receive an update every time a user presses any of the text buttons. You can listen to these updates like this:

// Specific buttons:
bot.callbackQuery('button-data', ctx => { ... })
// Any button of any inline keyboard:
bot.on('callback_query:data',    ctx => { ... })
toFlowed(columns: number, options?: FlowOptions)

Creates a new inline keyboard with the same buttons but reflowed into a given number of columns as if the buttons were text elements. Optionally, you can specify if the flow should make sure to fill up the last row.

This method is idempotent, so calling it a second time will effectively clone this inline keyboard without reordering the buttons.

Here are some examples.

original    flowed
[  a  ]  ~> [  a  ]    (4 columns)

            [  a  ]
[a b c]  ~> [  b  ]    (1 column)
            [  c  ]

[ a b ]     [a b c]
[ c d ]  ~> [ d e ]    (3 columns)
[  e  ]

[ a b ]     [abcde]
[  c  ]  ~> [  f  ]    (5 columns)
[d e f]

[a b c]     [  a  ]
[d e f]  ~> [b c d]    (3 colums, { fillLastRow: true })
[g h i]     [e f g]
[  j  ]     [h i j]

Creates a new inline keyboard that contains the transposed grid of buttons of this inline keyboard. This means that the resulting inline keyboard has the rows and columns flipped.

Note that inline buttons can only span multiple columns, but never multiple rows. This means that if the given arrays have different lengths, some buttons might flow up in the layout. In these cases, transposing an inline keyboard a second time will not undo the first transposition.

Here are some examples.

original    transposed
[  a  ]  ~> [  a  ]

            [  a  ]
[a b c]  ~> [  b  ]
            [  c  ]

[ a b ]     [a c e]
[ c d ]  ~> [ b d ]
[  e  ]

[ a b ]     [a c d]
[  c  ]  ~> [ b e ]
[d e f]     [  f  ]
url(text: string, url: string)

Adds a new URL button. Telegram clients will open the provided URL when the button is pressed.

webApp(text: string, url: string | WebAppInfo)

Adds a new web app button, confer https://core.telegram.org/bots/webapps

Static Methods

copyText(text: string, copyText: string | CopyTextButton): InlineKeyboardButton.CopyTextButtonButton

Creates a new copy text button. When clicked, the specified text will be copied to the clipboard.

from(source: InlineKeyboardSource): InlineKeyboard

Turns a two-dimensional inline button array into an inline keyboard instance. You can use the static button builder methods to create inline button objects.

game(text: string): InlineKeyboardButton.GameButton

Creates 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.

login(text: string, loginUrl: string | LoginUrl): InlineKeyboardButton.LoginButton

Creates 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.

pay(text: string): InlineKeyboardButton.PayButton

Create 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.

switchInline(text: string, query?): InlineKeyboardButton.SwitchInlineButton

Creates 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 => { ... })
switchInlineChosen(text: string, query?: SwitchInlineQueryChosenChat): InlineKeyboardButton.SwitchInlineChosenChatButton

Creates 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 => { ... })
switchInlineCurrent(text: string, query?): InlineKeyboardButton.SwitchInlineCurrentChatButton

Creates 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:

bot.on('inline_query', ctx => { ... })
text(text: string, data?): InlineKeyboardButton.CallbackButton

Creates a new callback query button. The button contains a text and a custom payload. This payload will be sent back to your bot when the button is pressed. If you omit the payload, the display text will be sent back to your bot.

Your bot will receive an update every time a user presses any of the text buttons. You can listen to these updates like this:

// Specific buttons:
bot.callbackQuery('button-data', ctx => { ... })
// Any button of any inline keyboard:
bot.on('callback_query:data',    ctx => { ... })
url(text: string, url: string): InlineKeyboardButton.UrlButton

Creates a new URL button. Telegram clients will open the provided URL when the button is pressed.

webApp(text: string, url: string | WebAppInfo): InlineKeyboardButton.WebAppButton

Creates a new web app button, confer https://core.telegram.org/bots/webapps