Buttons
RimTUB 2.2 provides convenient tools for working with buttons, unlike previous versions of yub.
Telegram personal accounts cannot create buttons, so a linked bot and inline mode are used for their creation.
Data Types
class Buttons
Creates a set of inline buttons.
Arguments:
inline_keyboard(list[Button]) - A list of button rows, where each row is a list ofButtonobjects.general_extra_data(dict, optional) = None - Additional data applied to all buttons.
Example:
buttons = Buttons([
[ # first row of buttons
Button('Hello', callback_data='hello'), # first button in the row
Button('Copy', copy_text='copied!') # second button in the row
],
[ # second row of buttons
Button('Link', url='https://example.com') # first button in the row
]
])

class Button
Creates an individual inline button with various parameters.
Arguments:
text(str) - The button text.callback_data(str, optional) - Callback identifier for handling clicks.url(str, optional) - A URL opened when the button is clicked.copy_text(str, optional) - Text copied when the button is clicked.extra_data(dict, optional) - Additional data passed to the@mod.callbackhandler.
Example:
button = Button('Click me', callback_data='click')
Sending Buttons
Buttons should be sent using the send_buttons method. Read more here.
Example:
await mod.send_buttons(
msg.chat.id,
"Here are your buttons!",
buttons,
message_thread_id=msg.message_thread_id
)
Handling Button Clicks
To handle button clicks, specify callback_data in the button. This callback can be tracked using the @mod.callback decorator.
@mod.callback('hello')
async def _hello(c: C):
await c.answer("Hello!", True)
Read more about this decorator here.
Passing Data
Often, you need to pass some data through buttons. There are two ways to do this, each with its pros and cons.
Extra Data
Buttons with callback_data support an additional extra_data field, where arbitrary data in dict format can be passed. This data is available only to the handler and is not displayed in callback_data. It is serialized using pickle and stored for 3 days, after which it is automatically deleted. The lifetime can be changed in the configuration via the DEFAULT_STORAGE_FILES_TTL parameter.
Pros:
- Can store any Python objects.
- No size limitations.
- Data is not visible in
callback_data. - Easily accessible in the handler.
Cons:
- Takes up disk space.
- Harder to debug.
- Data is stored for a limited time.
Example:
buttons = Buttons([
[
Button(
'Private button with a secret',
callback_data='private',
extra_data={'secret': 8}
)
]
])
@mod.callback('private')
async def _private(c: C):
if c.from_user.id != app.me.id: # make the button private, only for yourself
return await c.answer("This is not your button!", True)
secret = c.extra_data.get('secret')
await c.answer(f"Your secret: {secret}")
Using callback_data
You can also pass data through callback_data. Simply embed it directly into the callback text, separating it with a colon (:), for example.
Pros:
- Data is stored indefinitely.
- Easy to debug.
- Convenient for passing small data, such as IDs.
- Does not clutter memory.
Cons:
- Only strings can be passed.
- Strong length limitation. (The maximum length of the entire callback data is calculated using the formula
64-(module name length)-12characters). - Data is visible to others.
- You will need to extract this data yourself.
Example:
my_id = 1234567890
buttons = Buttons([
[
Button(
'Pass ID in callback data',
callback_data=f'id:{my_id}',
)
]
])
# Check the start of the callback data
@mod.callback(startswith='id')
async def _id(c: C):
my_id = int(c.data.split(':')[-1])
await c.answer(f"Your ID: {my_id}")
Modifying Messages and Buttons
Modifying Message Text
A message can be modified without sending a new one by replacing its content and buttons.
Telegram quirk: When modifying text, you must resend the buttons, or they will disappear.
Text modification is done using standard Kurigram methods, but you need to prepare the buttons first using the prepare_buttons method. Read more about the method here.
buttons = await mod.prepare_buttons(buttons)
await c.edit_message_text(new_text, reply_markup=buttons)
Modifying Buttons
You can modify not only the message text but also the buttons themselves.
Button modification is done using standard Kurigram methods, but you need to prepare the buttons first using the prepare_buttons method. Read more about the method here.
new_buttons = await mod.prepare_buttons(new_buttons)
await c.edit_message_reply_markup(new_buttons)
Private Buttons
Sometimes you need to make a button private so that no one else can press it.
This is implemented with a simple ID check.
@mod.callback('private')
async def _private(c: C):
if c.from_user.id != app.me.id: # check if the client's ID matches the ID of the person pressing the button
return await c.answer("This is not your button!", True)