|
19 | 19 | """This module contains the Filters for use with the MessageHandler class.""" |
20 | 20 |
|
21 | 21 | import re |
22 | | -from telegram import Chat |
| 22 | + |
23 | 23 | from future.utils import string_types |
24 | 24 |
|
| 25 | +from telegram import Chat |
| 26 | + |
25 | 27 |
|
26 | 28 | class BaseFilter(object): |
27 | 29 | """Base class for all Message Filters. |
@@ -56,7 +58,8 @@ class variable. |
56 | 58 |
|
57 | 59 | Attributes: |
58 | 60 | 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``. |
60 | 63 | """ |
61 | 64 |
|
62 | 65 | name = None |
@@ -709,3 +712,77 @@ def __init__(self, lang): |
709 | 712 | def filter(self, message): |
710 | 713 | return message.from_user.language_code and any( |
711 | 714 | [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 | + """ |
0 commit comments