styled_keyboards¶
This example shows how to use the new KeyboardButtonStyle to create styled keyboard buttons with custom colors and icons.
Available since Layer 224, keyboard buttons can now have custom styles including background colors (primary, danger, success) and custom emoji icons. This works for both ReplyKeyboardMarkup (regular keyboards) and InlineKeyboardMarkup (inline buttons).
Inline Keyboard Examples¶
from pyrogram import Client
from pyrogram.types import (
InlineKeyboardMarkup,
InlineKeyboardButton,
KeyboardButtonStyle,
WebAppInfo,
LoginUrl
)
# Create a client using your bot token
app = Client("my_bot", bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11")
async def main():
async with app:
# Example 1: Callback button with primary (blue) background
await app.send_message(
"me", # Edit this
"Callback button example:",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
text="Click Me",
callback_data="clicked",
style=KeyboardButtonStyle(bg_primary=True)
)
]]
)
)
# Example 2: URL button with danger (red) style
await app.send_message(
"me", # Edit this
"URL button example:",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
text="Open Website",
url="https://example.com",
style=KeyboardButtonStyle(bg_danger=True)
)
]]
)
)
# Example 3: Switch inline query button with success (green) style
await app.send_message(
"me", # Edit this
"Inline query buttons:",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="Search in other chat",
switch_inline_query="search query",
style=KeyboardButtonStyle(bg_primary=True)
)
],
[
InlineKeyboardButton(
text="Search here",
switch_inline_query_current_chat="search query",
style=KeyboardButtonStyle(bg_success=True)
)
]
]
)
)
# Example 4: User profile button with custom emoji icon
await app.send_message(
"me", # Edit this
"User profile button:",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
text="View Profile",
user_id=123456789, # Replace with actual user ID
style=KeyboardButtonStyle(
icon=5368324170671202286 # Custom emoji ID
)
)
]]
)
)
# Example 5: WebApp button with primary style
await app.send_message(
"me", # Edit this
"WebApp button:",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
text="Open WebApp",
web_app=WebAppInfo(url="https://example.com/webapp"),
style=KeyboardButtonStyle(bg_primary=True)
)
]]
)
)
# Example 6: Login URL button with style
await app.send_message(
"me", # Edit this
"Login button:",
reply_markup=InlineKeyboardMarkup(
[[
InlineKeyboardButton(
text="Login with Telegram",
login_url=LoginUrl(
url="https://example.com/login",
forward_text="Login to Example",
request_write_access=True
),
style=KeyboardButtonStyle(bg_success=True)
)
]]
)
)
# Example 7: All button types in one keyboard
await app.send_message(
"me", # Edit this
"Complete keyboard with all styled button types:",
reply_markup=InlineKeyboardMarkup(
[
# Row 1: Callback buttons
[
InlineKeyboardButton(
text="✅ Confirm",
callback_data="confirm",
style=KeyboardButtonStyle(bg_success=True)
),
InlineKeyboardButton(
text="❌ Cancel",
callback_data="cancel",
style=KeyboardButtonStyle(bg_danger=True)
)
],
# Row 2: URL button
[
InlineKeyboardButton(
text="🌐 Visit Website",
url="https://example.com",
style=KeyboardButtonStyle(bg_primary=True)
)
],
# Row 3: Inline query buttons
[
InlineKeyboardButton(
text="🔍 Search",
switch_inline_query_current_chat="",
style=KeyboardButtonStyle(
icon=5368324170671202286 # Search emoji
)
)
]
]
)
)
app.run(main())