Skip to content
Closed
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `d-qoi <https://github.com/d-qoi>`_
- `daimajia <https://github.com/daimajia>`_
- `Daniel Reed <https://github.com/nmlorg>`_
- `Dmitry Grigoryev <https://github.com/icecom-dg>`_
- `Eli Gao <https://github.com/eligao>`_
- `Emilio Molinari <https://github.com/xates>`_
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
Expand Down
27 changes: 27 additions & 0 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,30 @@ def __init__(self, lang):
def filter(self, message):
return message.from_user.language_code and any(
[message.from_user.language_code.startswith(x) for x in self.lang])

class msg_in(BaseFilter):
"""Filters messages to only allow those which exist in a list.

Examples:
``MessageHandler(Filters.msg_in(['i love python',]), callback_method)``

Args:
list_ (List[:obj:`str`]): Which messages to allow through. Only exact matches
are allowed.
caption (:obj:`bool`): Optional. Should the caption be used instead of text?
Default is ``False``.

"""

def __init__(self, list_, caption=False):
self.list_ = list_
self.caption = caption
self.name = 'Filters.msg_in({!r}, caption={!r})'.format(self.list_, self.caption)

def filter(self, message):
if self.caption:
txt = message.caption
else:
txt = message.text

return txt in self.list_
10 changes: 10 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ def test_language_filter_multiple(self, message):
message.from_user.language_code = 'da'
assert f(message)

def test_msg_in_filter(self, message):
message.text = 'test'
message.caption = 'caption'

assert Filters.msg_in(['test'])(message)
assert Filters.msg_in(['caption'], caption=True)(message)

assert not Filters.msg_in(['test'], caption=True)(message)
assert not Filters.msg_in(['caption'])(message)

def test_and_filters(self, message):
message.text = 'test'
message.forward_date = True
Expand Down