GramIO API Reference / @gramio/keyboards/dist / InlineKeyboard
Class: InlineKeyboard
Defined in: keyboards/index.d.ts:416
InlineKeyboardMarkup builder
This object represents an inline keyboard that appears right next to the message it belongs to.
Extends
Constructors
Constructor
new InlineKeyboard(
featureFlags?):InlineKeyboard
Defined in: keyboards/index.d.ts:48
Parameters
| Parameter | Type |
|---|---|
featureFlags? | KeyboardFeatureFlags |
Returns
InlineKeyboard
Inherited from
BaseKeyboardConstructor.constructor
Properties
| Property | Modifier | Type | Inherited from | Defined in |
|---|---|---|---|---|
currentRow | protected | TelegramInlineKeyboardButton[] | BaseKeyboardConstructor.currentRow | keyboards/index.d.ts:46 |
featureFlags | protected | KeyboardFeatureFlags | BaseKeyboardConstructor.featureFlags | keyboards/index.d.ts:47 |
rows | protected | TelegramInlineKeyboardButton[][] | BaseKeyboardConstructor.rows | keyboards/index.d.ts:45 |
Accessors
keyboard
Get Signature
get
protectedkeyboard():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
| Parameter | Type |
|---|---|
...buttons | TelegramInlineKeyboardButton[] |
Returns
this
Example
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
addIf()
addIf(
condition, ...buttons):this
Defined in: keyboards/index.d.ts:147
Allows you to dynamically substitute buttons depending on something
Parameters
| Parameter | Type |
|---|---|
condition | boolean | ((options) => boolean) |
...buttons | TelegramInlineKeyboardButton[] |
Returns
this
Example
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
build()
build():
TelegramInlineKeyboardMarkupFix
Defined in: keyboards/index.d.ts:588
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
| Parameter | Type |
|---|---|
length? | number |
Returns
this
Example
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:582
Allows you to combine keyboards. Only keyboards are combined. You need to call the .row() method to line-break after combine.
Parameters
| Parameter | Type |
|---|---|
keyboard | TelegramInlineKeyboardMarkup | TelegramInlineKeyboardButton[][] | { toJSON: () => TelegramInlineKeyboardMarkup; } |
Returns
this
Example
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:564
Parameters
| Parameter | Type |
|---|---|
text | string |
textToCopy | string | TelegramCopyTextButton |
options? | ButtonOptions |
Returns
this
filter()
filter(
fn?):this
Defined in: keyboards/index.d.ts:99
A handler that helps filter keyboard buttons
Parameters
| Parameter | Type |
|---|---|
fn? | ButtonsIterator<TelegramInlineKeyboardButton> |
Returns
this
Example
new InlineKeyboard()
.filter(({ button }) => button.callback_data !== "hidden")
.text("button", "pass")
.text("button", "hidden")
.text("button", "pass");Inherited from
BaseKeyboardConstructor.filter
game()
game(
text,gameOptions?,options?):this
Defined in: keyboards/index.d.ts:557
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
| Parameter | Type |
|---|---|
text | string |
gameOptions? | TelegramCallbackGame |
options? | ButtonOptions |
Returns
this
Example
new InlineKeyboard().game("text", ???);login()
login(
text,url,options?):this
Defined in: keyboards/index.d.ts:469
An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.
Parameters
| Parameter | Type |
|---|---|
text | string |
url | string | TelegramLoginUrl |
options? | ButtonOptions |
Returns
this
Example
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
| Parameter | Type |
|---|---|
rows | number |
columns | number |
fn | CreateButtonIterator<TelegramInlineKeyboardButton> |
Returns
this
Example
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
| Parameter | Type |
|---|---|
pattern? | number[] |
Returns
this
Example
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:483
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
| Parameter | Type |
|---|---|
text | string |
options? | ButtonOptions |
Returns
this
Example
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
new InlineKeyboard()
.text("first row", "payload")
.row()
.text("second row", "payload");Inherited from
switchToChat()
switchToChat(
text,query?,options?):this
Defined in: keyboards/index.d.ts:501
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
| Parameter | Type |
|---|---|
text | string |
query? | string |
options? | ButtonOptions |
Returns
this
Example
new InlineKeyboard().switchToChat("Select chat");
// or
new InlineKeyboard().switchToChat("Select chat", "InlineQuery");switchToChosenChat()
switchToChosenChat(
text,query?,options?):this
Defined in: keyboards/index.d.ts:525
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
| Parameter | Type |
|---|---|
text | string |
query? | string | TelegramSwitchInlineQueryChosenChat |
options? | ButtonOptions |
Returns
this
Example
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:541
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
| Parameter | Type |
|---|---|
text | string |
query? | string |
options? | ButtonOptions |
Returns
this
Example
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:428
Text button with data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
Parameters
| Parameter | Type |
|---|---|
text | string |
payload | string | Record<string, unknown> |
options? | ButtonOptions |
Returns
this
Example
new InlineKeyboard().text("some text", "payload");
// or
new InlineKeyboard().text("some text", {
json: "payload",
}); // it uses JSON.stringifytoJSON()
toJSON():
TelegramInlineKeyboardMarkupFix
Defined in: keyboards/index.d.ts:592
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:440
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
| Parameter | Type |
|---|---|
text | string |
url | string |
options? | ButtonOptions |
Returns
this
Example
new InlineKeyboard().url("GitHub", "https://github.com/gramiojs/gramio");webApp()
webApp(
text,url,options?):this
Defined in: keyboards/index.d.ts:452
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
| Parameter | Type |
|---|---|
text | string |
url | string |
options? | ButtonOptions |
Returns
this
Example
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
| Parameter | Type |
|---|---|
fn? | ButtonsIterator<TelegramInlineKeyboardButton> |
Returns
this
Example
new InlineKeyboard()
.wrap(({ button }) => button.callback_data === "2")
.text("first row", "1")
.text("first row", "1");
.text("second row", "2");Inherited from
copy()
staticcopy(text,textToCopy,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:565
Parameters
| Parameter | Type |
|---|---|
text | string |
textToCopy | string | TelegramCopyTextButton |
options? | ButtonOptions |
Returns
game()
staticgame(text,gameOptions?,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:563
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
| Parameter | Type |
|---|---|
text | string |
gameOptions? | TelegramCallbackGame |
options? | ButtonOptions |
Returns
login()
staticlogin(text,url,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:473
An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.
Parameters
| Parameter | Type |
|---|---|
text | string |
url | string | TelegramLoginUrl |
options? | ButtonOptions |
Returns
pay()
staticpay(text,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:489
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
| Parameter | Type |
|---|---|
text | string |
options? | ButtonOptions |
Returns
switchToChat()
staticswitchToChat(text,query?,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:507
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
| Parameter | Type |
|---|---|
text | string |
query? | string |
options? | ButtonOptions |
Returns
switchToChosenChat()
staticswitchToChosenChat(text,query?,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:529
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
| Parameter | Type |
|---|---|
text | string |
query? | string | TelegramSwitchInlineQueryChosenChat |
options? | ButtonOptions |
Returns
switchToCurrentChat()
staticswitchToCurrentChat(text,query?,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:547
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
| Parameter | Type |
|---|---|
text | string |
query? | string |
options? | ButtonOptions |
Returns
text()
statictext(text,payload,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:432
Text button with data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
Parameters
| Parameter | Type |
|---|---|
text | string |
payload | string | Record<string, unknown> |
options? | ButtonOptions |
Returns
url()
staticurl(text,url,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:444
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
| Parameter | Type |
|---|---|
text | string |
url | string |
options? | ButtonOptions |
Returns
webApp()
staticwebApp(text,url,options?):TelegramInlineKeyboardButton
Defined in: keyboards/index.d.ts:456
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
| Parameter | Type |
|---|---|
text | string |
url | string |
options? | ButtonOptions |