Skip to content

Commit

Permalink
feat: make parameters optional for on_callback and on_message
Browse files Browse the repository at this point in the history
  • Loading branch information
hearot committed Jun 10, 2020
1 parent 455a0a9 commit 562f323
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down
15 changes: 8 additions & 7 deletions pyrubrum/base_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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.
Expand All @@ -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
16 changes: 13 additions & 3 deletions pyrubrum/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 562f323

Please sign in to comment.