X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains the Filters for use with the MessageHandler class."""

import re
from telegram import Chat
from future.utils import string_types

Expand Down Expand Up @@ -171,6 +173,33 @@ def filter(self, message):
command = _Command()
""":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):
name = 'Filters.reply'

Expand Down
17 changes: 17 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from telegram import Message, User, Chat, MessageEntity
from telegram.ext import Filters, BaseFilter
import re


@pytest.fixture(scope='function')
Expand Down Expand Up @@ -51,6 +52,22 @@ def test_filters_command(self, message):
message.text = '/test'
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):
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(),
Chat(0, 'private'))
Expand Down
X Tutup