Skip to main content

Built and signed on GitHub Actions

Framework-agnostic Telegram bot keyboard builder with many cool features!

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.1.0)
class Keyboard
extends BaseKeyboardConstructor<TelegramKeyboardButton>

ReplyKeyboardMarkup builder

This object represents a custom keyboard with reply options (see Introduction to bots for details and examples).

[Documentation]

Properties

options: { isOneTime: boolean; isPersistent: boolean; isResized: boolean; isSelective: boolean; placeholder: string | undefined; }

Methods

build(): TelegramReplyKeyboardMarkup

Return TelegramReplyKeyboardMarkup as object

combine(keyboard:
TelegramKeyboardButton[][]
| TelegramReplyKeyboardMarkup
| { toJSON: () => TelegramReplyKeyboardMarkup; }
): this

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

oneTime(isEnabled?: boolean): this

Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat - the user can press a special button in the input field to see the custom keyboard again. Defaults to false.

persistent(isEnabled?: boolean): this

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.

placeholder(value?: string): this

The placeholder to be shown in the input field when the keyboard is active; 1-64 characters

requestChat(
text: string,
requestId: number,
options?: Omit<TelegramKeyboardButtonRequestChat, "request_id" | "chat_is_channel"> & { chat_is_channel?: boolean; },
): this

If specified, pressing the button will open a list of suitable chats. Tapping on a chat will send its identifier to the bot in a “chat_shared” service message. Available in private chats only.

If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only.

If True, the user's current location will be sent when the button is pressed. Available in private chats only.

requestPoll(
text: string,
type?: TelegramKeyboardButtonPollType["type"],
): this

If specified, 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.

If quiz is passed, the user will be allowed to create only polls in the quiz mode. If regular is passed, only regular polls will be allowed. Otherwise, the user will be allowed to create a poll of any type.

requestUsers(
text: string,
requestId: number,
options?: Omit<TelegramKeyboardButtonRequestUsers, "request_id">,
): this

If specified, pressing the button will open a list of suitable users. Identifiers of selected users will be sent to the bot in a “users_shared” service message. Available in private chats only.

resized(isEnabled?: boolean): this

!Note Keyboard is resized by default! For disable it you can use .resized(false)

Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.

selective(isEnabled?: boolean): this

Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply to a message in the same chat and forum topic, sender of the original message.

Example: A user requests to change the bot's language, bot replies to the request with a keyboard to select the new language. Other users in the group don't see the keyboard.

text(text: string): this

Text of the button. It will be sent as a message when the button is pressed

toJSON(): TelegramReplyKeyboardMarkup

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

webApp(
text: string,
url: string,
): this

If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only.

Static Methods

requestChat(
text: string,
requestId: number,
options?: Omit<TelegramKeyboardButtonRequestChat, "request_id" | "chat_is_channel"> & { chat_is_channel?: boolean; },
): TelegramKeyboardButton

If specified, pressing the button will open a list of suitable chats. 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): TelegramKeyboardButton

If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only.

requestLocation(text: string): TelegramKeyboardButton

If True, the user's current location will be sent when the button is pressed. Available in private chats only.

requestPoll(
text: string,
type?: TelegramKeyboardButtonPollType["type"],
): TelegramKeyboardButton

If specified, 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.

If quiz is passed, the user will be allowed to create only polls in the quiz mode. If regular is passed, only regular polls will be allowed. Otherwise, the user will be allowed to create a poll of any type.

requestUsers(
text: string,
requestId: number,
options?: Omit<TelegramKeyboardButtonRequestUsers, "request_id">,
): TelegramKeyboardButton

If specified, pressing the button will open a list of suitable users. Identifiers of selected users will be sent to the bot in a “users_shared” service message. Available in private chats only.

text(text: string): TelegramKeyboardButton

Text of the button. It will be sent as a message when the button is pressed

webApp(
text: string,
url: string,
): TelegramKeyboardButton

If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only.

Add Package

deno add jsr:@gramio/keyboards

Import symbol

import { Keyboard } from "@gramio/keyboards";

---- OR ----

Import directly with a jsr specifier

import { Keyboard } from "jsr:@gramio/keyboards";

Add Package

npx jsr add @gramio/keyboards

Import symbol

import { Keyboard } from "@gramio/keyboards";

Add Package

yarn dlx jsr add @gramio/keyboards

Import symbol

import { Keyboard } from "@gramio/keyboards";

Add Package

pnpm dlx jsr add @gramio/keyboards

Import symbol

import { Keyboard } from "@gramio/keyboards";

Add Package

bunx jsr add @gramio/keyboards

Import symbol

import { Keyboard } from "@gramio/keyboards";