Skip to content

API Reference parrot.integrations.telegram.callbacks

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.integrations.telegram.callbacks

View source

Telegram Callback Decorators.

Provides @telegram_callback decorator for agents to register inline keyboard callback handlers, following the same pattern as @telegram_command for commands.

Usage on an Agent:

class JiraSpecialist(Agent):

    @telegram_callback(
        prefix="ticket_select",
        description="Handle ticket selection from daily standup"
    )
    async def on_ticket_selected(self, callback: CallbackContext) -> CallbackResult:
        ticket_key = callback.payload["ticket_key"]
        await self.transition_ticket(ticket_key, "In Progress")
        return CallbackResult(
            answer_text=f"✅ {ticket_key} → In Progress",
            edit_message=f"✅ Ticket *{ticket_key}* moved to *In Progress*. Let's get to work! 💪"
        )

Class CallbackContext

Context object passed to @telegram_callback handlers.

Parameter Type Default Description
prefix str
payload Dict[str, Any]
chat_id int
user_id int
message_id int
username Optional[str] None
first_name Optional[str] None
raw_query Optional['CallbackQuery'] None

Fields:

Field Type Default Description
prefix str The callback prefix that matched.
payload Dict[str, Any] Decoded payload dict from callback_data.
chat_id int Telegram chat ID.
user_id int Telegram user ID who clicked the button.
message_id int Message ID of the message containing the button.
username Optional[str] None Telegram username (if available).
first_name Optional[str] None
raw_query Optional['CallbackQuery'] None The original aiogram CallbackQuery object.

Attributes:

Attribute Type Description
display_name str Best available display name for the user.

Class CallbackData

Encode/decode callback_data for Telegram InlineKeyboardButtons.

Format: prefix:json_payload

Telegram limits callback_data to 64 bytes, so payloads must be compact. Use short keys and values.

Method CallbackData.encode

Encode a prefix + payload into callback_data string.

Raises ValueError if result exceeds 64 bytes.

encode(prefix: str, payload: Dict[str, Any]) -> str

Parameter Type Default Description
prefix str
payload Dict[str, Any]

Returns: str

Method CallbackData.decode

Decode callback_data into (prefix, payload).

decode(data: str) -> tuple[str, Dict[str, Any]]

Parameter Type Default Description
data str

Returns: tuple[str, Dict[str, Any]] — Tuple of (prefix_string, payload_dict). If there's no payload, returns (prefix, {}).

Class CallbackMetadata

Metadata stored on a method decorated with @telegram_callback.

Parameter Type Default Description
prefix str
description str
method Callable[..., Awaitable[CallbackResult]]
method_name str

Fields:

Field Type Default Description
prefix str
description str
method Callable[..., Awaitable[CallbackResult]]
method_name str

Class CallbackRegistry

Discovers and stores @telegram_callback handlers from an agent.

Used by TelegramAgentWrapper to route incoming CallbackQuery updates to the correct agent method.

Attributes:

Attribute Type Description
prefixes List[str] List all registered prefixes.
handlers Dict[str, CallbackMetadata] All registered handlers.

Method CallbackRegistry.discover_from_agent

Scan an agent instance for methods decorated with @telegram_callback.

discover_from_agent(agent: Any) -> int

Parameter Type Default Description
agent Any

Returns: int — Number of callbacks discovered.

Method CallbackRegistry.register

Programmatically register a callback handler.

register(prefix: str, handler: Callable, description: str = '') -> None

Parameter Type Default Description
prefix str
handler Callable
description str ''

Method CallbackRegistry.get_handler

Get handler metadata by prefix.

get_handler(prefix: str) -> Optional[CallbackMetadata]

Parameter Type Default Description
prefix str

Returns: Optional[CallbackMetadata]

Method CallbackRegistry.match

Match callback_data against registered prefixes.

match(callback_data: str) -> Optional[tuple[CallbackMetadata, Dict[str, Any]]]

Parameter Type Default Description
callback_data str

Returns: Optional[tuple[CallbackMetadata, Dict[str, Any]]] — Tuple of (CallbackMetadata, payload_dict) or None.

Class CallbackResult

Result returned by a @telegram_callback handler.

All fields are optional — the wrapper will apply whichever are set.

Parameter Type Default Description
answer_text Optional[str] None
show_alert bool False
edit_message Optional[str] None
edit_parse_mode str 'Markdown'
reply_text Optional[str] None
reply_parse_mode str 'Markdown'
reply_markup Optional[Any] None
remove_keyboard bool False

Fields:

Field Type Default Description
answer_text Optional[str] None Toast notification shown to the user (up to 200 chars).
show_alert bool False If True, answer_text is shown as a modal alert instead of toast.
edit_message Optional[str] None Replace the original message text with this.
edit_parse_mode str 'Markdown' Parse mode for edit_message (default: Markdown).
reply_text Optional[str] None Send a new message as reply to the callback message.
reply_parse_mode str 'Markdown' Parse mode for reply_text.
reply_markup Optional[Any] None New InlineKeyboardMarkup to replace the current one. Set to None (default) to keep existing, or pass an empty dict/markup to remove buttons.
remove_keyboard bool False If True, removes inline keyboard from original message.

Function build_inline_keyboard

Build an InlineKeyboardMarkup dict compatible with aiogram.

build_inline_keyboard(buttons: List[List[Dict[str, Any]]]) -> Dict[str, Any]

Parameter Type Default Description
buttons List[List[Dict[str, Any]]] 2D list of button dicts (rows × columns).

Returns: Dict[str, Any] — InlineKeyboardMarkup-compatible dict.

Function telegram_callback

Decorator to register an agent method as a Telegram inline callback handler.

The method will be called when a user clicks an InlineKeyboardButton whose callback_data starts with the given prefix.

telegram_callback(prefix: str, description: str = '')

Parameter Type Default Description
prefix str Unique prefix string that identifies this callback. Must be short (recommended ≤8 chars) to leave room for payload within Telegram's 64-byte callback_data limit.
description str '' Human-readable description of what this callback does.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally