mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 09:55:21 +00:00
Add more filters.
This commit is contained in:
parent
e1b60dfe57
commit
8b28ba8fc4
3 changed files with 89 additions and 15 deletions
|
|
@ -305,8 +305,8 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.message_handlers.register(callback, filters_set)
|
self.message_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def message_handler(self, *, commands=None, regexp=None, content_types=None, func=None, state=None,
|
def message_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, state=None,
|
||||||
custom_filters=None, **kwargs):
|
**kwargs):
|
||||||
"""
|
"""
|
||||||
Decorator for messages handler
|
Decorator for messages handler
|
||||||
|
|
||||||
|
|
@ -409,8 +409,8 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.edited_message_handlers.register(callback, filters_set)
|
self.edited_message_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def edited_message_handler(self, *, commands=None, regexp=None, content_types=None, func=None, state=None,
|
def edited_message_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, state=None,
|
||||||
custom_filters=None, **kwargs):
|
**kwargs):
|
||||||
"""
|
"""
|
||||||
Analog of message_handler but only for edited messages
|
Analog of message_handler but only for edited messages
|
||||||
|
|
||||||
|
|
@ -472,8 +472,8 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.channel_post_handlers.register(callback, filters_set)
|
self.channel_post_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def channel_post_handler(self, *, commands=None, regexp=None, content_types=None, func=None, state=None,
|
def channel_post_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, state=None,
|
||||||
custom_filters=None, **kwargs):
|
**kwargs):
|
||||||
"""
|
"""
|
||||||
Register channels posts handler
|
Register channels posts handler
|
||||||
|
|
||||||
|
|
@ -526,8 +526,8 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.edited_channel_post_handlers.register(callback, filters_set)
|
self.edited_channel_post_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def edited_channel_post_handler(self, *, commands=None, regexp=None, content_types=None, func=None, state=None,
|
def edited_channel_post_handler(self, *custom_filters, commands=None, regexp=None, content_types=None, func=None, state=None,
|
||||||
custom_filters=None, **kwargs):
|
**kwargs):
|
||||||
"""
|
"""
|
||||||
Register handler for edited channels posts
|
Register handler for edited channels posts
|
||||||
|
|
||||||
|
|
@ -576,7 +576,7 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.inline_query_handlers.register(callback, filters_set)
|
self.inline_query_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def inline_handler(self, *, func=None, state=None, custom_filters=None, **kwargs):
|
def inline_handler(self, *custom_filters, func=None, state=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Handle inline query
|
Handle inline query
|
||||||
|
|
||||||
|
|
@ -627,7 +627,7 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.chosen_inline_result_handlers.register(callback, filters_set)
|
self.chosen_inline_result_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def chosen_inline_handler(self, *, func=None, state=None, custom_filters=None, **kwargs):
|
def chosen_inline_handler(self, *custom_filters, func=None, state=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Register chosen inline handler
|
Register chosen inline handler
|
||||||
|
|
||||||
|
|
@ -678,7 +678,7 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.callback_query_handlers.register(callback, filters_set)
|
self.callback_query_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def callback_query_handler(self, *, func=None, state=None, custom_filters=None, **kwargs):
|
def callback_query_handler(self, *custom_filters, func=None, state=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Add callback query handler
|
Add callback query handler
|
||||||
|
|
||||||
|
|
@ -728,7 +728,7 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.shipping_query_handlers.register(callback, filters_set)
|
self.shipping_query_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def shipping_query_handler(self, *, func=None, state=None, custom_filters=None, **kwargs):
|
def shipping_query_handler(self, *custom_filters, func=None, state=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Add shipping query handler
|
Add shipping query handler
|
||||||
|
|
||||||
|
|
@ -778,7 +778,7 @@ class Dispatcher:
|
||||||
**kwargs)
|
**kwargs)
|
||||||
self.pre_checkout_query_handlers.register(callback, filters_set)
|
self.pre_checkout_query_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def pre_checkout_query_handler(self, *, func=None, state=None, custom_filters=None, **kwargs):
|
def pre_checkout_query_handler(self, *custom_filters, func=None, state=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Add shipping query handler
|
Add shipping query handler
|
||||||
|
|
||||||
|
|
@ -817,7 +817,7 @@ class Dispatcher:
|
||||||
filters_set.append(ExceptionsFilter(exception))
|
filters_set.append(ExceptionsFilter(exception))
|
||||||
self.errors_handlers.register(callback, filters_set)
|
self.errors_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
def errors_handler(self, *, func=None, exception=None):
|
def errors_handler(self, func=None, exception=None):
|
||||||
"""
|
"""
|
||||||
Decorator for registering errors handler
|
Decorator for registering errors handler
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import asyncio
|
||||||
import inspect
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
@ -46,6 +47,23 @@ class AsyncFilter(Filter):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class AnyFilter(AsyncFilter):
|
||||||
|
def __init__(self, *filters: callable):
|
||||||
|
self.filters = filters
|
||||||
|
|
||||||
|
async def check(self, *args, **kwargs):
|
||||||
|
f = (check_filter(filter_, args, kwargs) for filter_ in self.filters)
|
||||||
|
return any(await asyncio.gather(*f))
|
||||||
|
|
||||||
|
|
||||||
|
class NotFilter(AsyncFilter):
|
||||||
|
def __init__(self, filter_: callable):
|
||||||
|
self.filter = filter_
|
||||||
|
|
||||||
|
async def check(self, *args, **kwargs):
|
||||||
|
return not await check_filter(self.filter, args, kwargs)
|
||||||
|
|
||||||
|
|
||||||
class CommandsFilter(AsyncFilter):
|
class CommandsFilter(AsyncFilter):
|
||||||
def __init__(self, commands):
|
def __init__(self, commands):
|
||||||
self.commands = commands
|
self.commands = commands
|
||||||
|
|
@ -81,7 +99,7 @@ class ContentTypeFilter(Filter):
|
||||||
|
|
||||||
def check(self, message):
|
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
|
||||||
|
|
||||||
|
|
||||||
class CancelFilter(Filter):
|
class CancelFilter(Filter):
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,62 @@ class ChatType(helper.Helper):
|
||||||
SUPER_GROUP = helper.Item() # supergroup
|
SUPER_GROUP = helper.Item() # supergroup
|
||||||
CHANNEL = helper.Item() # channel
|
CHANNEL = helper.Item() # channel
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _check(obj, chat_types) -> bool:
|
||||||
|
if not hasattr(obj, 'chat'):
|
||||||
|
return False
|
||||||
|
return obj.chat.type in chat_types
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_private(cls, obj) -> bool:
|
||||||
|
"""
|
||||||
|
Check chat is private
|
||||||
|
|
||||||
|
:param obj:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return cls._check(obj, [cls.PRIVATE])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_group(cls, obj) -> bool:
|
||||||
|
"""
|
||||||
|
Check chat is group
|
||||||
|
|
||||||
|
:param obj:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return cls._check(obj, [cls.GROUP])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_super_group(cls, obj) -> bool:
|
||||||
|
"""
|
||||||
|
Check chat is super-group
|
||||||
|
|
||||||
|
:param obj:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return cls._check(obj, [cls.SUPER_GROUP])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_group_or_super_group(cls, obj) -> bool:
|
||||||
|
"""
|
||||||
|
Check chat is group or super-group
|
||||||
|
|
||||||
|
:param obj:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return cls._check(obj, [cls.GROUP, cls.SUPER_GROUP])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_channel(cls, obj) -> bool:
|
||||||
|
"""
|
||||||
|
Check chat is channel
|
||||||
|
|
||||||
|
:param obj:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return cls._check(obj, [cls.CHANNEL])
|
||||||
|
|
||||||
|
|
||||||
class ChatActions(helper.Helper):
|
class ChatActions(helper.Helper):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue