Keyboard class simplifies building custom reply keyboards (bottom-of-screen keyboards that replace the system keyboard). These keyboards appear when users interact with your bot and send text responses back when buttons are pressed.
Constructor
Creates a new Keyboard instance.KeyboardButton[][]
Optional two-dimensional array of keyboard buttons. If not provided, starts with an empty keyboard.
Example
Properties
KeyboardButton[][]
The two-dimensional array of buttons that makes up the keyboard (read-only).
boolean
Requests clients to always show the keyboard when the regular keyboard is hidden. Defaults to
false.boolean
Show the keyboard only to users mentioned in the text of the message object.
boolean
Hide the keyboard after a button is pressed.
boolean
Resize the keyboard according to its buttons. Usually makes the keyboard smaller.
string
Placeholder shown in the input field when the keyboard is active.
Button Methods
These methods add buttons to the keyboard and returnthis for chaining.
text
Adds a text button that sends its text as a message when pressed.string
required
The button text to display
string | object
Button style (
'primary', 'success', 'danger') or additional button propertiesrequestUsers
Adds a button that requests users to be shared when pressed.string | object
required
The button text to display
number
required
A signed 32-bit identifier of the request that will be received back in
users_shared service messageobject
Additional requirements for user selection
requestChat
Adds a button that requests a chat to be shared when pressed.string | object
required
The button text to display
number
required
A signed 32-bit identifier of the request that will be received back in
chat_shared service messageobject
Requirements for chat selection. Defaults to
{ chat_is_channel: false }.requestContact
Adds a button that requests the user’s phone number as a contact.string | object
required
The button text to display
requestLocation
Adds a button that requests the user’s current location.string | object
required
The button text to display
requestPoll
Adds a button that requests the user to create and send a poll.string | object
required
The button text to display
'quiz' | 'regular'
Type of poll allowed. Omit to allow any type.
webApp
Adds a button that opens a Web App.string | object
required
The button text to display
string
required
HTTPS URL of the Web App to be opened
Styling Methods
These methods modify the last added button.style
Applies a style to the last added button.'primary' | 'success' | 'danger'
required
Button style:
'primary' (blue), 'success' (green), or 'danger' (red)danger
Applies danger (red) style. Alias for.style('danger').
success
Applies success (green) style. Alias for.style('success').
primary
Applies primary (blue) style. Alias for.style('primary').
icon
Adds a custom emoji icon to the last added button.string
required
Unique identifier of the custom emoji
Layout Methods
row
Adds a line break to start a new row of buttons.KeyboardButton[]
Optional buttons to add to the new row
add
Adds pre-constructed button objects to the current row.KeyboardButton[]
required
Button objects to add
Transformation Methods
toTransposed
Creates a new keyboard with rows and columns flipped.toFlowed
Creates a new keyboard with buttons reflowed into a given number of columns.number
required
Maximum number of buttons per row
object
clone
Creates a deep copy of the keyboard.KeyboardButton[][]
Optional button array to use instead of cloning the current one
append
Appends buttons from other keyboards.Array
required
Keyboards or button arrays to append
Configuration Methods
persistent
Makes the keyboard persistent (always shown).boolean
Whether to enable persistence. Defaults to
true.selected
Makes the keyboard selective (only shown to mentioned users).boolean
Whether to enable selective mode. Defaults to
true.oneTime
Makes the keyboard hide after a button is pressed.boolean
Whether to enable one-time mode. Defaults to
true.resized
Makes the keyboard resize to fit its buttons.boolean
Whether to enable resizing. Defaults to
true.placeholder
Sets the input field placeholder text.string
required
The placeholder text
Static Methods
Keyboard.text
Creates a text button without adding it to a keyboard.Keyboard.from
Creates a keyboard from a two-dimensional button array.KeyboardButton[][] | Keyboard
required
Button array or existing keyboard to copy
requestUsers, requestChat, etc.) also have static equivalents.
Complete Example
See Also
- InlineKeyboard - Inline keyboards with callback buttons
- Keyboards Guide - Complete keyboard documentation
- Telegram Bot API: ReplyKeyboardMarkup