ParameterizedBaseHandler

class pyrubrum.ParameterizedBaseHandler(database: pyrubrum.database.base_database.BaseDatabase)

Basic implementation of an handler which has got, by definition, a database, with which it is able to perform parameterization (i.e. it supports parameters).

The purpose of this class is to give a general interface for an handler which supports parameterization, as it doesn’t implement anything except for a basic setup of the client (see setup) and a filter that allows the recognition of parameterized queries (see filter).

Parameters

database (BaseDatabase) – The storage for all the query parameters. It is used to pass parameters between menus.

filter(menu_id: str) → pyrogram.client.filters.filter.Filter

Generate a function which is used by CallbackQueryHandler in order to filter callback queries relying on the content of the provided menu identifier.

The content of the callback query is always a MD5 hash which behaves as the key for the parameters that are associated to the query the user made.

If the identifier of the chat from which the query was sent does not match the one defined in the retrieved parameters, the callback is considered invalid.

The filter returns True if the query is valid and matches menu_id. Otherwise, it returns False.

Parameters

menu_id (str) – The unique identifier of the menu that has to be matched.

Returns

The function that gets called whenever a callback query is being handled.

Return type

Filter

process_keyboard(keyboard: List[List[pyrubrum.keyboard.button.Button]], callback_query_id: str, chat_id: int) → List[List[pyrogram.client.types.bots_and_keyboards.inline_keyboard_button.InlineKeyboardButton]]

Given a list which represents an inline keyboard and a unique identifier for the callback, generate a Pyrogram-compatible inline keyboard.

It generates a MD5 hash key for each button by following this pattern:

[CALLBACK_QUERY_ID][MENU_ID][ELEMENT_ID]

After having generated a key, it sets it to be equal, inside the database, to the parameters of the button, which have been previously converted to JSON and include from_chat_id, the identifier of the chat from which the query was sent, element_id, menu_id and same_menu.

Parameters
  • keyboard (List[List[Button]]) – The inline keyboard you want to process.

  • callback_query_id (str) – The unique identifier of the callback for which the keyboard is generated.

  • chat_id (int) – The identifier of the chat from which the query has been sent.

Returns

The generated keyboard in a Pyrogram-compatible type.

Return type

List[List[pyrogram.InlineKeyboardButton]]

setup(client: pyrogram.client.client.Client)

Make all the defined menus reachable by the client by adding handlers that catch all their identifiers to it. It adds support to parameterization by applying ParameterizedBaseHandler.filter to all the handled callback queries. It also calls pass_parameterized_handler, which lets the callback functions get this handler as argument and deletes handled callback queries from the database relying on the passed identifiers.

Parameters

client (Client) – The client which is being set up.

Warning

The functions the handlers make use of are not set up in the same way objects added using Pyrogram handlers are. Pyrubrum implements the following pattern:

callback(handler, client, context, parameters)