-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
291f558
commit 2cfe939
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""The module contains a bot that is intended to demonstrate how to | ||
create and work with a dynamic keyboard. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""The module contains the settings of the demo.""" | ||
|
||
import os | ||
|
||
TOKEN = os.getenv('TOKEN', '') |