diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 164d6aad..322a0696 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -11,7 +11,7 @@ from aiohttp.helpers import sentinel from aiogram.utils.deprecated import renamed_argument from .filters import Command, ContentTypeFilter, ExceptionsFilter, FiltersFactory, HashTag, Regexp, \ RegexpCommandsFilter, StateFilter, Text, IDFilter, AdminFilter, IsReplyFilter, ForwardedMessageFilter -from .filters.builtin import IsSenderContact +from .filters.builtin import IsSenderContact, ChatTypesFilter from .handler import Handler from .middlewares import MiddlewareManager from .storage import BaseStorage, DELTA, DisabledStorage, EXCEEDED_COUNT, FSMContext, \ @@ -166,6 +166,16 @@ class Dispatcher(DataMixin, ContextInstanceMixin): self.channel_post_handlers, self.edited_channel_post_handlers ]) + filters_factory.bind(ChatTypesFilter, event_handlers=[ + self.message_handlers, + self.edited_message_handlers, + self.channel_post_handlers, + self.edited_channel_post_handlers, + self.callback_query_handlers, + self.poll_handlers, + self.inline_query_handlers, + ]) + def __del__(self): self.stop_polling() diff --git a/aiogram/dispatcher/filters/builtin.py b/aiogram/dispatcher/filters/builtin.py index c59d9b0d..97e8739f 100644 --- a/aiogram/dispatcher/filters/builtin.py +++ b/aiogram/dispatcher/filters/builtin.py @@ -691,3 +691,17 @@ class ForwardedMessageFilter(BoundFilter): async def check(self, message: Message): return bool(getattr(message, "forward_date")) is self.is_forwarded + + +class ChatTypesFilter(BoundFilter): + key = 'chat_types' + + def __init__(self, chat_types: typing.List[ChatType]): + self.chat_types = chat_types + + async def check(self, obj: Union[Message, CallbackQuery]): + if isinstance(obj, Message): + obj = obj.chat + if isinstance(obj, CallbackQuery): + obj = obj.message.chat + return obj.type in self.chat_types diff --git a/aiogram/types/chat.py b/aiogram/types/chat.py index 4a7287d8..28cc5ed0 100644 --- a/aiogram/types/chat.py +++ b/aiogram/types/chat.py @@ -10,6 +10,7 @@ from .chat_member import ChatMember from .chat_permissions import ChatPermissions from .chat_photo import ChatPhoto from .input_file import InputFile +from ..utils.deprecated import deprecated class Chat(base.TelegramObject): @@ -512,6 +513,7 @@ class ChatType(helper.Helper): return obj.type in chat_types @classmethod + @deprecated("This filter was moved to ChatTypeFilter, and will be removed in aiogram v3.0") def is_private(cls, obj) -> bool: """ Check chat is private @@ -522,6 +524,7 @@ class ChatType(helper.Helper): return cls._check(obj, [cls.PRIVATE]) @classmethod + @deprecated("This filter was moved to ChatTypeFilter, and will be removed in aiogram v3.0") def is_group(cls, obj) -> bool: """ Check chat is group @@ -532,6 +535,7 @@ class ChatType(helper.Helper): return cls._check(obj, [cls.GROUP]) @classmethod + @deprecated("This filter was moved to ChatTypeFilter, and will be removed in aiogram v3.0") def is_super_group(cls, obj) -> bool: """ Check chat is super-group @@ -542,6 +546,7 @@ class ChatType(helper.Helper): return cls._check(obj, [cls.SUPER_GROUP]) @classmethod + @deprecated("This filter was moved to ChatTypeFilter, and will be removed in aiogram v3.0") def is_group_or_super_group(cls, obj) -> bool: """ Check chat is group or super-group @@ -552,6 +557,7 @@ class ChatType(helper.Helper): return cls._check(obj, [cls.GROUP, cls.SUPER_GROUP]) @classmethod + @deprecated("This filter was moved to ChatTypeFilter, and will be removed in aiogram v3.0") def is_channel(cls, obj) -> bool: """ Check chat is channel diff --git a/examples/chat_types_filter.py b/examples/chat_types_filter.py new file mode 100644 index 00000000..e69de29b