Skip to content
Alexey Nesterov edited this page Aug 3, 2017 · 6 revisions

Custom keyboards

Custom keyboards are supported by Telegram. More information

KeyboardButton and ReplyKeboardMarkup

KeyboardButton - This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button.

ReplyKeboardMarkup - This object represents a custom keyboard with reply options.

When you send a message to a user, you can specify a custom keyboard. You need to do as follow:

            KeyboardButton[] lines1 =
            {
                new KeyboardButton {Text = "Button1"},
                new KeyboardButton {Text = "Button2"},
                new KeyboardButton {Text = "Button3"},
                new KeyboardButton {Text = "Button4"}
            };

            KeyboardButton[] lines2 =
            {
                new KeyboardButton {Text = "Button5"},
                new KeyboardButton {Text = "Button4"}
            };

            KeyboardButton[][] keyboard =
            {
                lines1, lines2
            };

            ReplyKeyboardMarkup replyMarkup = new ReplyKeyboardMarkup
            {
                Keyboard = keyboard,
            };

Keyboards are 2 dimensional arrays of KeyboardButton (Arrays of Arrays of KeyboardButton). An array for each line of keys on the keyboard and all lines in an array to define the complete keyboard.

Optional fileds in KeyboardButton

            KeyboardButton[] lines1 =
            {
                new KeyboardButton {Text = "Button1", RequestContact = true},
                new KeyboardButton {Text = "Button2", RequestLocation = true}
            };

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

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

Optional fields are mutually exclusive.

Optional fileds in ReplyKeboardMarkup

            ReplyKeyboardMarkup replyMarkup = new ReplyKeyboardMarkup
            {
                Keyboard = keyboard,
                ResizeKeyboard = true,
                Selective = false,
                OneTimeKeyboard = true
            };

ResizeKeyboard - Optional. 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 - Optional. 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.

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.

OneTimeKeyboard - Optional. 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.

Sending ReplyKeboardMarkup

client.SendMessage(chatId, "Please select an option", replyMarkup: keyboardMarkup);