diff --git a/CHANGELOG.md b/CHANGELOG.md index 127362d..8b656db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ ### Documentation - - Add docstrings to `BaseHandler` + - Add docstrings to `BaseHandler` ([455a0a94977c1652b445b623df561f9606a0615e](https://github.com/hearot/pyrubrum/commit/455a0a94977c1652b445b623df561f9606a0615e)) - Add docstrings to `BaseMenu` ([3a7ad4e3b23bfb85a9faa8c3c056e76abb87b771](https://github.com/hearot/pyrubrum/commit/3a7ad4e3b23bfb85a9faa8c3c056e76abb87b771)) - Add issue templates ([64c006258e373a97d0a9f83318af02c4310b585b](https://github.com/hearot/pyrubrum/commit/64c006258e373a97d0a9f83318af02c4310b585b)) - Add the official pronunciation for Pyrubrum ([b6d1fe8e01f79007338cd4a6dfe409c406c12cba](https://github.com/hearot/pyrubrum/commit/b6d1fe8e01f79007338cd4a6dfe409c406c12cba)) @@ -33,6 +33,7 @@ - Include the release dates inside changelog ([d4397f396c64eeec6549cd90c64cef6359017dd5](https://github.com/hearot/pyrubrum/commit/d4397f396c64eeec6549cd90c64cef6359017dd5)) - Integrate Dependabot ([2bb0a43b0f7f8266c7d0544209194c2b26b878a4](https://github.com/hearot/pyrubrum/commit/2bb0a43b0f7f8266c7d0544209194c2b26b878a4)) + - Make parameters optional for `on_callback` and `on_message` ### Testing changes diff --git a/pyrubrum/base_menu.py b/pyrubrum/base_menu.py index ffa6a5c..3474f1f 100644 --- a/pyrubrum/base_menu.py +++ b/pyrubrum/base_menu.py @@ -21,6 +21,7 @@ from dataclasses import dataclass from typing import Any from typing import Dict +from typing import Optional from typing import Union from pyrogram import CallbackQuery @@ -36,7 +37,7 @@ @dataclass(eq=False, init=False, repr=True) class BaseMenu(ABC): """Basic represention of a menu, which is an entity that has got by definition - a name and a unique identifier. + at least a name and a unique identifier. The purpose of this class is to give a general interface for a menu, as it doesn't implement anything except for the generation of both buttons and @@ -172,7 +173,7 @@ def on_callback( handler: BaseHandler, client: Client, callback: CallbackQuery, - parameters: Dict[str, Any], + parameters: Optional[Dict[str, Any]] = None, ): """This abstract method is intended to be implemented and is called whenever a callback query is handled and refers to this menu. @@ -183,8 +184,8 @@ def on_callback( client (Client): The client which is linked to the handler. context (Union[CallbackQuery, Message]): The context which the button is generated for. - parameters (Dict[str, Any]): The parameters which were passed to - to the handler. + parameters (Optional[Dict[str, Any]]): The parameters which were + passed to to the handler. Defaults to None. """ raise NotImplementedError @@ -194,7 +195,7 @@ def on_message( handler: BaseHandler, client: Client, message: Message, - parameters: Dict[str, Any], + parameters: Optional[Dict[str, Any]] = None, ): """This abstract method is intended to be implemented and is called whenever a message is handled and refers to this menu. @@ -205,7 +206,7 @@ def on_message( client (Client): The client which is linked to the handler. context (Union[CallbackQuery, Message]): The context which the button is generated for. - parameters (Dict[str, Any]): The parameters which were passed to - to the handler. + parameters (Optional[Dict[str, Any]]): The parameters which were + passed to to the handler. Defaults to None. """ raise NotImplementedError diff --git a/pyrubrum/menu.py b/pyrubrum/menu.py index 6fffe27..5161496 100644 --- a/pyrubrum/menu.py +++ b/pyrubrum/menu.py @@ -96,8 +96,11 @@ def on_callback( tree: Handler, client: Client, callback: CallbackQuery, - parameters: Dict[str, Any], + parameters: Optional[Dict[str, Any]] = None, ): + if not parameters: + parameters = {} + self.preliminary(tree, client, callback, parameters) content = self.get_content(tree, client, callback, parameters) @@ -153,8 +156,15 @@ def keyboard( elif isinstance(context, CallbackQuery): return Keyboard(keyboard, tree, context.id) if keyboard else None - def on_message(self, tree: Handler, client: Client, message: Message): - parameters = {} + def on_message( + self, + tree: Handler, + client: Client, + message: Message, + parameters: Optional[Dict[str, Any]] = None, + ): + if not parameters: + parameters = {} self.preliminary(tree, client, message, parameters) content = self.get_content(tree, client, message, parameters)