diff --git a/src/telegram/ext/filters.py b/src/telegram/ext/filters.py index a86e4b96399..f5e30243877 100644 --- a/src/telegram/ext/filters.py +++ b/src/telegram/ext/filters.py @@ -60,6 +60,7 @@ "HAS_PROTECTED_CONTENT", "INVOICE", "IS_AUTOMATIC_FORWARD", + "IS_BOT", "IS_FROM_OFFLINE", "IS_TOPIC_MESSAGE", "LIVE_PHOTO", @@ -1591,6 +1592,16 @@ def filter(self, message: Message) -> bool: .. versionadded:: 13.9 """ +class _IsBot(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.from_user and message.from_user.is_bot) + + +IS_BOT = _IsBot(name="filters.IS_BOT") +"""Messages that are sent by a bot. +""" class _IsTopicMessage(MessageFilter): diff --git a/tests/ext/test_filters.py b/tests/ext/test_filters.py index 8b101b73f4c..aac51e82826 100644 --- a/tests/ext/test_filters.py +++ b/tests/ext/test_filters.py @@ -2161,6 +2161,11 @@ 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_from_offline(self, update): assert not filters.IS_FROM_OFFLINE.check_update(update) update.message.is_from_offline = True