@@ -91,6 +91,16 @@ class Dispatcher:
91
91
Example: '/add item1 item2 item3' -> ['item1', 'item2', 'item3']
92
92
For other updates, args will be None
93
93
94
+ For regex-based handlers, you can also request information about the match.
95
+ For all other handlers, these will be None
96
+
97
+ groups:
98
+ A tuple that contains the result of
99
+ re.match(matcher, ...).groups()
100
+ groupdict:
101
+ A dictionary that contains the result of
102
+ re.match(matcher, ...).groupdict()
103
+
94
104
Attributes:
95
105
96
106
Args:
@@ -185,7 +195,7 @@ def processUpdate(self, update):
185
195
self .dispatchStringCommand (update )
186
196
handled = True
187
197
elif type (update ) is str :
188
- self .dispatchStringRegex (update )
198
+ self .dispatchRegex (update )
189
199
handled = True
190
200
191
201
# An error happened while polling
@@ -195,7 +205,7 @@ def processUpdate(self, update):
195
205
196
206
# Telegram update (regex)
197
207
if isinstance (update , Update ):
198
- self .dispatchTelegramRegex (update )
208
+ self .dispatchRegex (update )
199
209
handled = True
200
210
201
211
# Telegram update (command)
@@ -469,25 +479,30 @@ def dispatchTelegramCommand(self, update):
469
479
else :
470
480
self .dispatchTo (self .unknown_telegram_command_handlers , update )
471
481
472
- def dispatchTelegramRegex (self , update ):
482
+ def dispatchRegex (self , update ):
473
483
"""
474
- Dispatches an update to all regex handlers that match the message
475
- string.
484
+ Dispatches an update to all string or telegram regex handlers that
485
+ match the string/message content .
476
486
477
487
Args:
478
- command (str): The command keyword
479
- update (telegram.Update): The Telegram update that contains the
480
- command
488
+ update (str, Update): The update that should be checked for matches
481
489
"""
482
490
483
- matching_handlers = []
484
-
485
- for matcher in self .telegram_regex_handlers :
486
- if match (matcher , update .message .text ):
487
- for handler in self .telegram_regex_handlers [matcher ]:
488
- matching_handlers .append (handler )
489
-
490
- self .dispatchTo (matching_handlers , update )
491
+ if isinstance (update , Update ):
492
+ handlers = self .telegram_regex_handlers
493
+ to_match = update .message .text
494
+ elif isinstance (update , str ):
495
+ handlers = self .string_regex_handlers
496
+ to_match = update
497
+
498
+ for matcher in handlers :
499
+ m = match (matcher , to_match )
500
+ if m :
501
+ for handler in handlers [matcher ]:
502
+ self .call_handler (handler ,
503
+ update ,
504
+ groups = m .groups (),
505
+ groupdict = m .groupdict ())
491
506
492
507
def dispatchStringCommand (self , update ):
493
508
"""
@@ -504,25 +519,6 @@ def dispatchStringCommand(self, update):
504
519
else :
505
520
self .dispatchTo (self .unknown_string_command_handlers , update )
506
521
507
- def dispatchStringRegex (self , update ):
508
- """
509
- Dispatches an update to all string regex handlers that match the
510
- string.
511
-
512
- Args:
513
- command (str): The command keyword
514
- update (str): The string that contains the command
515
- """
516
-
517
- matching_handlers = []
518
-
519
- for matcher in self .string_regex_handlers :
520
- if match (matcher , update ):
521
- for handler in self .string_regex_handlers [matcher ]:
522
- matching_handlers .append (handler )
523
-
524
- self .dispatchTo (matching_handlers , update )
525
-
526
522
def dispatchType (self , update ):
527
523
"""
528
524
Dispatches an update of any type.
0 commit comments