X Tutup
Skip to content

Commit 3244417

Browse files
committed
Add docs for filters.
1 parent 6159640 commit 3244417

File tree

5 files changed

+61
-38
lines changed

5 files changed

+61
-38
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.ext.filters module
2+
===========================
3+
4+
.. automodule:: telegram.ext.filters
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/source/telegram.ext.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Submodules
1515
telegram.ext.commandhandler
1616
telegram.ext.inlinequeryhandler
1717
telegram.ext.messagehandler
18+
telegram.ext.filters
1819
telegram.ext.regexhandler
1920
telegram.ext.stringcommandhandler
2021
telegram.ext.stringregexhandler

telegram/ext/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .handler import Handler
2828
from .inlinequeryhandler import InlineQueryHandler
2929
from .messagehandler import MessageHandler
30-
from .filters import Filters
30+
from .filters import BaseFilter, Filters
3131
from .regexhandler import RegexHandler
3232
from .stringcommandhandler import StringCommandHandler
3333
from .stringregexhandler import StringRegexHandler
@@ -36,5 +36,5 @@
3636

3737
__all__ = ('Dispatcher', 'JobQueue', 'Job', 'Updater', 'CallbackQueryHandler',
3838
'ChosenInlineResultHandler', 'CommandHandler', 'Handler', 'InlineQueryHandler',
39-
'MessageHandler', 'Filters', 'RegexHandler', 'StringCommandHandler',
39+
'MessageHandler', 'BaseFilter', 'Filters', 'RegexHandler', 'StringCommandHandler',
4040
'StringRegexHandler', 'TypeHandler', 'ConversationHandler')

telegram/ext/filters.py

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,31 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19-
""" This module contains the MessageHandler class """
19+
""" This module contains the Filters for use with the MessageHandler class """
2020

2121

2222
class BaseFilter(object):
23-
"""Base class for all Message Filters"""
23+
"""Base class for all Message Filters
24+
25+
Subclassing from this class filters to be combined using bitwise operators:
26+
27+
And:
28+
29+
>>> (Filters.text & Filters.entity(MENTION)
30+
31+
Or:
32+
33+
>>> (Filters.audio | Filters.video)
34+
35+
Also works with more than two filters:
36+
37+
>>> (Filters.text & (Filters.entity(URL |Filters.entity(TEXT_LINK))))
38+
39+
If you want to create your own filters create a class inheriting from this class and implement
40+
a `filter` method that returns a boolean: `True` if the message should be handled, `False`
41+
otherwise. Note that the filters work only as class instances, not actual class objects
42+
(so remember to initialize your filter classes).
43+
"""
2444

2545
def __call__(self, message):
2646
return self.filter(message)
@@ -52,88 +72,87 @@ def filter(self, message):
5272

5373
class Filters(object):
5474
"""
55-
Convenient namespace (class) & methods for the filter funcs of the
56-
MessageHandler class.
75+
Predefined filters for use with the `filter` argument of :class:`telegram.ext.MessageHandler`.
5776
"""
5877

59-
class Text(BaseFilter):
78+
class _Text(BaseFilter):
6079

6180
def filter(self, message):
6281
return bool(message.text and not message.text.startswith('/'))
6382

64-
text = Text()
83+
text = _Text()
6584

66-
class Command(BaseFilter):
85+
class _Command(BaseFilter):
6786

6887
def filter(self, message):
6988
return bool(message.text and message.text.startswith('/'))
7089

71-
command = Command()
90+
command = _Command()
7291

73-
class Audio(BaseFilter):
92+
class _Audio(BaseFilter):
7493

7594
def filter(self, message):
7695
return bool(message.audio)
7796

78-
audio = Audio()
97+
audio = _Audio()
7998

80-
class Document(BaseFilter):
99+
class _Document(BaseFilter):
81100

82101
def filter(self, message):
83102
return bool(message.document)
84103

85-
document = Document()
104+
document = _Document()
86105

87-
class Photo(BaseFilter):
106+
class _Photo(BaseFilter):
88107

89108
def filter(self, message):
90109
return bool(message.photo)
91110

92-
photo = Photo()
111+
photo = _Photo()
93112

94-
class Sticker(BaseFilter):
113+
class _Sticker(BaseFilter):
95114

96115
def filter(self, message):
97116
return bool(message.sticker)
98117

99-
sticker = Sticker()
118+
sticker = _Sticker()
100119

101-
class Video(BaseFilter):
120+
class _Video(BaseFilter):
102121

103122
def filter(self, message):
104123
return bool(message.video)
105124

106-
video = Video()
125+
video = _Video()
107126

108-
class Voice(BaseFilter):
127+
class _Voice(BaseFilter):
109128

110129
def filter(self, message):
111130
return bool(message.voice)
112131

113-
voice = Voice()
132+
voice = _Voice()
114133

115-
class Contact(BaseFilter):
134+
class _Contact(BaseFilter):
116135

117136
def filter(self, message):
118137
return bool(message.contact)
119138

120-
contact = Contact()
139+
contact = _Contact()
121140

122-
class Location(BaseFilter):
141+
class _Location(BaseFilter):
123142

124143
def filter(self, message):
125144
return bool(message.location)
126145

127-
location = Location()
146+
location = _Location()
128147

129-
class Venue(BaseFilter):
148+
class _Venue(BaseFilter):
130149

131150
def filter(self, message):
132151
return bool(message.venue)
133152

134-
venue = Venue()
153+
venue = _Venue()
135154

136-
class StatusUpdate(BaseFilter):
155+
class _StatusUpdate(BaseFilter):
137156

138157
def filter(self, message):
139158
return bool(message.new_chat_member or message.left_chat_member
@@ -143,16 +162,16 @@ def filter(self, message):
143162
or message.migrate_to_chat_id or message.migrate_from_chat_id
144163
or message.pinned_message)
145164

146-
status_update = StatusUpdate()
165+
status_update = _StatusUpdate()
147166

148-
class Forwarded(BaseFilter):
167+
class _Forwarded(BaseFilter):
149168

150169
def filter(self, message):
151170
return bool(message.forward_date)
152171

153-
forwarded = Forwarded()
172+
forwarded = _Forwarded()
154173

155-
class Entity(BaseFilter):
174+
class entity(BaseFilter):
156175
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
157176
where their `type` matches `entity_type`.
158177
@@ -168,7 +187,3 @@ def __init__(self, entity_type):
168187

169188
def filter(self, message):
170189
return any([entity.type == self.entity_type for entity in message.entities])
171-
172-
# We don't initialize since this filter accepts arguments.
173-
174-
entity = Entity

tests/test_filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""
20-
This module contains a object that represents Tests for MessageHandler.Filters
20+
This module contains a object that represents Tests for Filters for use with MessageHandler
2121
"""
2222

2323
import sys

0 commit comments

Comments
 (0)
X Tutup