Skip to content

GramIO API Reference / @gramio/keyboards/dist / InlineKeyboard

Class: InlineKeyboard ​

Defined in: keyboards/index.d.ts:420

InlineKeyboardMarkup builder

This object represents an inline keyboard that appears right next to the message it belongs to.

[Documentation]

Extends ​

Constructors ​

Constructor ​

new InlineKeyboard(featureFlags?): InlineKeyboard

Defined in: keyboards/index.d.ts:48

Parameters ​

ParameterType
featureFlags?KeyboardFeatureFlags

Returns ​

InlineKeyboard

Inherited from ​

BaseKeyboardConstructor.constructor

Properties ​

PropertyModifierTypeInherited fromDefined in
currentRowprotectedTelegramInlineKeyboardButton[]BaseKeyboardConstructor.currentRowkeyboards/index.d.ts:46
featureFlagsprotectedKeyboardFeatureFlagsBaseKeyboardConstructor.featureFlagskeyboards/index.d.ts:47
rowsprotectedTelegramInlineKeyboardButton[][]BaseKeyboardConstructor.rowskeyboards/index.d.ts:45

Accessors ​

keyboard ​

Get Signature ​

get protected keyboard(): T[][]

Defined in: keyboards/index.d.ts:52

Returns ​

T[][]

Inherited from ​

BaseKeyboardConstructor.keyboard

Methods ​

add() ​

add(...buttons): this

Defined in: keyboards/index.d.ts:127

Allows you to add multiple buttons in raw format.

Parameters ​

ParameterType
...buttonsTelegramInlineKeyboardButton[]

Returns ​

this

Example ​

ts
const labels = ["some", "buttons"];

new InlineKeyboard()
    .add({ text: "raw button", callback_data: "payload" })
    .add(InlineKeyboard.text("raw button by InlineKeyboard.text", "payload"))
    .add(...labels.map((x) => InlineKeyboard.text(x, `${x}payload`)));

Inherited from ​

BaseKeyboardConstructor.add


addIf() ​

addIf(condition, ...buttons): this

Defined in: keyboards/index.d.ts:147

Allows you to dynamically substitute buttons depending on something

Parameters ​

ParameterType
conditionboolean | ((options) => boolean)
...buttonsTelegramInlineKeyboardButton[]

Returns ​

this

Example ​

ts
const labels = ["some", "buttons"];
const isAdmin = true;

new InlineKeyboard()
    .addIf(1 === 2, { text: "raw button", callback_data: "payload" })
    .addIf(
        isAdmin,
        InlineKeyboard.text("raw button by InlineKeyboard.text", "payload")
    )
    .addIf(
        ({ index, rowIndex }) => rowIndex === index,
        ...labels.map((x) => InlineKeyboard.text(x, `${x}payload`))
    );

Inherited from ​

BaseKeyboardConstructor.addIf


build() ​

build(): TelegramInlineKeyboardMarkupFix

Defined in: keyboards/index.d.ts:599

Return TelegramInlineKeyboardMarkup as JSON

Returns ​

TelegramInlineKeyboardMarkupFix


columns() ​

columns(length?): this

Defined in: keyboards/index.d.ts:75

Allows you to limit the number of columns in the keyboard.

Parameters ​

ParameterType
length?number

Returns ​

this

Example ​

ts
new InlineKeyboard()
    .columns(1)
    .text("first row", "payload")
    .text("second row", "payload");
    .text("third row", "payload");

Inherited from ​

BaseKeyboardConstructor.columns


combine() ​

combine(keyboard): this

Defined in: keyboards/index.d.ts:593

Allows you to combine keyboards. Only keyboards are combined. You need to call the .row() method to line-break after combine.

Parameters ​

ParameterType
keyboardTelegramInlineKeyboardMarkup | TelegramInlineKeyboardButton[][] | { toJSON: () => TelegramInlineKeyboardMarkup; }

Returns ​

this

Example ​

ts
new InlineKeyboard()
			.combine(new InlineKeyboard().text("some", "payload"))
			.row()
			.combine(
				new InlineKeyboard()
					.text("test", "payload")
					.row()
					.text("second row???", "payload"),
			)

copy() ​

copy(text, textToCopy, options?): this

Defined in: keyboards/index.d.ts:571

Parameters ​

ParameterType
textstring
textToCopystring | TelegramCopyTextButton
options?ButtonOptions

Returns ​

this


disabled() ​

disabled(text, options?): this

Defined in: keyboards/index.d.ts:574

Add a button that is visible but can't be pressed.

Parameters ​

ParameterType
textstring
options?ButtonOptions

Returns ​

this


filter() ​

filter(fn?): this

Defined in: keyboards/index.d.ts:99

A handler that helps filter keyboard buttons

Parameters ​

ParameterType
fn?ButtonsIterator<TelegramInlineKeyboardButton>

Returns ​

this

Example ​

ts
new InlineKeyboard()
    .filter(({ button }) => button.callback_data !== "hidden")
    .text("button", "pass")
    .text("button", "hidden")
    .text("button", "pass");

Inherited from ​

BaseKeyboardConstructor.filter


forceReply() ​

forceReply(enabled?): this

Defined in: keyboards/index.d.ts:423

Show the reply interface together with this inline keyboard.

Parameters ​

ParameterType
enabled?boolean

Returns ​

this


game() ​

game(text, gameOptions?, options?): this

Defined in: keyboards/index.d.ts:564

Description of the game that will be launched when the user presses the button.

NOTE: This type of button must always be the first button in the first row.

Parameters ​

ParameterType
textstring
gameOptions?TelegramCallbackGame
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().game("text", ???);

login() ​

login(text, url, options?): this

Defined in: keyboards/index.d.ts:476

An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.

Parameters ​

ParameterType
textstring
urlstring | TelegramLoginUrl
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().login("some text", "https://...");
// or
new InlineKeyboard().login("some text", {
   url: "https://...",
   request_write_access: true,
});

matrix() ​

matrix(rows, columns, fn): this

Defined in: keyboards/index.d.ts:167

Allows you to create a button matrix.

Parameters ​

ParameterType
rowsnumber
columnsnumber
fnCreateButtonIterator<TelegramInlineKeyboardButton>

Returns ​

this

Example ​

ts
import { randomInt } from "node:crypto";

const bomb = [randomInt(0, 9), randomInt(0, 9)] as const;

new InlineKeyboard().matrix(10, 10, ({ rowIndex, index }) =>
   InlineKeyboard.text(
       rowIndex === bomb[0] && index === bomb[1] ? "💣" : "ㅤ",
       "payload"
   )
);

Inherited from ​

BaseKeyboardConstructor.matrix


pattern() ​

pattern(pattern?): this

Defined in: keyboards/index.d.ts:114

An array with the number of columns per row. Allows you to set a "template"

Parameters ​

ParameterType
pattern?number[]

Returns ​

this

Example ​

ts
new InlineKeyboard()
    .pattern([1, 3, 2])
    .text("1", "payload")
    .text("2", "payload")
    .text("2", "payload")
    .text("2", "payload")
    .text("3", "payload")
    .text("3", "payload");

Inherited from ​

BaseKeyboardConstructor.pattern


pay() ​

pay(text, options?): this

Defined in: keyboards/index.d.ts:490

Send a Pay button.

NOTE: This type of button must always be the first button in the first row and can only be used in invoice messages.

Parameters ​

ParameterType
textstring
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().pay("5 coins");

resetHelpers() ​

resetHelpers(): this

Defined in: keyboards/index.d.ts:168

Returns ​

this

Inherited from ​

BaseKeyboardConstructor.resetHelpers


row() ​

row(): this

Defined in: keyboards/index.d.ts:63

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

Returns ​

this

Example ​

ts
new InlineKeyboard()
    .text("first row", "payload")
    .row()
    .text("second row", "payload");

Inherited from ​

BaseKeyboardConstructor.row


switchToChat() ​

switchToChat(text, query?, options?): this

Defined in: keyboards/index.d.ts:508

Pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field.

By default empty, in which case just the bot's username will be inserted.

Parameters ​

ParameterType
textstring
query?string
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().switchToChat("Select chat");
// or
new InlineKeyboard().switchToChat("Select chat", "InlineQuery");

switchToChosenChat() ​

switchToChosenChat(text, query?, options?): this

Defined in: keyboards/index.d.ts:532

Pressing the button will prompt the user to select one of their chats of the specified type, open that chat and insert the bot's username and the specified inline query in the input field

Parameters ​

ParameterType
textstring
query?string | TelegramSwitchInlineQueryChosenChat
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().switchToChosenChat("Select chat");
// or
new InlineKeyboard().switchToChosenChat("Select chat", "InlineQuery");
// or
new InlineKeyboard().switchToChosenChat("Select chat", {
    query: "InlineQuery",
    allow_channel_chats: true,
    allow_group_chats: true,
    allow_bot_chats: true,
    allow_user_chats: true,
});

switchToCurrentChat() ​

switchToCurrentChat(text, query?, options?): this

Defined in: keyboards/index.d.ts:548

Pressing the button will insert the bot's username and the specified inline query in the current chat's input field. May be empty, in which case only the bot's username will be inserted.

This offers a quick way for the user to open your bot in inline mode in the same chat - good for selecting something from multiple options.

Parameters ​

ParameterType
textstring
query?string
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().switchToChosenChat("Open Inline mod");
// or
new InlineKeyboard().switchToChosenChat("Open Inline mod", "InlineQuery");

text() ​

text(text, payload, options?): this

Defined in: keyboards/index.d.ts:435

Text button with data to be sent in a callback query to the bot when button is pressed, 1-64 bytes

Parameters ​

ParameterType
textstring
payloadstring | Record<string, unknown>
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().text("some text", "payload");
// or
new InlineKeyboard().text("some text", {
    json: "payload",
}); // it uses JSON.stringify

toJSON() ​

toJSON(): TelegramInlineKeyboardMarkupFix

Defined in: keyboards/index.d.ts:603

Serializing a class into an TelegramInlineKeyboardMarkupFix object (used by JSON.stringify)

Returns ​

TelegramInlineKeyboardMarkupFix


url() ​

url(text, url, options?): this

Defined in: keyboards/index.d.ts:447

HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id=<user_id> can be used to mention a user by their identifier without using a username, if this is allowed by their privacy settings.

Parameters ​

ParameterType
textstring
urlstring
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().url("GitHub", "https://github.com/gramiojs/gramio");

webApp() ​

webApp(text, url, options?): this

Defined in: keyboards/index.d.ts:459

Description of the Web App that will be launched when the user presses the button. The Web App will be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only in private chats between a user and the bot.

Parameters ​

ParameterType
textstring
urlstring
options?ButtonOptions

Returns ​

this

Example ​

ts
new InlineKeyboard().webApp("some text", "https://...");

wrap() ​

wrap(fn?): this

Defined in: keyboards/index.d.ts:87

A custom handler that controls row wrapping.

Parameters ​

ParameterType
fn?ButtonsIterator<TelegramInlineKeyboardButton>

Returns ​

this

Example ​

ts
new InlineKeyboard()
    .wrap(({ button }) => button.callback_data === "2")
    .text("first row", "1")
    .text("first row", "1");
    .text("second row", "2");

Inherited from ​

BaseKeyboardConstructor.wrap


copy() ​

static copy(text, textToCopy, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:572

Parameters ​

ParameterType
textstring
textToCopystring | TelegramCopyTextButton
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


disabled() ​

static disabled(text, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:576

Create a button that is visible but can't be pressed.

Parameters ​

ParameterType
textstring
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


game() ​

static game(text, gameOptions?, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:570

Description of the game that will be launched when the user presses the button.

NOTE: This type of button must always be the first button in the first row.

Parameters ​

ParameterType
textstring
gameOptions?TelegramCallbackGame
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


login() ​

static login(text, url, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:480

An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.

Parameters ​

ParameterType
textstring
urlstring | TelegramLoginUrl
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


pay() ​

static pay(text, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:496

Send a Pay button.

NOTE: This type of button must always be the first button in the first row and can only be used in invoice messages.

Parameters ​

ParameterType
textstring
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


switchToChat() ​

static switchToChat(text, query?, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:514

If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field.

By default empty, in which case just the bot's username will be inserted.

Parameters ​

ParameterType
textstring
query?string
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


switchToChosenChat() ​

static switchToChosenChat(text, query?, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:536

Pressing the button will prompt the user to select one of their chats of the specified type, open that chat and insert the bot's username and the specified inline query in the input field

Parameters ​

ParameterType
textstring
query?string | TelegramSwitchInlineQueryChosenChat
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


switchToCurrentChat() ​

static switchToCurrentChat(text, query?, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:554

Pressing the button will insert the bot's username and the specified inline query in the current chat's input field. May be empty, in which case only the bot's username will be inserted.

This offers a quick way for the user to open your bot in inline mode in the same chat - good for selecting something from multiple options.

Parameters ​

ParameterType
textstring
query?string
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


text() ​

static text(text, payload, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:439

Text button with data to be sent in a callback query to the bot when button is pressed, 1-64 bytes

Parameters ​

ParameterType
textstring
payloadstring | Record<string, unknown>
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


url() ​

static url(text, url, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:451

HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id=<user_id> can be used to mention a user by their identifier without using a username, if this is allowed by their privacy settings.

Parameters ​

ParameterType
textstring
urlstring
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton


webApp() ​

static webApp(text, url, options?): TelegramInlineKeyboardButton

Defined in: keyboards/index.d.ts:463

Description of the Web App that will be launched when the user presses the button. The Web App will be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only in private chats between a user and the bot.

Parameters ​

ParameterType
textstring
urlstring
options?ButtonOptions

Returns ​

TelegramInlineKeyboardButton