@@ -113,24 +113,24 @@ def check_missing_setup(user):
113
113
@staticmethod
114
114
def is_allowed_update (missing , trigger , state , query_action , command ):
115
115
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
+ )),
134
134
)):
135
135
return True
136
136
return False
@@ -153,6 +153,51 @@ def set_up(self, missing, update):
153
153
case _:
154
154
raise ValueError ("Unrecognized missing setup stage" )
155
155
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
+
156
201
def handle_update (self , update ):
157
202
user = None
158
203
try :
@@ -167,6 +212,7 @@ def handle_update(self, update):
167
212
command = None
168
213
state = None
169
214
query_action = None
215
+ msg_id = None
170
216
if "message" in update :
171
217
if "text" in update ["message" ]:
172
218
text = update ["message" ]["text" ]
@@ -196,44 +242,7 @@ def handle_update(self, update):
196
242
return
197
243
198
244
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 )
237
246
238
247
if was_missing :
239
248
is_missing = self .check_missing_setup (user )
0 commit comments