Keyboards and callbacks¶
Inline keyboards are ReplyMarkupInlineKeyboard. A callback button’s data is bytes (TDLib max 64).
from pytdbot import Client, types
await message.reply_text(
"Pick",
reply_markup=types.ReplyMarkupInlineKeyboard(
rows=[[
types.InlineKeyboardButton(
text="OK",
type=types.InlineKeyboardButtonTypeCallback(data=b"ok"),
),
]]
),
)
@client.on_updateNewCallbackQuery()
async def on_cb(c: Client, update: types.UpdateNewCallbackQuery):
# update.text is the payload decoded as UTF-8 ("ok")
await update.answer(text="done")
update.answer acknowledges the press. update.text is the raw bytes as a string.
To pack a Python object instead of raw bytes, use utils.callback_data (JSON, still max 64 bytes). Read it back with load_callback_data or update.callback_data:
from pytdbot import utils
data = utils.callback_data("ok", {"id": 1})
# types.InlineKeyboardButtonTypeCallback(data=data)
@client.on_updateNewCallbackQuery()
async def on_cb(c: Client, update: types.UpdateNewCallbackQuery):
cb = update.callback_data
# cb.action == "ok", cb.data == {"id": 1}
await update.answer(text="done")
See also InlineKeyboardButton and InlineKeyboardButtonTypeCallback.
Rich messages
If you are sending a rich message, use utils.tg_button instead of ReplyMarkupInlineKeyboard.