From 2cfe939979decd35094f4fdb6b4b6d64b175de75 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivanko Date: Thu, 1 Feb 2024 15:38:10 +0300 Subject: [PATCH] demos: add dynamic_keyboard demo --- demos/dynamic_keyboard/README.md | 7 ++++ demos/dynamic_keyboard/__init__.py | 3 ++ demos/dynamic_keyboard/demo.py | 59 ++++++++++++++++++++++++++++++ demos/dynamic_keyboard/settings.py | 5 +++ 4 files changed, 74 insertions(+) create mode 100644 demos/dynamic_keyboard/README.md create mode 100644 demos/dynamic_keyboard/__init__.py create mode 100644 demos/dynamic_keyboard/demo.py create mode 100644 demos/dynamic_keyboard/settings.py diff --git a/demos/dynamic_keyboard/README.md b/demos/dynamic_keyboard/README.md new file mode 100644 index 00000000..68b83945 --- /dev/null +++ b/demos/dynamic_keyboard/README.md @@ -0,0 +1,7 @@ +# Dynamic keyboard demo + +To run the demo use the following command: + +```bash +env PYTHONPATH=$(pwd) HAMMETT_SETTINGS_MODULE=demos.dynamic_keyboard.settings TOKEN=your-token python3 demos/dynamic_keyboard/demo.py +``` diff --git a/demos/dynamic_keyboard/__init__.py b/demos/dynamic_keyboard/__init__.py new file mode 100644 index 00000000..eb5bf0ba --- /dev/null +++ b/demos/dynamic_keyboard/__init__.py @@ -0,0 +1,3 @@ +"""The module contains a bot that is intended to demonstrate how to +create and work with a dynamic keyboard. +""" diff --git a/demos/dynamic_keyboard/demo.py b/demos/dynamic_keyboard/demo.py new file mode 100644 index 00000000..ebeeae6b --- /dev/null +++ b/demos/dynamic_keyboard/demo.py @@ -0,0 +1,59 @@ +"""The module is a script to run the bot.""" + +from hammett.core import Button +from hammett.core.application import Application +from hammett.core.constants import DEFAULT_STATE, RenderConfig, SourcesTypes +from hammett.core.handlers import register_button_handler +from hammett.core.mixins import StartMixin + + +async def request_dynamic_keyboard(handler): # noqa: RUF029 + """Emulate an API request, pass a payload and return the keyboard.""" + buttons = range(3) # do some API request + + return [ + [Button( + f'Button {button_num + 1}', + handler, + source_type=SourcesTypes.HANDLER_SOURCE_TYPE, + payload=f"You pressed button number {button_num + 1}, I'm right?", + )] + for button_num in buttons + ] + + +class MainMenu(StartMixin): + """The class implements Main menu screen.""" + + description = 'Just press any button' + + async def add_default_keyboard(self, _update, _context): + """Set up the default keyboard for the screen.""" + return await request_dynamic_keyboard(self.handle_button_click) + + @register_button_handler + async def handle_button_click(self, update, context): + """Handle a button click by getting text from the payload and + passing it as a new description. + """ + payload = await self.get_payload(update, context) + + await self.render(update, context, config=RenderConfig(description=payload)) + return DEFAULT_STATE + + +def main(): + """Run the bot.""" + name = 'dynamic_keyboard' + app = Application( + name, + entry_point=MainMenu, + states={ + DEFAULT_STATE: [MainMenu], + }, + ) + app.run() + + +if __name__ == '__main__': + main() diff --git a/demos/dynamic_keyboard/settings.py b/demos/dynamic_keyboard/settings.py new file mode 100644 index 00000000..63c1062e --- /dev/null +++ b/demos/dynamic_keyboard/settings.py @@ -0,0 +1,5 @@ +"""The module contains the settings of the demo.""" + +import os + +TOKEN = os.getenv('TOKEN', '')