Skip to content

Commit cd69f69

Browse files
Add Filters.attachment (python-telegram-bot#2528)
* feat: attachment filter * fix: add versionadded statement * Fix: small doc string changes Co-authored-by: Bibo-Joshi <[email protected]> Co-authored-by: Bibo-Joshi <[email protected]>
1 parent 8b0d2e5 commit cd69f69

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

telegram/ext/filters.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,18 @@ def filter(self, message: Message) -> bool:
20752075
and any(message.from_user.language_code.startswith(x) for x in self.lang)
20762076
)
20772077

2078+
class _Attachment(MessageFilter):
2079+
name = 'Filters.attachment'
2080+
2081+
def filter(self, message: Message) -> bool:
2082+
return bool(message.effective_attachment)
2083+
2084+
attachment = _Attachment()
2085+
"""Messages that contain :meth:`telegram.Message.effective_attachment`.
2086+
2087+
2088+
.. versionadded:: 13.6"""
2089+
20782090
class _UpdateType(UpdateFilter):
20792091
name = 'Filters.update'
20802092

tests/test_filters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,3 +2173,18 @@ def test_filters_via_bot_repr(self):
21732173

21742174
with pytest.raises(RuntimeError, match='Cannot set name'):
21752175
f.name = 'foo'
2176+
2177+
def test_filters_attachment(self, update):
2178+
assert not Filters.attachment(update)
2179+
# we need to define a new Update (or rather, message class) here because
2180+
# effective_attachment is only evaluated once per instance, and the filter relies on that
2181+
up = Update(
2182+
0,
2183+
Message(
2184+
0,
2185+
datetime.datetime.utcnow(),
2186+
Chat(0, 'private'),
2187+
document=Document("str", "other_str"),
2188+
),
2189+
)
2190+
assert Filters.attachment(up)

0 commit comments

Comments
 (0)