-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinline_keyboard.py
More file actions
123 lines (93 loc) · 3.19 KB
/
Copy pathinline_keyboard.py
File metadata and controls
123 lines (93 loc) · 3.19 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import dataclasses
import enum
import typing
from telegrinder import (
API,
CallbackQuery,
Message,
Telegrinder,
Token,
)
from telegrinder.modules import configure_dotenv, setup_logger
from telegrinder.node import ChatId
from telegrinder.rules import PayloadModelRule, Text
from telegrinder.tools import MsgPackSerializer
from telegrinder.tools.keyboard import InlineButton, InlineKeyboard
configure_dotenv()
setup_logger()
api = API(token=Token.from_env())
bot = Telegrinder(api)
@enum.unique
class MenuAction(enum.Enum):
show_fact = enum.auto()
show_store = enum.auto()
@enum.unique
class StoreAction(enum.Enum):
buy_coffee = enum.auto()
buy_tea = enum.auto()
back_to_menu = enum.auto()
@dataclasses.dataclass(slots=True, frozen=True)
class MenuCallback:
__key__ = "menu"
__serializer__ = MsgPackSerializer[typing.Self]
action: MenuAction
@dataclasses.dataclass(slots=True, frozen=True)
class StoreCallback:
__key__ = "store"
__serializer__ = MsgPackSerializer[typing.Self]
action: StoreAction
item: str = ""
price: int = 0
class MainMenuKeyboard(InlineKeyboard):
show_fact = InlineButton(
"🎯 Show Random Fact",
callback_data=MenuCallback(action=MenuAction.show_fact),
new_row=True,
)
show_store = InlineButton(
"🛒 Open Store",
callback_data=MenuCallback(action=MenuAction.show_store),
)
class StoreKeyboard(InlineKeyboard):
buy_coffee = InlineButton(
"☕ Coffee - $3",
callback_data=StoreCallback(action=StoreAction.buy_coffee, item="Coffee", price=3),
new_row=True,
)
buy_tea = InlineButton(
"🍵 Tea - $2",
callback_data=StoreCallback(action=StoreAction.buy_tea, item="Tea", price=2),
new_row=True,
)
back_to_menu = InlineButton(
"⬅️ Back to Menu",
callback_data=StoreCallback(action=StoreAction.back_to_menu),
)
@bot.on.message(Text("/start"))
async def start(message: Message) -> None:
await message.answer(
text="hi there. pick an option below:",
reply_markup=MainMenuKeyboard.get_markup(),
)
@bot.on.callback_query(StoreKeyboard.back_to_menu)
async def back_menu(cb: CallbackQuery, chat_id: ChatId) -> None:
if cb.message_cute:
await cb.message_cute.unwrap().delete()
await cb.api.send_message(
chat_id=chat_id,
text="hi there. pick an option below:",
reply_markup=MainMenuKeyboard.get_markup(),
)
await cb.answer()
@bot.on.callback_query(MainMenuKeyboard.show_fact)
async def interest_fact(cb: CallbackQuery) -> None:
await cb.answer("brace yourself: he comes again", show_alert=True)
@bot.on.callback_query(MainMenuKeyboard.show_store)
async def store(cb: CallbackQuery) -> None:
await cb.edit_text("what do u want?", reply_markup=StoreKeyboard.get_markup())
await cb.answer()
@bot.on.callback_query(PayloadModelRule(StoreCallback, alias="my_model"))
async def view_store(cb: CallbackQuery, chat_id: ChatId, my_model: StoreCallback) -> None:
await cb.api.send_message(chat_id=chat_id, text=f"your choice - {my_model.item}; price - {my_model.price}")
await cb.answer()
bot.run_forever(skip_updates=True)