-
Notifications
You must be signed in to change notification settings - Fork 0
/
menus.py
83 lines (67 loc) · 2.44 KB
/
menus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from typing import Callable, Optional, Union
from telegram import Bot, Chat, KeyboardButton, ReplyKeyboardMarkup, Update
from telegram.ext import CallbackContext
from prompts import BotPrompt, send_prompt
class BotMenu:
def __init__(
self,
text: str,
markup: ReplyKeyboardMarkup,
callbacks: list[
Union[
Callable[[Update, CallbackContext], None],
"BotMenu",
BotPrompt,
]
],
**kwargs,
) -> None:
"""
A simple menu that consists of a message and several ReplyKeyboard
buttons, where each button creates a callback when pressed. Callbacks can
be either a function, prompt or another menu.
"""
button_count = 0
for row in markup.keyboard:
for _ in row:
button_count += 1
if button_count != len(callbacks):
raise ValueError(
"Attempted to create BotMenu containing a"
"different amount of callbacks and buttons."
f"{button_count} buttons and {len(callbacks)} callbacks"
)
self.text = text
self.markup = markup
self.callbacks = callbacks
self.kwargs = kwargs
def send_menu(chat: Chat, chat_data: dict, menu: BotMenu):
"""Sends a "menu" to a chat, and defines callbacks for each answer."""
chat.send_message(text=menu.text, reply_markup=menu.markup, **menu.kwargs)
menu_callbacks = chat_data.get("menu_callbacks")
# Clear any previous callbacks
if menu_callbacks:
menu_callbacks.clear()
buttons = []
for row in menu.markup.keyboard:
for button in row:
if isinstance(button, KeyboardButton):
buttons.append(button.text)
else:
buttons.append(button)
menu_callbacks = {
button: callback for (button, callback) in zip(buttons, menu.callbacks)
}
def menu_listener(update: Update, context: CallbackContext):
"""Listens to buttons pressed in defined menus"""
menu_callbacks = context.chat_data.get("menu_callbacks")
if not menu_callbacks:
return
text = update.message.text
if text not in menu_callbacks:
return
callback = menu_callbacks[text]
if isinstance(callback, BotMenu):
send_menu(update.effective_chat, context.chat_data, callback)
elif isinstance(callback, BotPrompt):
send_prompt()