Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filters for Category and file types #1046

Merged
merged 9 commits into from Mar 16, 2018
Copy path View file
@@ -192,6 +192,79 @@ def filter(self, message):
class _Document(BaseFilter):
name = 'Filters.document'

class category(BaseFilter):
"""This Filter filters documents by their category in the mime-type attribute
Note:
This Filter only filters by the mime_type of the document,
it doesn't check the validity of the document.
The user can manipulate the mime-type of a message and
send media with wrong types that don't fit to this handler.
Examples:
Filters.documents.category('audio/') returnes `True` for all types
of audio sent as file, for example 'audio/mpeg' or 'audio/x-wav'
"""

def __init__(self, category):
"""Initialize the category you want to filter
Args:
category (str, optional): category of the media you want to filter"""
self.category = category
self.name = 'Filters.document.category(\'{}\')'.format(self.category)

This comment has been minimized.

Copy link
@jh0ker

jh0ker Mar 16, 2018

Member

Please use double-quotes on the string instead of escaping single-quotes inside the string


def filter(self, message):
if message.document:
return message.document.mime_type.startswith(self.category)

application = category('application/')
audio = category('audio/')
image = category('image/')
video = category('video/')
text = category('text/')

class file_type(BaseFilter):

This comment has been minimized.

Copy link
@jh0ker

jh0ker Mar 16, 2018

Member

I believe in one of your previous PRs we had agreed to rename this filter to mime_type to prevent misunderstandings. Is there a reason you changed this back?

"""This Filter filters documents by their mime-type attribute
Note:
This Filter only filters by the mime_type of the document,
it doesn't check the validity of document.
The user can manipulate the mime-type of a message and
send media with wrong types that don't fit to this handler.
Examples:
Filters.documents.file_type('audio/mpeg') filters all audio in mp3 format.
"""

def __init__(self, filetype):
"""Initialize the category you want to filter
Args:
filetype (str, optional): mime_type of the media you want to filter"""
self.filetype = filetype
self.name = 'Filters.document.file_type(\'{}\')'.format(self.filetype)

def filter(self, message):
if message.document:
return message.document.mime_type == self.filetype

apk = file_type('application/vnd.android.package-archive')
doc = file_type('application/msword')
docx = file_type('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
exe = file_type('application/x-ms-dos-executable')
gif = file_type('video/mp4')
jpg = file_type('image/jpeg')
mp3 = file_type('audio/mpeg')
pdf = file_type('application/pdf')
py = file_type('text/x-python')
svg = file_type('image/svg+xml')
txt = file_type('text/plain')
targz = file_type('application/x-compressed-tar')
wav = file_type('audio/x-wav')
xml = file_type('application/xml')
zip = file_type('application/zip')

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

@@ -277,7 +350,7 @@ def filter(self, message):
return bool(message.new_chat_members)

new_chat_members = _NewChatMembers()
""":obj:`Filter`: Messages that contain :attr:`telegram.Message.new_chat_members`."""
""":obj:`Filter`: Messages that contain :attr:`telegram.Message.new_chat_member`."""

This comment has been minimized.

Copy link
@jh0ker

jh0ker Mar 16, 2018

Member

Please revert this change


class _LeftChatMember(BaseFilter):
name = 'Filters.status_update.left_chat_member'
@@ -368,7 +441,7 @@ def filter(self, message):
"""Subset for messages containing a status update.
Examples:
Use these filters like: ``Filters.status_update.new_chat_members`` etc. Or use just
Use these filters like: ``Filters.status_update.new_chat_member`` etc. Or use just

This comment has been minimized.

Copy link
@jh0ker

jh0ker Mar 16, 2018

Member

Please revert this change

``Filters.status_update`` for all status update messages.
Attributes:
@@ -384,7 +457,7 @@ def filter(self, message):
:attr:`telegram.Message.migrate_from_chat_id` or
:attr: `telegram.Message.migrate_from_chat_id`.
new_chat_members (:obj:`Filter`): Messages that contain
:attr:`telegram.Message.new_chat_members`.
:attr:`telegram.Message.new_chat_member`.

This comment has been minimized.

Copy link
@jh0ker

jh0ker Mar 16, 2018

Member

Please revert this change

new_chat_photo (:obj:`Filter`): Messages that contain
:attr:`telegram.Message.new_chat_photo`.
new_chat_title (:obj:`Filter`): Messages that contain
Copy path View file
@@ -20,7 +20,7 @@

import pytest

from telegram import Message, User, Chat, MessageEntity
from telegram import Message, User, Chat, MessageEntity, Document
from telegram.ext import Filters, BaseFilter


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

def test_filters_document_type(self, message):
message.document = Document("file_id", mime_type="application/vnd.android.package-archive")
assert Filters.document.apk(message)
assert Filters.document.application(message)
assert not Filters.document.doc(message)
assert not Filters.document.audio(message)

message.document.mime_type = "application/msword"
assert Filters.document.doc(message)
assert Filters.document.application(message)
assert not Filters.document.docx(message)
assert not Filters.document.audio(message)

message.document.mime_type = "application/vnd.openxmlformats-" \
"officedocument.wordprocessingml.document"
assert Filters.document.docx(message)
assert Filters.document.application(message)
assert not Filters.document.exe(message)
assert not Filters.document.audio(message)

message.document.mime_type = "application/x-ms-dos-executable"
assert Filters.document.exe(message)
assert Filters.document.application(message)
assert not Filters.document.docx(message)
assert not Filters.document.audio(message)

message.document.mime_type = "video/mp4"
assert Filters.document.gif(message)
assert Filters.document.video(message)
assert not Filters.document.jpg(message)
assert not Filters.document.text(message)

message.document.mime_type = "image/jpeg"
assert Filters.document.jpg(message)
assert Filters.document.image(message)
assert not Filters.document.mp3(message)
assert not Filters.document.video(message)

message.document.mime_type = "audio/mpeg"
assert Filters.document.mp3(message)
assert Filters.document.audio(message)
assert not Filters.document.pdf(message)
assert not Filters.document.image(message)

message.document.mime_type = "application/pdf"
assert Filters.document.pdf(message)
assert Filters.document.application(message)
assert not Filters.document.py(message)
assert not Filters.document.audio(message)

message.document.mime_type = "text/x-python"
assert Filters.document.py(message)
assert Filters.document.text(message)
assert not Filters.document.svg(message)
assert not Filters.document.application(message)

message.document.mime_type = "image/svg+xml"
assert Filters.document.svg(message)
assert Filters.document.image(message)
assert not Filters.document.txt(message)
assert not Filters.document.video(message)

message.document.mime_type = "text/plain"
assert Filters.document.txt(message)
assert Filters.document.text(message)
assert not Filters.document.targz(message)
assert not Filters.document.application(message)

message.document.mime_type = "application/x-compressed-tar"
assert Filters.document.targz(message)
assert Filters.document.application(message)
assert not Filters.document.wav(message)
assert not Filters.document.audio(message)

message.document.mime_type = "audio/x-wav"
assert Filters.document.wav(message)
assert Filters.document.audio(message)
assert not Filters.document.xml(message)
assert not Filters.document.image(message)

message.document.mime_type = "application/xml"
assert Filters.document.xml(message)
assert Filters.document.application(message)
assert not Filters.document.zip(message)
assert not Filters.document.audio(message)

message.document.mime_type = "application/zip"
assert Filters.document.zip(message)
assert Filters.document.application(message)
assert not Filters.document.apk(message)
assert not Filters.document.audio(message)

message.document.mime_type = "image/x-rgb"
assert not Filters.document.category("application/")(message)
assert not Filters.document.file_type("application/x-sh")(message)
message.document.mime_type = "application/x-sh"
assert Filters.document.category("application/")(message)
assert Filters.document.file_type("application/x-sh")(message)

def test_filters_photo(self, message):
assert not Filters.photo(message)
message.photo = 'test'
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.