InlineKeyboard
Use this class to simplify building an inline keyboard (something like this: https://
// 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
InlineKeyboard(inline_keyboard: InlineKeyboardButton[][]);
Initialize a new Inline
with an optional two-dimensional array of Inline
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
add(...buttons: InlineKeyboardButton[]);
Allows you to add your own Inline
objects if you already have them for some reason. You most likely want to call one of the other methods.
row
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 Inline
objects if you already have the instances for some reason. You most likely don’t want to pass any arguments to row
.
url
url(text: string, url: string);
Adds a new URL button. Telegram clients will open the provided URL when the button is pressed.
text
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 => { ... })
webApp
webApp(text: string, url: string);
Adds a new web app button, confer https://
login
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.
switchInline
switchInline(text: string, query: string);
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
switchInlineCurrent(text: string, query: string);
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 => { ... })
switchInlineChosen
switchInlineChosen(text: string, query: SwitchInlineQueryChosenChat);
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 => { ... })
game
game(text: string);
Adds a new game query button, confer https://
This type of button must always be the first button in the first row.
pay
pay(text: string);
Adds a new payment button, confer https://
This type of button must always be the first button in the first row and can only be used in invoice messages.
toTransposed
toTransposed();
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 ]
2
3
4
5
6
7
8
9
10
11
12
13
14
toFlowed
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]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
clone
clone();
Creates and returns a deep copy of this inline keyboard.
append
append(...sources: InlineKeyboardSource[]);
Appends the buttons of the given inline keyboards to this keyboard.
Static Methods
url
url(text: string, url: string): InlineKeyboardButton.UrlButton;
Creates a new URL button. Telegram clients will open the provided URL when the button is pressed.
text
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 => { ... })
webApp
webApp(text: string, url: string): InlineKeyboardButton.WebAppButton;
Creates a new web app button, confer https://
login
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.
switchInline
switchInline(text: string, query: string): 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 => { ... })
switchInlineCurrent
switchInlineCurrent(text: string, query: string): 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 => { ... })
switchInlineChosen
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 => { ... })
game
game(text: string): InlineKeyboardButton.GameButton;
Creates a new game query button, confer https://
This type of button must always be the first button in the first row.
pay
pay(text: string): InlineKeyboardButton.PayButton;
Create a new payment button, confer https://
This type of button must always be the first button in the first row and can only be used in invoice messages.
from
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.