Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"HAS_PROTECTED_CONTENT",
"INVOICE",
"IS_AUTOMATIC_FORWARD",
"IS_BOT",
"IS_FROM_OFFLINE",
"IS_TOPIC_MESSAGE",
"LIVE_PHOTO",
Expand Down Expand Up @@ -1593,6 +1594,31 @@ def filter(self, message: Message) -> bool:
"""


class _IsBot(UpdateFilter):
__slots__ = ()

def filter(self, update: Update) -> bool:
# Updates without a user, e.g. channel posts, are not sent by a bot
return bool(update.effective_user) and bool(
update.effective_user.is_bot # type: ignore
)


IS_BOT = _IsBot(name="filters.IS_BOT")
"""This filter filters *any* message that has a :attr:`bot <telegram.User.is_bot>` as
:attr:`telegram.Update.effective_user`.

Note:
In groups, a bot only receives messages sent by other bots if
:attr:`telegram.User.can_read_all_group_messages` is :obj:`True` for it, i.e. if privacy
mode is disabled.

.. seealso:: :attr:`telegram.ext.filters.VIA_BOT`

.. versionadded:: NEXT.VERSION
"""


class _IsTopicMessage(MessageFilter):
__slots__ = ()

Expand Down
10 changes: 10 additions & 0 deletions tests/ext/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,16 @@ def test_filters_is_automatic_forward(self, update):
update.message.is_automatic_forward = True
assert filters.IS_AUTOMATIC_FORWARD.check_update(update)

def test_filters_is_bot(self, update):
assert not filters.IS_BOT.check_update(update)
update.message.from_user.is_bot = True
assert filters.IS_BOT.check_update(update)

def test_filters_is_bot_without_user(self):
# channel posts have no `from_user`, so there is no user to look at
update = Update(0, channel_post=Message(0, dtm.datetime.utcnow(), Chat(0, Chat.CHANNEL)))
assert not filters.IS_BOT.check_update(update)

def test_filters_is_from_offline(self, update):
assert not filters.IS_FROM_OFFLINE.check_update(update)
update.message.is_from_offline = True
Expand Down