Types¶
The data models, enums, and exceptions that underpin the library. All models are built on Pydantic v2.
telegram_menu_builder.types ¶
Core type definitions for the menu builder library.
This module contains all the fundamental data structures used throughout the library, implemented with Pydantic v2 for validation and type safety.
HandlerFunc
module-attribute
¶
Type alias for async handler functions.
A handler is an async callable that receives the Telegram Update, the bot
Context, and the dict of decoded parameters from the callback data, and
returns None.
StorageStrategy ¶
Bases: StrEnum
Strategy for storing callback data based on size and persistence requirements.
Attributes:
| Name | Type | Description |
|---|---|---|
INLINE |
Store directly in callback_data (< 60 bytes) |
|
SHORT |
Store in temporary storage with TTL (60-500 bytes) |
|
PERSISTENT |
Store in permanent storage (> 500 bytes or long-lived) |
Source code in src/telegram_menu_builder/types.py
MenuAction ¶
Bases: BaseModel
Represents an action to be executed when a menu item is selected.
This class handles the encoding and decoding of callback data, intelligently choosing the appropriate storage strategy based on data size.
Attributes:
| Name | Type | Description |
|---|---|---|
handler |
str
|
Name of the handler function to call |
params |
dict[str, Any]
|
Dictionary of parameters to pass to the handler |
strategy |
StorageStrategy | None
|
Storage strategy (auto-selected if None) |
ttl |
int
|
Time-to-live in seconds for SHORT strategy storage |
Example
action = MenuAction( ... handler="edit_user", ... params={"user_id": 123, "field": "email"} ... )
Source code in src/telegram_menu_builder/types.py
validate_handler_name
classmethod
¶
Validate handler name follows Python identifier rules.
Source code in src/telegram_menu_builder/types.py
validate_params_serializable
classmethod
¶
Validate that params contain only JSON-serializable values.
Source code in src/telegram_menu_builder/types.py
MenuItem ¶
Bases: BaseModel
Represents a single item in an inline keyboard menu.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
Display text for the button |
callback_data |
str | None
|
Encoded callback data (max 64 bytes for Telegram) |
url |
str | None
|
Optional URL for URL buttons |
Example
item = MenuItem( ... text="⚙️ Settings", ... callback_data="encoded_data_here" ... )
Source code in src/telegram_menu_builder/types.py
validate_callback_size
classmethod
¶
Ensure callback_data doesn't exceed Telegram's 64-byte limit.
Source code in src/telegram_menu_builder/types.py
to_telegram_button ¶
Convert to telegram.InlineKeyboardButton.
Returns:
| Type | Description |
|---|---|
InlineKeyboardButton
|
InlineKeyboardButton instance ready for use |
Source code in src/telegram_menu_builder/types.py
LayoutConfig ¶
Bases: BaseModel
Configuration for menu layout and button arrangement.
Attributes:
| Name | Type | Description |
|---|---|---|
columns |
int
|
Number of columns in the grid layout |
max_rows |
int | None
|
Maximum number of rows (None for unlimited) |
fill_last_row |
bool
|
Whether to fill the last row or center items |
button_width_balance |
bool
|
Try to balance button widths in same row |
Example
layout = LayoutConfig(columns=3, max_rows=5)
Source code in src/telegram_menu_builder/types.py
NavigationButton ¶
Bases: BaseModel
Configuration for a navigation button (back, next, exit).
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
Button text (emoji + label) |
handler |
str
|
Handler to call when clicked |
params |
dict[str, Any]
|
Additional parameters for the handler |
position |
Literal['top', 'bottom', 'inline']
|
Where to place the button |
Source code in src/telegram_menu_builder/types.py
NavigationConfig ¶
Bases: BaseModel
Configuration for navigation buttons (back, next, exit).
Attributes:
| Name | Type | Description |
|---|---|---|
back_button |
NavigationButton | None
|
Back button configuration |
next_button |
NavigationButton | None
|
Next button configuration |
exit_button |
NavigationButton | None
|
Exit button configuration |
cancel_button |
NavigationButton | None
|
Cancel button configuration |
Example
nav = NavigationConfig( ... back_button=NavigationButton( ... text="🔙 Back", ... handler="go_back" ... ) ... )
Source code in src/telegram_menu_builder/types.py
validate_exclusive_buttons ¶
Ensure exit and cancel buttons are not used together.
Source code in src/telegram_menu_builder/types.py
CallbackData ¶
Bases: BaseModel
Complete callback data structure for menu interactions.
This is the internal representation of all data associated with a callback. It gets encoded into the callback_data string or stored externally.
Attributes:
| Name | Type | Description |
|---|---|---|
action |
MenuAction
|
The menu action to execute |
menu_id |
str | None
|
Unique identifier for the menu instance |
timestamp |
float | None
|
When the callback data was created |
metadata |
dict[str, Any]
|
Additional metadata (breadcrumb, context, etc.) |
Example
callback = CallbackData( ... action=MenuAction(handler="test", params={"id": 1}), ... menu_id="main_menu" ... )
Source code in src/telegram_menu_builder/types.py
MenuBuilderError ¶
EncodingError ¶
Bases: MenuBuilderError
Raised when callback data encoding fails.
DecodingError ¶
Bases: MenuBuilderError
Raised when callback data decoding fails.
StorageError ¶
Bases: MenuBuilderError
Raised when storage operations fail.
ValidationError ¶
Bases: MenuBuilderError
Raised when menu validation fails.