mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-17 04:39:55 +01:00
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:
parent
8b0d2e5f75
commit
cd69f69b28
2 changed files with 27 additions and 0 deletions
|
@ -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'
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue