New filter: regex (#1028)

This commit is contained in:
Joscha Götzer 2018-03-15 05:59:27 +01:00 committed by Noam Meltzer
parent 2b221da9b9
commit 1530ed20e5
2 changed files with 46 additions and 0 deletions

View file

@ -17,6 +17,8 @@
# You should have received a copy of the GNU Lesser Public License # You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains the Filters for use with the MessageHandler class.""" """This module contains the Filters for use with the MessageHandler class."""
import re
from telegram import Chat from telegram import Chat
from future.utils import string_types from future.utils import string_types
@ -171,6 +173,33 @@ class Filters(object):
command = _Command() command = _Command()
""":obj:`Filter`: Messages starting with ``/``.""" """:obj:`Filter`: Messages starting with ``/``."""
class regex(BaseFilter):
"""
Filters updates by searching for an occurence of ``pattern`` in the message text.
The ``re.search`` function is used to determine whether an update should be filtered.
Refer to the documentation of the ``re`` module for more information.
Note: Does not allow passing groups or a groupdict like the ``RegexHandler`` yet,
but this will probably be implemented in a future update, gradually phasing out the
RegexHandler (see https://github.com/python-telegram-bot/python-telegram-bot/issues/835).
Examples:
Example ``CommandHandler("start", deep_linked_callback, Filters.regex('parameter'))``
Args:
pattern (:obj:`str` | :obj:`Pattern`): The regex pattern.
"""
def __init__(self, pattern):
self.pattern = re.compile(pattern)
self.name = 'Filters.regex({})'.format(self.pattern)
# TODO: Once the callback revamp (#1026) is done, the regex filter should be able to pass
# the matched groups and groupdict to the context object.
def filter(self, message):
return bool(self.pattern.search(message.text))
class _Reply(BaseFilter): class _Reply(BaseFilter):
name = 'Filters.reply' name = 'Filters.reply'

View file

@ -22,6 +22,7 @@ import pytest
from telegram import Message, User, Chat, MessageEntity from telegram import Message, User, Chat, MessageEntity
from telegram.ext import Filters, BaseFilter from telegram.ext import Filters, BaseFilter
import re
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -51,6 +52,22 @@ class TestFilters(object):
message.text = '/test' message.text = '/test'
assert Filters.command(message) assert Filters.command(message)
def test_filters_regex(self, message):
message.text = '/start deep-linked param'
assert Filters.regex(r'deep-linked param')(message)
message.text = '/help'
assert Filters.regex(r'help')(message)
message.text = '/help'
assert Filters.regex('help')(message)
message.text = 'test'
assert not Filters.regex(r'fail')(message)
assert Filters.regex(r'test')(message)
assert Filters.regex(re.compile(r'test'))(message)
message.text = 'i love python'
assert Filters.regex(r'.\b[lo]{2}ve python')(message)
def test_filters_reply(self, message): def test_filters_reply(self, message):
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(), another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(),
Chat(0, 'private')) Chat(0, 'private'))