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

x/grammy/mod.ts>Keyboard

The Telegram Bot Framework.
Go to Latest
class Keyboard
import { Keyboard } from "https://deno.land/x/grammy@v1.15.3/mod.ts";

Use this class to simplify building a custom keyboard (something like this: https://core.telegram.org/bots#keyboards).

// Build a custom keyboard:
const keyboard = new Keyboard()
  .text('A').text('B').row()
  .text('C').text('D')

// Now you can either pass it directly:
await ctx.reply('Here is your custom keyboard!', {
  reply_markup: keyboard
})
// Or if you need to specify more options in `reply_markup`:
await ctx.reply('Here is your custom keyboard!', {
  reply_markup: {
    keyboard: keyboard.build(), // note the `build` call
    one_time_keyboard: true,
  }
})

Be sure to check out the documentation on custom keyboards in grammY.

Constructors

new
Keyboard(keyboard?: KeyboardButton[][])

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

Properties

optional
input_field_placeholder: string

Placeholder to be shown in the input field when the keyboard is active.

optional
is_persistent: boolean

Requests clients to always show the keyboard when the regular keyboard is hidden. Defaults to false, in which case the custom keyboard can be hidden and opened with a keyboard icon.

optional
one_time_keyboard: boolean

Hide the keyboard after a button is pressed.

optional
resize_keyboard: boolean

Resize the current keyboard according to its buttons. Usually, this will make the keyboard smaller.

optional
selective: boolean

Show the current keyboard only to those users that are mentioned in the text of the message object.

Methods

add(...buttons: KeyboardButton[])

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

Returns the keyboard that was build. Note that it doesn't return resize_keyboard or other options that may be set. You don't usually need to call this method. It is no longer useful.

oneTime(isEnabled?)

Make the current keyboard one-time. See https://grammy.dev/plugins/keyboard.html#one-time-custom-keyboards for more details.

Keyboards are non-one-time by default, use this function to enable it (without any parameters or pass true). Pass false to force the keyboard to be non-one-time.

persistent(isEnabled?)

Make the current keyboard persistent. See https://grammy.dev/plugins/keyboard.html#persistent-keyboards for more details.

Keyboards are not persistent by default, use this function to enable it (without any parameters or pass true). Pass false to force the keyboard to not persist.

placeholder(value: string)

Set the current keyboard's input field placeholder. See https://grammy.dev/plugins/keyboard.html#input-field-placeholder for more details.

requestChat(
text: string,
requestId: number,
options?: Omit<KeyboardButtonRequestChat, "request_id">,
)

Adds a new request chat button. When the user presses the button, a list of suitable users will be opened. Tapping on a chat will send its identifier to the bot in a “chat_shared” service message. Available in private chats only.

requestContact(text: string)

Adds a new contact request button. The user's phone number will be sent as a contact when the button is pressed. Available in private chats only.

requestLocation(text: string)

Adds a new location request button. The user's current location will be sent when the button is pressed. Available in private chats only.

requestPoll(text: string, type?: "quiz" | "regular")

Adds a new poll request button. The user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only.

requestUser(
text: string,
requestId: number,
options?: Omit<KeyboardButtonRequestUser, "request_id">,
)

Adds a new request user button. When the user presses the button, a list of suitable users will be opened. Tapping on any user will send their identifier to the bot in a “user_shared” service message. Available in private chats only.

resized(isEnabled?)

Make the current keyboard resized. See https://grammy.dev/plugins/keyboard.html#resize-custom-keyboard for more details.

Keyboards are non-resized by default, use this function to enable it (without any parameters or pass true). Pass false to force the keyboard to be non-resized.

row(...buttons: KeyboardButton[])

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 KeyboardButton objects if you already have the instances for some reason. You most likely don't want to pass any arguments to row.

selected(isEnabled?)

Make the current keyboard selective. See https://grammy.dev/plugins/keyboard.html#selectively-send-custom-keyboards for more details.

Keyboards are non-selective by default, use this function to enable it (without any parameters or pass true). Pass false to force the keyboard to be non-selective.

text(text: string)

Adds a new text button. This button will simply send the given text as a text message back to your bot if a user clicks on it.

webApp(text: string, url: string)

Adds a new web app button. The Web App that will be launched when the user presses the button. The Web App will be able to send a “web_app_data” service message. Available in private chats only.