pyrubrum Logo
latest

Quickstart

  • Installation
  • Writing your first bot

Examples

  • Environment variables
  • Café Bot
  • Calendar Bot
  • Hitchhiker Bot
  • Sample Bot

API Reference

  • Database
  • Database Errors
  • Handlers
  • Keyboard
  • Menus
    • BaseMenu
    • DeepLinkMenu
    • LinkMenu
    • Menu
    • PageMenu
  • Objects
  • Styles
  • Tree
  • Types

Meta

  • Assets
  • Changelog
  • Code of Conduct
  • Contributing
  • Flowchart guidelines
  • Glossary
  • License
  • Read me
  • Security Policy
pyrubrum
  • Docs »
  • Menus »
  • Menu
  • Edit on GitHub

Menu¶

class pyrubrum.Menu(name: str, menu_id: str, content: Union[pyrogram.client.types.input_media.input_media.InputMedia, str, Callable[[Handler, pyrogram.client.client.Client, Union[pyrogram.client.types.bots_and_keyboards.callback_query.CallbackQuery, pyrogram.client.types.messages_and_media.message.Message], Dict[str, Any]], Union[pyrogram.client.types.input_media.input_media.InputMedia, str]]], deep_link: Optional[bool] = False, default: Optional[bool] = False, message_filter: Optional[pyrogram.client.filters.filter.Filter] = None, preliminary: Optional[Union[Callable[[Handler, pyrogram.client.client.Client, Any, Dict[str, Any]], None], List[Callable[[Handler, pyrogram.client.client.Client, Any, Dict[str, Any]], None]]]] = None, style: pyrubrum.menus.styles.base_style.BaseStyle = <pyrubrum.menus.styles.menu_style.MenuStyle object>, **kwargs)¶

Implementation of a menu who has got, by definition, a content to display (i.e. what the user will see by accessing it), a name and a style.

See also

For complete examples of styles you can make use of:

  • MenuStyle

  • PageStyle (although it is recommended to use it only with PageMenu)

Parameters
  • name (str) – The name you give to the menu, which will be used as the text of callback button, if needed. See BaseMenu for more information.

  • menu_id (str) – The unique identifier given to the menu, which will refer unequivocally to this entity. The hash for this class is generated relying on the content of this field. Avoid using 0 as it is used for buttons whose purpose is only related to design (i.e. they do not point to any menu, see Null-pointer button). See BaseMenu for more information.

  • content (Types.Content) –

    What will be displayed whenever a user accesses this menu. Both text and media can be provided. A function that returns such values can be provided as well and must follow the this arguments pattern:

    func(handler, client, context, parameters)
    

  • deep_link (Optional[bool]) – If this menu shall be reached by a deep-link whose payload is the identifier of this instance. Defaults to False.

  • default (Optional[bool]) – If this menu shall be displayed if no other top-level menu has been matched. It works only if this menu is a top-level one.

  • message_filter (Optional[Filter]) –

    A filter for telling Pyrogram when a message should be associated to this menu. It works only for top-level menus (see Handler.setup). Defaults to None, which automatically makes this menu reachable when the user texts a message that follows this pattern:

    /[MENU_ID]
    

  • preliminary (Types.Preliminary) – A function which is executed each time before doing anything else in on_callback and on_message. You can provide a list of such functions as well, which will be executed following the same order as the one of the list. Defaults to None, which means that no function is going to be executed.

  • style (BaseStyle) – The class which generates the keyboard for this function following a certain style. Defaults to MenuStyle().

Note

This implementation is compatible with a non-parameterized handler.

get_content(handler: Handler, client: pyrogram.client.client.Client, context: Union[pyrogram.client.types.bots_and_keyboards.callback_query.CallbackQuery, pyrogram.client.types.messages_and_media.message.Message], parameters: Optional[Dict[str, Any]] = None) → Union[pyrogram.client.types.input_media.input_media.InputMedia, str]¶

Get the content which will be displayed to the user.

Parameters
  • handler (BaseHandler) – The handler which coordinates the management of the menus.

  • client (Client) – The client which is linked to the handler.

  • context (Union[CallbackQuery, Message]) – The context for which the button is generated.

  • parameters (Optional[Dict[str, Any]]) – The parameters which were passed to the handler. Defaults to None.

Returns

The content of the menu, which is then displayed to the user as a media (if it is a subclass of pyrogram.InputMedia) or a message (if it is just a string).

Return type

Union[InputMedia, str]

on_callback(handler: Handler, client: pyrogram.client.client.Client, callback: pyrogram.client.types.bots_and_keyboards.callback_query.CallbackQuery, parameters: Optional[Dict[str, Any]] = None)¶

Each time a callback query is handled, this function calls the preliminary function (i.e. Menu.preliminary), if callable, then gets the content that is going to be provided to the user (both text and media are compatible), sets up an inline keyboard using the style defined in Menu.style and finally edits the message with CallbackQuery.edit_message_text (if the content is a string, i.e. a text) or CallbackQuery.edit_message_media (if the content is an instance of pyrogram.InputMedia, i.e. a media).

Parameters
  • handler (BaseHandler) – The handler which coordinates the management of the menus.

  • client (Client) – The client which is linked to the handler.

  • context (CallbackQuery) – The callback query for which the button is generated.

  • parameters (Optional[Dict[str, Any]]) – The parameters which were passed to the handler. Defaults to None.

on_message(handler: Handler, client: pyrogram.client.client.Client, message: pyrogram.client.types.messages_and_media.message.Message, parameters: Optional[Dict[str, Any]] = None)¶

Each time a message is handled, this function calls the preliminary function (i.e. Menu.preliminary), if callable, then gets the content that is going to be provided to the user (both text and media are compatible), sets up an inline keyboard using the style defined in Menu.style and finally sends the message with Message.reply_text (if the content is a string, i.e. a text) or Message.reply_cached_media (if the content is an instance of pyrogram.InputMedia, i.e. a media).

Parameters
  • handler (BaseHandler) – The handler which coordinates the management of the menus.

  • client (Client) – The client which is linked to the handler.

  • context (Message) – The message for which the button is generated.

  • parameters (Optional[Dict[str, Any]]) – The parameters which were passed to the handler. Defaults to None.

keyboard(handler: Handler, client: pyrogram.client.client.Client, context: Union[pyrogram.client.types.bots_and_keyboards.callback_query.CallbackQuery, pyrogram.client.types.messages_and_media.message.Message], parameters: Optional[Dict[str, Any]] = None) → pyrogram.client.types.bots_and_keyboards.inline_keyboard_markup.InlineKeyboardMarkup¶

Provide a keyboard using the chosen style in Menu.style.

Parameters
  • handler (BaseHandler) – The handler which coordinates the management of the menus.

  • client (Client) – The client which is linked to the handler.

  • context (Union[CallbackQuery, Message]) – The context for which the button is generated.

  • parameters (Dict[str, Any]) – The parameters which were passed to the handler.

Returns

The generated inline keyboard, which is then displayed to the user.

Return type

pyrogram.InlineKeyboardMarkup

Next Previous

© Copyright 2020, Hearot Revision 2539f70e.

Built with Sphinx using a theme provided by Read the Docs.