Add Filters.attachment (#2528)

* feat: attachment filter

* fix: add versionadded statement

* Fix: small doc string changes

Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>

Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
This commit is contained in:
Poolitzer 2021-05-19 10:39:10 +02:00 committed by GitHub
parent 8b0d2e5f75
commit cd69f69b28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View file

@ -2075,6 +2075,18 @@ officedocument.wordprocessingml.document")``.
and any(message.from_user.language_code.startswith(x) for x in self.lang)
)
class _Attachment(MessageFilter):
name = 'Filters.attachment'
def filter(self, message: Message) -> bool:
return bool(message.effective_attachment)
attachment = _Attachment()
"""Messages that contain :meth:`telegram.Message.effective_attachment`.
.. versionadded:: 13.6"""
class _UpdateType(UpdateFilter):
name = 'Filters.update'

View file

@ -2173,3 +2173,18 @@ class TestFilters:
with pytest.raises(RuntimeError, match='Cannot set name'):
f.name = 'foo'
def test_filters_attachment(self, update):
assert not Filters.attachment(update)
# we need to define a new Update (or rather, message class) here because
# effective_attachment is only evaluated once per instance, and the filter relies on that
up = Update(
0,
Message(
0,
datetime.datetime.utcnow(),
Chat(0, 'private'),
document=Document("str", "other_str"),
),
)
assert Filters.attachment(up)