Skip to content

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

Class: BaseKeyboardConstructor<T> ​

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

Base-class for construct keyboard with useful helpers

Extended by ​

Type Parameters ​

Type Parameter
T

Constructors ​

Constructor ​

new BaseKeyboardConstructor<T>(featureFlags?): BaseKeyboardConstructor<T>

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

Parameters ​

ParameterType
featureFlags?KeyboardFeatureFlags

Returns ​

BaseKeyboardConstructor<T>

Properties ​

PropertyModifierTypeDefined in
currentRowprotectedT[]keyboards/index.d.ts:46
featureFlagsprotectedKeyboardFeatureFlagskeyboards/index.d.ts:47
rowsprotectedT[][]keyboards/index.d.ts:45

Accessors ​

keyboard ​

Get Signature ​

get protected keyboard(): T[][]

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

Returns ​

T[][]

Methods ​

add() ​

add(...buttons): this

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

Allows you to add multiple buttons in raw format.

Parameters ​

ParameterType
...buttonsT[]

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`)));

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)
...buttonsT[]

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`))
    );

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");

filter() ​

filter(fn?): this

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

A handler that helps filter keyboard buttons

Parameters ​

ParameterType
fn?ButtonsIterator<T>

Returns ​

this

Example ​

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

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<T>

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"
   )
);

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");

resetHelpers() ​

resetHelpers(): this

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

Returns ​

this


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");

wrap() ​

wrap(fn?): this

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

A custom handler that controls row wrapping.

Parameters ​

ParameterType
fn?ButtonsIterator<T>

Returns ​

this

Example ​

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