1717# You should have received a copy of the GNU Lesser Public License
1818# along with this program. If not, see [http://www.gnu.org/licenses/].
1919""" This module contains the InlineQueryHandler class """
20+ import re
21+
22+ from future .utils import string_types
2023
21- from .handler import Handler
2224from telegram import Update
2325from telegram .utils .deprecate import deprecate
26+ from .handler import Handler
2427
2528
2629class InlineQueryHandler (Handler ):
2730 """
28- Handler class to handle Telegram inline queries.
31+ Handler class to handle Telegram inline queries. Optionally based on a regex. Read the
32+ documentation of the ``re`` module for more information.
2933
3034 Args:
3135 callback (function): A function that takes ``bot, update`` as
@@ -39,18 +43,52 @@ class InlineQueryHandler(Handler):
3943 ``job_queue`` will be passed to the callback function. It will be a ``JobQueue``
4044 instance created by the ``Updater`` which can be used to schedule new jobs.
4145 Default is ``False``.
46+ pattern (optional[str or Pattern]): Optional regex pattern. If not ``None`` ``re.match``
47+ is used to determine if an update should be handled by this handler.
48+ pass_groups (optional[bool]): If the callback should be passed the
49+ result of ``re.match(pattern, query).groups()`` as a keyword
50+ argument called ``groups``. Default is ``False``
51+ pass_groupdict (optional[bool]): If the callback should be passed the
52+ result of ``re.match(pattern, query).groupdict()`` as a keyword
53+ argument called ``groupdict``. Default is ``False``
4254 """
4355
44- def __init__ (self , callback , pass_update_queue = False , pass_job_queue = False ):
56+ def __init__ (self ,
57+ callback ,
58+ pass_update_queue = False ,
59+ pass_job_queue = False ,
60+ pattern = None ,
61+ pass_groups = False ,
62+ pass_groupdict = False ):
4563 super (InlineQueryHandler , self ).__init__ (callback ,
4664 pass_update_queue = pass_update_queue ,
4765 pass_job_queue = pass_job_queue )
4866
67+ if isinstance (pattern , string_types ):
68+ pattern = re .compile (pattern )
69+
70+ self .pattern = pattern
71+ self .pass_groups = pass_groups
72+ self .pass_groupdict = pass_groupdict
73+
4974 def check_update (self , update ):
50- return isinstance (update , Update ) and update .inline_query
75+ if isinstance (update , Update ) and update .inline_query :
76+ if self .pattern :
77+ if update .inline_query .query :
78+ match = re .match (self .pattern , update .inline_query .query )
79+ return bool (match )
80+ else :
81+ return True
5182
5283 def handle_update (self , update , dispatcher ):
5384 optional_args = self .collect_optional_args (dispatcher )
85+ if self .pattern :
86+ match = re .match (self .pattern , update .inline_query .query )
87+
88+ if self .pass_groups :
89+ optional_args ['groups' ] = match .groups ()
90+ if self .pass_groupdict :
91+ optional_args ['groupdict' ] = match .groupdict ()
5492
5593 return self .callback (dispatcher .bot , update , ** optional_args )
5694
0 commit comments