Skip to content

Commit 8c698ca

Browse files
committed
Add Regex handling to CallbackQueryHandler and InlineQueryHandler.
Mostly a copy-paste from RegexHandler. Not fully tested! Also needs yapf - sorry.
1 parent f5c57cd commit 8c698ca

2 files changed

Lines changed: 85 additions & 10 deletions

File tree

telegram/ext/callbackqueryhandler.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
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. Read the documentation of the ``re`` module for more information.
2933
3034
Args:
3135
callback (function): A function that takes ``bot, update`` as
@@ -39,22 +43,56 @@ class CallbackQueryHandler(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`` The ``re.match`` function is used to determine if an update should be handled by this handler.
47+
pass_groups (optional[bool]): If the callback should be passed the
48+
result of ``re.match(pattern, data).groups()`` as a keyword
49+
argument called ``groups``. Default is ``False``
50+
pass_groupdict (optional[bool]): If the callback should be passed the
51+
result of ``re.match(pattern, data).groupdict()`` as a keyword
52+
argument called ``groupdict``. Default is ``False``
4253
"""
4354

44-
def __init__(self, callback, pass_update_queue=False, pass_job_queue=False):
55+
def __init__(self,
56+
callback,
57+
pass_update_queue=False,
58+
pass_job_queue=False,
59+
pattern=None,
60+
pass_groups=False,
61+
pass_groupdict=False):
4562
super(CallbackQueryHandler, self).__init__(callback,
4663
pass_update_queue=pass_update_queue,
4764
pass_job_queue=pass_job_queue)
4865

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

5282
def handle_update(self, update, dispatcher):
5383
optional_args = self.collect_optional_args(dispatcher)
84+
if self.pattern:
85+
match = re.match(self.pattern, update.callback_query.data)
86+
87+
if self.pass_groups:
88+
optional_args['groups'] = match.groups()
89+
if self.pass_groupdict:
90+
optional_args['groupdict'] = match.groupdict()
5491

5592
return self.callback(dispatcher.bot, update, **optional_args)
5693

57-
# old non-PEP8 Handler methods
94+
# old non-PEP8 Handler methods
95+
5896
m = "telegram.CallbackQueryHandler."
5997
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
6098
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/inlinequeryhandler.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
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 documentation of the ``re`` module for more information.
2932
3033
Args:
3134
callback (function): A function that takes ``bot, update`` as
@@ -39,22 +42,56 @@ class InlineQueryHandler(Handler):
3942
``job_queue`` will be passed to the callback function. It will be a ``JobQueue``
4043
instance created by the ``Updater`` which can be used to schedule new jobs.
4144
Default is ``False``.
45+
pattern (optional[str or Pattern]): Optional regex pattern. If not ``None`` The ``re.match`` function is used to determine if an update should be handled by this handler.
46+
pass_groups (optional[bool]): If the callback should be passed the
47+
result of ``re.match(pattern, query).groups()`` as a keyword
48+
argument called ``groups``. Default is ``False``
49+
pass_groupdict (optional[bool]): If the callback should be passed the
50+
result of ``re.match(pattern, query).groupdict()`` as a keyword
51+
argument called ``groupdict``. Default is ``False``
4252
"""
4353

44-
def __init__(self, callback, pass_update_queue=False, pass_job_queue=False):
54+
def __init__(self,
55+
callback,
56+
pass_update_queue=False,
57+
pass_job_queue=False,
58+
pattern=None,
59+
pass_groups=False,
60+
pass_groupdict=False):
4561
super(InlineQueryHandler, self).__init__(callback,
4662
pass_update_queue=pass_update_queue,
4763
pass_job_queue=pass_job_queue)
4864

65+
if isinstance(pattern, string_types):
66+
pattern = re.compile(pattern)
67+
68+
self.pattern = pattern
69+
self.pass_groups = pass_groups
70+
self.pass_groupdict = pass_groupdict
71+
4972
def check_update(self, update):
50-
return isinstance(update, Update) and update.inline_query
73+
if isinstance(update, Update) and update.inline_query:
74+
if self.pattern:
75+
if update.inline_query.query:
76+
match = re.match(self.pattern, update.inline_query.query)
77+
return bool(match)
78+
else:
79+
return True
5180

5281
def handle_update(self, update, dispatcher):
5382
optional_args = self.collect_optional_args(dispatcher)
83+
if self.pattern:
84+
match = re.match(self.pattern, update.inline_query.query)
85+
86+
if self.pass_groups:
87+
optional_args['groups'] = match.groups()
88+
if self.pass_groupdict:
89+
optional_args['groupdict'] = match.groupdict()
5490

5591
return self.callback(dispatcher.bot, update, **optional_args)
5692

57-
# old non-PEP8 Handler methods
93+
# old non-PEP8 Handler methods
94+
5895
m = "telegram.InlineQueryHandler."
5996
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
6097
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

0 commit comments

Comments
 (0)