Skip to content

Commit 38e3b91

Browse files
spontanurlaubjh0ker
authored andcommitted
Filters for Category and file types (python-telegram-bot#1046)
* Added extra Filters for File Type and Category Added extra Filters for File Type and Category, based on the provided Mime-Type * Added tests for the new Filters Added Tests for the Category and File Types Filters. * Fixed Tests Fixed the Tests from the last commit * Little Fix * Revert of unwanted changes * Update filters.py * Changed the strings * Changed file_type to mime_type * Fixed Tests
1 parent e182046 commit 38e3b91

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

telegram/ext/filters.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,79 @@ def filter(self, message):
221221
class _Document(BaseFilter):
222222
name = 'Filters.document'
223223

224+
class category(BaseFilter):
225+
"""This Filter filters documents by their category in the mime-type attribute
226+
227+
Note:
228+
This Filter only filters by the mime_type of the document,
229+
it doesn't check the validity of the document.
230+
The user can manipulate the mime-type of a message and
231+
send media with wrong types that don't fit to this handler.
232+
233+
Examples:
234+
Filters.documents.category('audio/') returnes `True` for all types
235+
of audio sent as file, for example 'audio/mpeg' or 'audio/x-wav'
236+
"""
237+
238+
def __init__(self, category):
239+
"""Initialize the category you want to filter
240+
241+
Args:
242+
category (str, optional): category of the media you want to filter"""
243+
self.category = category
244+
self.name = "Filters.document.category('{}')".format(self.category)
245+
246+
def filter(self, message):
247+
if message.document:
248+
return message.document.mime_type.startswith(self.category)
249+
250+
application = category('application/')
251+
audio = category('audio/')
252+
image = category('image/')
253+
video = category('video/')
254+
text = category('text/')
255+
256+
class mime_type(BaseFilter):
257+
"""This Filter filters documents by their mime-type attribute
258+
259+
Note:
260+
This Filter only filters by the mime_type of the document,
261+
it doesn't check the validity of document.
262+
The user can manipulate the mime-type of a message and
263+
send media with wrong types that don't fit to this handler.
264+
265+
Examples:
266+
Filters.documents.mime_type('audio/mpeg') filters all audio in mp3 format.
267+
"""
268+
269+
def __init__(self, mimetype):
270+
"""Initialize the category you want to filter
271+
272+
Args:
273+
filetype (str, optional): mime_type of the media you want to filter"""
274+
self.mimetype = mimetype
275+
self.name = "Filters.document.mime_type('{}')".format(self.mimetype)
276+
277+
def filter(self, message):
278+
if message.document:
279+
return message.document.mime_type == self.mimetype
280+
281+
apk = mime_type('application/vnd.android.package-archive')
282+
doc = mime_type('application/msword')
283+
docx = mime_type('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
284+
exe = mime_type('application/x-ms-dos-executable')
285+
gif = mime_type('video/mp4')
286+
jpg = mime_type('image/jpeg')
287+
mp3 = mime_type('audio/mpeg')
288+
pdf = mime_type('application/pdf')
289+
py = mime_type('text/x-python')
290+
svg = mime_type('image/svg+xml')
291+
txt = mime_type('text/plain')
292+
targz = mime_type('application/x-compressed-tar')
293+
wav = mime_type('audio/x-wav')
294+
xml = mime_type('application/xml')
295+
zip = mime_type('application/zip')
296+
224297
def filter(self, message):
225298
return bool(message.document)
226299

tests/test_filters.py

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import pytest
2222

23-
from telegram import Message, User, Chat, MessageEntity
23+
from telegram import Message, User, Chat, MessageEntity, Document
2424
from telegram.ext import Filters, BaseFilter
2525
import re
2626

@@ -86,6 +86,105 @@ def test_filters_document(self, message):
8686
message.document = 'test'
8787
assert Filters.document(message)
8888

89+
def test_filters_document_type(self, message):
90+
message.document = Document("file_id", mime_type="application/vnd.android.package-archive")
91+
assert Filters.document.apk(message)
92+
assert Filters.document.application(message)
93+
assert not Filters.document.doc(message)
94+
assert not Filters.document.audio(message)
95+
96+
message.document.mime_type = "application/msword"
97+
assert Filters.document.doc(message)
98+
assert Filters.document.application(message)
99+
assert not Filters.document.docx(message)
100+
assert not Filters.document.audio(message)
101+
102+
message.document.mime_type = "application/vnd.openxmlformats-" \
103+
"officedocument.wordprocessingml.document"
104+
assert Filters.document.docx(message)
105+
assert Filters.document.application(message)
106+
assert not Filters.document.exe(message)
107+
assert not Filters.document.audio(message)
108+
109+
message.document.mime_type = "application/x-ms-dos-executable"
110+
assert Filters.document.exe(message)
111+
assert Filters.document.application(message)
112+
assert not Filters.document.docx(message)
113+
assert not Filters.document.audio(message)
114+
115+
message.document.mime_type = "video/mp4"
116+
assert Filters.document.gif(message)
117+
assert Filters.document.video(message)
118+
assert not Filters.document.jpg(message)
119+
assert not Filters.document.text(message)
120+
121+
message.document.mime_type = "image/jpeg"
122+
assert Filters.document.jpg(message)
123+
assert Filters.document.image(message)
124+
assert not Filters.document.mp3(message)
125+
assert not Filters.document.video(message)
126+
127+
message.document.mime_type = "audio/mpeg"
128+
assert Filters.document.mp3(message)
129+
assert Filters.document.audio(message)
130+
assert not Filters.document.pdf(message)
131+
assert not Filters.document.image(message)
132+
133+
message.document.mime_type = "application/pdf"
134+
assert Filters.document.pdf(message)
135+
assert Filters.document.application(message)
136+
assert not Filters.document.py(message)
137+
assert not Filters.document.audio(message)
138+
139+
message.document.mime_type = "text/x-python"
140+
assert Filters.document.py(message)
141+
assert Filters.document.text(message)
142+
assert not Filters.document.svg(message)
143+
assert not Filters.document.application(message)
144+
145+
message.document.mime_type = "image/svg+xml"
146+
assert Filters.document.svg(message)
147+
assert Filters.document.image(message)
148+
assert not Filters.document.txt(message)
149+
assert not Filters.document.video(message)
150+
151+
message.document.mime_type = "text/plain"
152+
assert Filters.document.txt(message)
153+
assert Filters.document.text(message)
154+
assert not Filters.document.targz(message)
155+
assert not Filters.document.application(message)
156+
157+
message.document.mime_type = "application/x-compressed-tar"
158+
assert Filters.document.targz(message)
159+
assert Filters.document.application(message)
160+
assert not Filters.document.wav(message)
161+
assert not Filters.document.audio(message)
162+
163+
message.document.mime_type = "audio/x-wav"
164+
assert Filters.document.wav(message)
165+
assert Filters.document.audio(message)
166+
assert not Filters.document.xml(message)
167+
assert not Filters.document.image(message)
168+
169+
message.document.mime_type = "application/xml"
170+
assert Filters.document.xml(message)
171+
assert Filters.document.application(message)
172+
assert not Filters.document.zip(message)
173+
assert not Filters.document.audio(message)
174+
175+
message.document.mime_type = "application/zip"
176+
assert Filters.document.zip(message)
177+
assert Filters.document.application(message)
178+
assert not Filters.document.apk(message)
179+
assert not Filters.document.audio(message)
180+
181+
message.document.mime_type = "image/x-rgb"
182+
assert not Filters.document.category("application/")(message)
183+
assert not Filters.document.mime_type("application/x-sh")(message)
184+
message.document.mime_type = "application/x-sh"
185+
assert Filters.document.category("application/")(message)
186+
assert Filters.document.mime_type("application/x-sh")(message)
187+
89188
def test_filters_photo(self, message):
90189
assert not Filters.photo(message)
91190
message.photo = 'test'

0 commit comments

Comments
 (0)