Skip to content

Commit 1f01e48

Browse files
committed
added function causing multiple actions
1 parent e61ff20 commit 1f01e48

File tree

2 files changed

+66
-57
lines changed

2 files changed

+66
-57
lines changed

bot/__init__.py

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,24 @@ def check_missing_setup(user):
113113
@staticmethod
114114
def is_allowed_update(missing, trigger, state, query_action, command):
115115
if any((
116-
missing is None, # No missing setup
117-
missing == "user", # allow user setup
118-
trigger == "text" and command == "/start", # Always allow /start
119-
all(( # Allow language selection
120-
missing == "lang",
121-
trigger == "callback_query",
122-
query_action == QUERY_ACTIONS.LANGUAGE_CHOSEN.value
123-
)),
124-
all(( # Allow vocabulary creation
125-
missing == "vocabulary",
126-
trigger == "text",
127-
state == USER_STATES.CREATE_VOCABULARY.value
128-
)),
129-
all(( # Allow timezone setup
130-
missing == "timezone",
131-
trigger == "callback_query",
132-
query_action in {QUERY_ACTIONS.PICK_TIME.value, QUERY_ACTIONS.CHANGE_TIMEZONE_FINALIZE.value}
133-
)),
116+
missing is None, # No missing setup
117+
missing == "user", # allow user setup
118+
trigger == "text" and command == "/start", # Always allow /start
119+
all(( # Allow language selection
120+
missing == "lang",
121+
trigger == "callback_query",
122+
query_action == QUERY_ACTIONS.LANGUAGE_CHOSEN.value
123+
)),
124+
all(( # Allow vocabulary creation
125+
missing == "vocabulary",
126+
trigger == "text",
127+
state == USER_STATES.CREATE_VOCABULARY.value
128+
)),
129+
all(( # Allow timezone setup
130+
missing == "timezone",
131+
trigger == "callback_query",
132+
query_action in {QUERY_ACTIONS.PICK_TIME.value, QUERY_ACTIONS.CHANGE_TIMEZONE_FINALIZE.value}
133+
)),
134134
)):
135135
return True
136136
return False
@@ -153,6 +153,51 @@ def set_up(self, missing, update):
153153
case _:
154154
raise ValueError("Unrecognized missing setup stage")
155155

156+
def execute_action(self, user, action, function=None, update=None, callback_query_id=None, msg_id=None, text=None,
157+
reply_markup=None, lang=None, add_cancel_button=False, answer_callback_query=True):
158+
# not answering callback query leads to long waiting animation, but some actions don't require it
159+
if callback_query_id and answer_callback_query and action not in {"edit", "edit_markup"}:
160+
self.answerCallbackQuery(callback_query_id)
161+
162+
result = function(update) if function else None
163+
164+
match action:
165+
case "send":
166+
if add_cancel_button:
167+
text, lang = result if result else (text, lang)
168+
self.deliver_message(user, text, add_cancel_button=True, lang=lang)
169+
else:
170+
text, reply_markup = result if result else (text, reply_markup)
171+
self.deliver_message(user, text, reply_markup=reply_markup)
172+
173+
case "edit":
174+
if not msg_id:
175+
raise ValueError("Missing parameter: msg_id for editing message")
176+
177+
text, reply_markup = result if result else (text, reply_markup)
178+
self.editMessageText((user, msg_id), text, parse_mode="HTML", reply_markup=reply_markup)
179+
180+
case "edit_markup":
181+
if not msg_id:
182+
raise ValueError("Missing parameter: msg_id for editing reply markup")
183+
184+
reply_markup = result if result else reply_markup
185+
self.editMessageReplyMarkup((user, msg_id), reply_markup=reply_markup)
186+
187+
case "popup":
188+
raise NotImplemented("Popup action is not yet implemented.")
189+
190+
case "multi_action":
191+
if not isinstance(result, dict):
192+
raise ValueError(f"multi_action must return a dict, got {type(result).__name__} instead.")
193+
self.execute_action(user, **result)
194+
195+
case None:
196+
pass # function has been executed at the beginning and no additional actions are required
197+
198+
case _:
199+
raise ValueError(f"Unknown action {action}")
200+
156201
def handle_update(self, update):
157202
user = None
158203
try:
@@ -167,6 +212,7 @@ def handle_update(self, update):
167212
command = None
168213
state = None
169214
query_action = None
215+
msg_id = None
170216
if "message" in update:
171217
if "text" in update["message"]:
172218
text = update["message"]["text"]
@@ -196,44 +242,7 @@ def handle_update(self, update):
196242
return
197243

198244
function, action, cancel_button = get_route(trigger, state, query_action, command)
199-
match action:
200-
case "send":
201-
if callback_query_id:
202-
self.answerCallbackQuery(callback_query_id)
203-
204-
if cancel_button:
205-
text, lang = function(update)
206-
self.deliver_message(user, text, add_cancel_button=True, lang=lang)
207-
else:
208-
text, reply_markup = function(update)
209-
self.deliver_message(user, text, reply_markup=reply_markup)
210-
211-
case "edit": # editing message doesn't require answering callback_query
212-
if trigger == "callback_query":
213-
text, reply_markup = function(update)
214-
self.editMessageText((user, msg_id), text, parse_mode="HTML", reply_markup=reply_markup)
215-
else:
216-
raise ValueError("Action is set to edit, but not triggered by callback query, so no msg_id")
217-
218-
case "edit_markup": # editing message reply markup doesn't require answering callback_query
219-
if trigger == "callback_query":
220-
reply_markup = function(update)
221-
self.editMessageReplyMarkup((user, msg_id), reply_markup=reply_markup)
222-
else:
223-
raise ValueError("Action is set to edit, but not triggered by callback query, so no msg_id")
224-
225-
case "popup":
226-
if callback_query_id:
227-
self.answerCallbackQuery(callback_query_id)
228-
raise NotImplemented
229-
230-
case None:
231-
if callback_query_id:
232-
self.answerCallbackQuery(callback_query_id)
233-
function(update)
234-
235-
case _:
236-
raise ValueError(f"Unknown action {action}")
245+
self.execute_action(user, action, function, update, callback_query_id, msg_id)
237246

238247
if was_missing:
239248
is_missing = self.check_missing_setup(user)

router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
TRIGGERS = ("text", "callback_query", "chat_member", "other")
9-
ACTIONS = ("send", "edit", "edit_markup", "popup", None)
9+
ACTIONS = ("send", "edit", "edit_markup", "popup", "multi_action", None)
1010
routes = {}
1111

1212

0 commit comments

Comments
 (0)