mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 02:03:04 +00:00
Default filters
This commit is contained in:
parent
1ca0be538b
commit
b4ecc421e4
3 changed files with 15 additions and 4 deletions
|
|
@ -345,9 +345,6 @@ class Dispatcher:
|
||||||
:param state:
|
:param state:
|
||||||
:return: decorated function
|
:return: decorated function
|
||||||
"""
|
"""
|
||||||
if content_types is None:
|
|
||||||
content_types = ContentType.TEXT
|
|
||||||
|
|
||||||
filters_set = self.filters_factory.resolve(self.message_handlers,
|
filters_set = self.filters_factory.resolve(self.message_handlers,
|
||||||
*custom_filters,
|
*custom_filters,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import re
|
import re
|
||||||
|
import typing
|
||||||
from _contextvars import ContextVar
|
from _contextvars import ContextVar
|
||||||
|
|
||||||
|
from aiogram import types
|
||||||
from aiogram.dispatcher.filters.filters import BaseFilter
|
from aiogram.dispatcher.filters.filters import BaseFilter
|
||||||
from aiogram.types import CallbackQuery, ContentType, Message
|
from aiogram.types import CallbackQuery, ContentType, Message
|
||||||
|
|
||||||
|
|
@ -98,6 +100,12 @@ class ContentTypeFilter(BaseFilter):
|
||||||
super().__init__(dispatcher)
|
super().__init__(dispatcher)
|
||||||
self.content_types = content_types
|
self.content_types = content_types
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def validate(cls, full_config: typing.Dict[str, typing.Any]):
|
||||||
|
result = super(ContentTypeFilter, cls).validate(full_config)
|
||||||
|
if not result:
|
||||||
|
return {cls.key: types.ContentType.TEXT}
|
||||||
|
|
||||||
async def check(self, message):
|
async def check(self, message):
|
||||||
return ContentType.ANY[0] in self.content_types or \
|
return ContentType.ANY[0] in self.content_types or \
|
||||||
message.content_type in self.content_types
|
message.content_type in self.content_types
|
||||||
|
|
@ -120,6 +128,12 @@ class StateFilter(BaseFilter):
|
||||||
def get_target(self, obj):
|
def get_target(self, obj):
|
||||||
return getattr(getattr(obj, 'chat', None), 'id', None), getattr(getattr(obj, 'from_user', None), 'id', None)
|
return getattr(getattr(obj, 'chat', None), 'id', None), getattr(getattr(obj, 'from_user', None), 'id', None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def validate(cls, full_config: typing.Dict[str, typing.Any]):
|
||||||
|
result = super(StateFilter, cls).validate(full_config)
|
||||||
|
if not result:
|
||||||
|
return {cls.key: None}
|
||||||
|
|
||||||
async def check(self, obj):
|
async def check(self, obj):
|
||||||
from ..dispatcher import Dispatcher
|
from ..dispatcher import Dispatcher
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ class AbstractFilter(abc.ABC):
|
||||||
|
|
||||||
class BaseFilter(AbstractFilter):
|
class BaseFilter(AbstractFilter):
|
||||||
"""
|
"""
|
||||||
Abstract class for filters with default validator
|
Base class for filters with default validator
|
||||||
"""
|
"""
|
||||||
key = None
|
key = None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue