X Tutup
Skip to content

Commit 1530ed2

Browse files
JosXatsnoam
authored andcommitted
New filter: regex (python-telegram-bot#1028)
1 parent 2b221da commit 1530ed2

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

telegram/ext/filters.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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 Filters for use with the MessageHandler class."""
20+
21+
import re
2022
from telegram import Chat
2123
from future.utils import string_types
2224

@@ -171,6 +173,33 @@ def filter(self, message):
171173
command = _Command()
172174
""":obj:`Filter`: Messages starting with ``/``."""
173175

176+
class regex(BaseFilter):
177+
"""
178+
Filters updates by searching for an occurence of ``pattern`` in the message text.
179+
The ``re.search`` function is used to determine whether an update should be filtered.
180+
Refer to the documentation of the ``re`` module for more information.
181+
182+
Note: Does not allow passing groups or a groupdict like the ``RegexHandler`` yet,
183+
but this will probably be implemented in a future update, gradually phasing out the
184+
RegexHandler (see https://github.com/python-telegram-bot/python-telegram-bot/issues/835).
185+
186+
Examples:
187+
Example ``CommandHandler("start", deep_linked_callback, Filters.regex('parameter'))``
188+
189+
Args:
190+
pattern (:obj:`str` | :obj:`Pattern`): The regex pattern.
191+
"""
192+
193+
def __init__(self, pattern):
194+
self.pattern = re.compile(pattern)
195+
self.name = 'Filters.regex({})'.format(self.pattern)
196+
197+
# TODO: Once the callback revamp (#1026) is done, the regex filter should be able to pass
198+
# the matched groups and groupdict to the context object.
199+
200+
def filter(self, message):
201+
return bool(self.pattern.search(message.text))
202+
174203
class _Reply(BaseFilter):
175204
name = 'Filters.reply'
176205

tests/test_filters.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from telegram import Message, User, Chat, MessageEntity
2424
from telegram.ext import Filters, BaseFilter
25+
import re
2526

2627

2728
@pytest.fixture(scope='function')
@@ -51,6 +52,22 @@ def test_filters_command(self, message):
5152
message.text = '/test'
5253
assert Filters.command(message)
5354

55+
def test_filters_regex(self, message):
56+
message.text = '/start deep-linked param'
57+
assert Filters.regex(r'deep-linked param')(message)
58+
message.text = '/help'
59+
assert Filters.regex(r'help')(message)
60+
message.text = '/help'
61+
assert Filters.regex('help')(message)
62+
63+
message.text = 'test'
64+
assert not Filters.regex(r'fail')(message)
65+
assert Filters.regex(r'test')(message)
66+
assert Filters.regex(re.compile(r'test'))(message)
67+
68+
message.text = 'i love python'
69+
assert Filters.regex(r'.\b[lo]{2}ve python')(message)
70+
5471
def test_filters_reply(self, message):
5572
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(),
5673
Chat(0, 'private'))

0 commit comments

Comments
 (0)
X Tutup