Skip to content

Commit 5134f71

Browse files
committed
Merge branch 'more-regex-handlers' of https://github.com/bomjacob/python-telegram-bot into bomjacob-more-regex-handlers
2 parents 5879084 + 3226859 commit 5134f71

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The following wonderful people contributed directly or indirectly to this projec
1313
- `bimmlerd <https://github.com/bimmlerd>`_
1414
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
1515
- `franciscod <https://github.com/franciscod>`_
16+
- `Jacob Bom <https://github.com/bomjacob>`_
1617
- `JASON0916 <https://github.com/JASON0916>`_
1718
- `jh0ker <https://github.com/jh0ker>`_
1819
- `JRoot3D <https://github.com/JRoot3D>`_

telegram/ext/callbackqueryhandler.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
""" This module contains the CallbackQueryHandler class """
2020

21-
from .handler import Handler
21+
import re
22+
23+
from future.utils import string_types
24+
2225
from telegram import Update
2326
from telegram.utils.deprecate import deprecate
27+
from .handler import Handler
2428

2529

2630
class CallbackQueryHandler(Handler):
2731
"""
28-
Handler class to handle Telegram callback queries.
32+
Handler class to handle Telegram callback queries. Optionally based on a regex.
33+
Read the documentation of the ``re`` module for more information.
2934
3035
Args:
3136
callback (function): A function that takes ``bot, update`` as
@@ -39,18 +44,52 @@ class CallbackQueryHandler(Handler):
3944
``job_queue`` will be passed to the callback function. It will be a ``JobQueue``
4045
instance created by the ``Updater`` which can be used to schedule new jobs.
4146
Default is ``False``.
47+
pattern (optional[str or Pattern]): Optional regex pattern. If not ``None`` ``re.match``
48+
is used to determine if an update should be handled by this handler.
49+
pass_groups (optional[bool]): If the callback should be passed the
50+
result of ``re.match(pattern, data).groups()`` as a keyword
51+
argument called ``groups``. Default is ``False``
52+
pass_groupdict (optional[bool]): If the callback should be passed the
53+
result of ``re.match(pattern, data).groupdict()`` as a keyword
54+
argument called ``groupdict``. Default is ``False``
4255
"""
4356

44-
def __init__(self, callback, pass_update_queue=False, pass_job_queue=False):
57+
def __init__(self,
58+
callback,
59+
pass_update_queue=False,
60+
pass_job_queue=False,
61+
pattern=None,
62+
pass_groups=False,
63+
pass_groupdict=False):
4564
super(CallbackQueryHandler, self).__init__(callback,
4665
pass_update_queue=pass_update_queue,
4766
pass_job_queue=pass_job_queue)
4867

68+
if isinstance(pattern, string_types):
69+
pattern = re.compile(pattern)
70+
71+
self.pattern = pattern
72+
self.pass_groups = pass_groups
73+
self.pass_groupdict = pass_groupdict
74+
4975
def check_update(self, update):
50-
return isinstance(update, Update) and update.callback_query
76+
if isinstance(update, Update) and update.callback_query:
77+
if self.pattern:
78+
if update.callback_query.data:
79+
match = re.match(self.pattern, update.callback_query.data)
80+
return bool(match)
81+
else:
82+
return True
5183

5284
def handle_update(self, update, dispatcher):
5385
optional_args = self.collect_optional_args(dispatcher)
86+
if self.pattern:
87+
match = re.match(self.pattern, update.callback_query.data)
88+
89+
if self.pass_groups:
90+
optional_args['groups'] = match.groups()
91+
if self.pass_groupdict:
92+
optional_args['groupdict'] = match.groupdict()
5493

5594
return self.callback(dispatcher.bot, update, **optional_args)
5695

telegram/ext/inlinequeryhandler.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
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
2224
from telegram import Update
2325
from telegram.utils.deprecate import deprecate
26+
from .handler import Handler
2427

2528

2629
class 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

Comments
 (0)