Adding a Keyboard to Your Bot¶
Typing out commands is fine for developers, but regular users would much rather tap buttons. Reply keyboards put buttons right below the message input box. When a user taps a button, Telegram sends that text as a message automatically.
In this tutorial, we will upgrade the /start command you built in the last step into a mini navigation menu!
What We Are Building¶
We will modify /start so that:
- It sends a message asking the user to choose an option.
- It shows two buttons: Help and About.
- We will create two new commands that respond when the user taps those buttons.
Step 1: Add Buttons to /start¶
Let's configure the keyboard layout:
- Open your bot dashboard and click Edit (pencil icon) next to your
/startcommand. - Find the Keyboard field.
- Type the button labels separated by a comma:
- Click Save Command.
Now, when someone sends /start, they will see Help and About buttons sitting side-by-side in a single row.
Keyboard Layout Rules¶
Arranging your buttons is super simple. You just type text with these rules:
- Buttons on the same row: Separate them with a comma
,(e.g.,Yes, No➔ Row 1:Yes|No) - Buttons on a new row: Separate them with a newline
\nor a line break (e.g.,Yes\nNo➔ Row 1:Yes, Row 2:No) - Mix it up:
Yes, No\nCancel➔ Row 1:Yes|No, Row 2:Cancel.
Step 2: Make the Buttons Work¶
Right now, if you tap the Help button, Telegram will send the text Help to the chat, but nothing will happen because your bot doesn't know what Help means yet.
Let's create the handler commands:
Create the Help Command:¶
- Click Add Command.
- Command: Type
Help(Note: Capitalization matters!Helpis not the same ashelp). - Answer: Type your help message:
- Save the command.
Create the About Command:¶
- Click Add Command.
- Command: Type
About. - Answer: Type your description:
- Save the command.
Step 3: Test It!¶
- Open your bot in Telegram and send
/start. The menu buttons should pop up at the bottom of your screen. - Tap Help. Your bot should reply with your help message.
- Tap About. Your bot should reply with your about description.
If a button doesn't do anything, double-check that the command name in your dashboard matches the button label exactly (character for character!).
What's Next?¶
If you have multiple buttons that should lead to the same command (like Help, help, and /help), you don't need to duplicate your code. In the next step, we will look at using Aliases to map multiple triggers to a single command.