Build Telegram Bot Inline Keyboards with Pagination
Frequently asked questions
What is the maximum number of buttons on a Telegram bot inline keyboard?
Telegram bot inline keyboards allow a maximum of 100 buttons per message. Because of this limit, developers must implement pagination when displaying large datasets or long lists. Splitting items across multiple pages ensures the menu remains user-friendly and prevents the message from exceeding Telegram's strict API constraints.
How do callback queries work in Telegram bot inline keyboards?
When a user taps a button on telegram bot inline keyboards, a callback query is sent to the bot containing specific data. Instead of sending a new text command, the bot intercepts this payload, processes the requested action, and can dynamically update the original message. This mechanism is essential for creating interactive scrolling menus.
Can I edit a Telegram inline keyboard message instead of sending a new one?
Yes, using the editMessageReplyMarkup or editMessageText API methods, you can dynamically update telegram bot inline keyboards without sending new messages. This creates a seamless pagination experience. When a user clicks Next Page, the bot simply swaps out the old keyboard and text for the new page's content.
How to store pagination state for Telegram bot inline keyboards?
To track pagination state, you can embed page numbers directly into the callback data string, such as page_2 or item_15_next. When the bot receives the callback query, it parses this string to determine the current page. Alternatively, you can store user state in a database, but embedding it in the callback data is generally more efficient.
Why did my Telegram bot inline keyboard stop working after 48 hours?
Telegram bot inline keyboards attached to messages older than 48 hours cannot trigger callback queries. If a user tries to click a button on an older paginated menu, the bot will not receive the update. To fix this, design your bot to send a fresh message or alert the user if they interact with an expired inline keyboard.
How do I prevent Telegram bot inline keyboards from showing empty pages?
When building pagination, dynamically generate your telegram bot inline keyboards based on the total item count. Calculate the maximum number of pages first. If the user is on the last page, omit the Next button. If they are on the first page, omit the Previous button. This prevents users from scrolling into empty menus.
Related resources
Build it with TheDevs
Post what you want built and TheDevs starts your project — any tech work, one team.