Skip to content

Commit a9c166d

Browse files
committed
add update_type filter
1 parent c5d5680 commit a9c166d

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

telegram/ext/filters.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
"""This module contains the Filters for use with the MessageHandler class."""
2020

2121
import re
22-
from telegram import Chat
22+
2323
from future.utils import string_types
2424

25+
from telegram import Chat
26+
2527

2628
class BaseFilter(object):
2729
"""Base class for all Message Filters.
@@ -56,7 +58,8 @@ class variable.
5658
5759
Attributes:
5860
name (:obj:`str`): Name for this filter. Defaults to the type of filter.
59-
61+
update_filter (:obj:`bool`): whether this filter should work on update. If ``False`` it
62+
will run the filter on :attr:`update.effective_message``. Default is ``False``.
6063
"""
6164

6265
name = None
@@ -709,3 +712,77 @@ def __init__(self, lang):
709712
def filter(self, message):
710713
return message.from_user.language_code and any(
711714
[message.from_user.language_code.startswith(x) for x in self.lang])
715+
716+
class _UpdateType(BaseFilter):
717+
update_filter = True
718+
719+
class _Message(BaseFilter):
720+
update_filter = True
721+
722+
def filter(self, update):
723+
return update.message is not None
724+
725+
message = _Message()
726+
727+
class _EditedMessage(BaseFilter):
728+
update_filter = True
729+
730+
def filter(self, update):
731+
return update.edited_message is not None
732+
733+
edited_message = _EditedMessage()
734+
735+
class _Messages(BaseFilter):
736+
update_filter = True
737+
738+
def filter(self, update):
739+
return update.message is not None or update.edited_message is not None
740+
741+
messages = _Messages()
742+
743+
class _ChannelPost(BaseFilter):
744+
update_filter = True
745+
746+
def filter(self, update):
747+
return update.channel_post is not None
748+
749+
channel_post = _ChannelPost()
750+
751+
class _EditedChannelPost(BaseFilter):
752+
update_filter = True
753+
754+
def filter(self, update):
755+
return update.edited_channel_post is not None
756+
757+
edited_channel_post = _EditedChannelPost()
758+
759+
class _ChannelPosts(BaseFilter):
760+
update_filter = True
761+
762+
def filter(self, update):
763+
return update.channel_post is not None or update.edited_channel_post is not None
764+
765+
channel_posts = _ChannelPosts()
766+
767+
def filter(self, update):
768+
return self.messages(update) or self.channel_posts(update)
769+
770+
update_type = _UpdateType()
771+
"""Subset for filtering the type of update.
772+
773+
Examples:
774+
Use these filters like: ``Filters.update_type.message`` or
775+
``Filters.update_type.channel_posts``etc. Or use just ``Filters.update_type`` for all
776+
types.
777+
778+
Attributes:
779+
message (:obj:`Filter`): Updates with :attr:`telegram.Update.message`
780+
edited_message (:obj:`Filter`): Updates with :attr:`telegram.Update.edited_message`
781+
messages (:obj:`Filter`): Updates with either :attr:`telegram.Update.message` or
782+
:attr:`telegram.Update.edited_message`
783+
channel_post (:obj:`Filter`): Updates with :attr:`telegram.Update.channel_post`
784+
edited_channel_post (:obj:`Filter`): Updates with
785+
:attr:`telegram.Update.edited_channel_post`
786+
channel_posts (:obj:`Filter`): Updates with either :attr:`telegram.Update.channel_post` or
787+
:attr:`telegram.Update.edited_channel_post`
788+
"""

tests/test_filters.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,42 @@ def filter(self, mes):
495495

496496
unnamed = Unnamed()
497497
assert str(unnamed) == Unnamed.__name__
498+
499+
def test_update_type_message(self, update):
500+
assert Filters.update_type.message(update)
501+
assert not Filters.update_type.edited_message(update)
502+
assert Filters.update_type.messages(update)
503+
assert not Filters.update_type.channel_post(update)
504+
assert not Filters.update_type.edited_channel_post(update)
505+
assert not Filters.update_type.channel_posts(update)
506+
assert Filters.update_type(update)
507+
508+
def test_update_type_edited_message(self, update):
509+
update.edited_message, update.message = update.message, update.edited_message
510+
assert not Filters.update_type.message(update)
511+
assert Filters.update_type.edited_message(update)
512+
assert Filters.update_type.messages(update)
513+
assert not Filters.update_type.channel_post(update)
514+
assert not Filters.update_type.edited_channel_post(update)
515+
assert not Filters.update_type.channel_posts(update)
516+
assert Filters.update_type(update)
517+
518+
def test_update_type_channel_post(self, update):
519+
update.channel_post, update.message = update.message, update.edited_message
520+
assert not Filters.update_type.message(update)
521+
assert not Filters.update_type.edited_message(update)
522+
assert not Filters.update_type.messages(update)
523+
assert Filters.update_type.channel_post(update)
524+
assert not Filters.update_type.edited_channel_post(update)
525+
assert Filters.update_type.channel_posts(update)
526+
assert Filters.update_type(update)
527+
528+
def test_update_type_edited_channel_post(self, update):
529+
update.edited_channel_post, update.message = update.message, update.edited_message
530+
assert not Filters.update_type.message(update)
531+
assert not Filters.update_type.edited_message(update)
532+
assert not Filters.update_type.messages(update)
533+
assert not Filters.update_type.channel_post(update)
534+
assert Filters.update_type.edited_channel_post(update)
535+
assert Filters.update_type.channel_posts(update)
536+
assert Filters.update_type(update)

0 commit comments

Comments
 (0)