Creating Interactive Menus with TPY
Inline Keyboard Basics
Inline keyboards allow you to add clickable buttons to your messages. Users can interact with these buttons without typing commands.
// Simple inline keyboard
var keyboard = [
[{text: "đ Search", callback_data: "search"}],
[{text: "đ Categories", callback_data: "categories"}],
[{text: "âšī¸ Help", callback_data: "help"}]
];
bot.sendInlineKeyboard(keyboard, "What would you like to do?");
Multi-Row Keyboards
You can organize buttons into multiple rows for better layout:
// Keyboard with multiple rows
var keyboard = [
[{text: "đĨ Popular", callback_data: "popular"}, {text: "⨠New", callback_data: "new"}],
[{text: "đ Categories", callback_data: "categories"}, {text: "đˇī¸ Tags", callback_data: "tags"}],
[{text: "âī¸ Settings", callback_data: "settings"}, {text: "â Help", callback_data: "help"}]
];
bot.sendInlineKeyboard(keyboard, "Browse content:");
Handling Button Clicks
When a user clicks a button, it sends a callback query to your bot. You need to handle these callbacks in your commands.
// In your command that handles callback_data="categories"
var categories = ["Technology", "Gaming", "Education", "Entertainment"];
var catKeyboard = [];
for (var i = 0; i < categories.length; i++) {
catKeyboard.push([{text: categories[i], callback_data: "cat_" + i}]);
}
bot.sendInlineKeyboard(catKeyboard, "Select a category:");
URL Buttons
Buttons can also link to external websites:
var keyboard = [
[{text: "đ Documentation", url: "https://docs.example.com"}],
[{text: "đŦ Support Chat", url: "https://t.me/support"}],
[{text: "đ Back", callback_data: "main_menu"}]
];
bot.sendInlineKeyboard(keyboard, "Need help? Check these resources:");
Dynamic Menus
Create dynamic menus that change based on user data or context:
var userRole = User.getProperty("role");
var keyboard = [];
// Common options for all users
keyboard.push([{text: "đ¤ Profile", callback_data: "profile"}]);
// Admin-only options
if (userRole === "admin") {
keyboard.push([{text: "âī¸ Admin Panel", callback_data: "admin"}]);
keyboard.push([{text: "đ Statistics", callback_data: "stats"}]);
}
// Premium member options
if (userRole === "premium" || userRole === "admin") {
keyboard.push([{text: "⨠Premium Features", callback_data: "premium"}]);
}
bot.sendInlineKeyboard(keyboard, "Menu:");
Updating Messages
You can edit existing messages to update menus without sending new messages:
// After user clicks a button, update the message
var newKeyboard = [
[{text: "â
Option Selected", callback_data: "selected"}],
[{text: "đ Back", callback_data: "back"}]
];
api.editMessageText({
message_id: request.message_id,
text: "You selected an option. What would you like to do next?",
reply_markup: {inline_keyboard: newKeyboard}
});
Learn More
For more interactive menu examples, visit:
đ Telebot Creator Documentation
Best Practices
Keep menus simple and intuitive. Use emojis to make buttons visually appealing. Always provide a way for users to go back to the main menu.
đ Source
This tutorial is based on the official Telebot Creator Documentation. Visit their site for the most up-to-date information and advanced guides.