Фильтр на тип чата
This commit is contained in:
105
bot/filters/chat_type.py
Normal file
105
bot/filters/chat_type.py
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
"""
|
||||||
|
Фильтры для проверки типов чатов
|
||||||
|
"""
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from aiogram.filters import BaseFilter
|
||||||
|
from aiogram.types import Message, CallbackQuery
|
||||||
|
from aiogram.enums import ChatType
|
||||||
|
|
||||||
|
__all__ = ('IsPrivateChat', 'IsGroupChat', 'IsSuperGroupChat', 'IsChannelChat', 'IsAnyGroup')
|
||||||
|
|
||||||
|
|
||||||
|
class IsPrivateChat(BaseFilter):
|
||||||
|
"""
|
||||||
|
Проверяет, что сообщение из личного чата (приватный диалог с ботом).
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
@router.message(Command("start"), IsPrivateChat())
|
||||||
|
async def start_private(message: Message):
|
||||||
|
await message.answer("Привет в личке!")
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def __call__(self, event: Union[Message, CallbackQuery]) -> bool:
|
||||||
|
if isinstance(event, CallbackQuery):
|
||||||
|
event = event.message
|
||||||
|
|
||||||
|
return event.chat.type == ChatType.PRIVATE
|
||||||
|
|
||||||
|
|
||||||
|
class IsGroupChat(BaseFilter):
|
||||||
|
"""
|
||||||
|
Проверяет, что сообщение из обычной группы (не супергруппы).
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
@router.message(IsGroupChat())
|
||||||
|
async def group_message(message: Message):
|
||||||
|
await message.answer("Это обычная группа")
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def __call__(self, event: Union[Message, CallbackQuery]) -> bool:
|
||||||
|
if isinstance(event, CallbackQuery):
|
||||||
|
event = event.message
|
||||||
|
|
||||||
|
return event.chat.type == ChatType.GROUP
|
||||||
|
|
||||||
|
|
||||||
|
class IsSuperGroupChat(BaseFilter):
|
||||||
|
"""
|
||||||
|
Проверяет, что сообщение из супергруппы.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
@router.message(IsSuperGroupChat())
|
||||||
|
async def supergroup_message(message: Message):
|
||||||
|
await message.answer("Это супергруппа")
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def __call__(self, event: Union[Message, CallbackQuery]) -> bool:
|
||||||
|
if isinstance(event, CallbackQuery):
|
||||||
|
event = event.message
|
||||||
|
|
||||||
|
return event.chat.type == ChatType.SUPERGROUP
|
||||||
|
|
||||||
|
|
||||||
|
class IsChannelChat(BaseFilter):
|
||||||
|
"""
|
||||||
|
Проверяет, что сообщение из канала.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
@router.message(IsChannelChat())
|
||||||
|
async def channel_message(message: Message):
|
||||||
|
await message.answer("Это канал")
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def __call__(self, event: Union[Message, CallbackQuery]) -> bool:
|
||||||
|
if isinstance(event, CallbackQuery):
|
||||||
|
event = event.message
|
||||||
|
|
||||||
|
return event.chat.type == ChatType.CHANNEL
|
||||||
|
|
||||||
|
|
||||||
|
class IsAnyGroup(BaseFilter):
|
||||||
|
"""
|
||||||
|
Проверяет, что сообщение из любой группы (обычная или супергруппа).
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
@router.message(Command("admin"), IsAnyGroup())
|
||||||
|
async def admin_command(message: Message):
|
||||||
|
await message.answer("Команда доступна только в группах")
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def __call__(self, event: Union[Message, CallbackQuery]) -> bool:
|
||||||
|
if isinstance(event, CallbackQuery):
|
||||||
|
event = event.message
|
||||||
|
|
||||||
|
return event.chat.type in (ChatType.GROUP, ChatType.SUPERGROUP)
|
||||||
Reference in New Issue
Block a user