mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
feat: ChatTypesFilter
This commit is contained in:
parent
21b4b64db1
commit
bcd2c96bb0
4 changed files with 31 additions and 1 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
0
examples/chat_types_filter.py
Normal file
0
examples/chat_types_filter.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue