mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +00:00
Added MagicData filter
This commit is contained in:
parent
2d21cb22f1
commit
3f2bcbd8d1
3 changed files with 100 additions and 14 deletions
|
|
@ -4,6 +4,7 @@ from .base import BaseFilter
|
||||||
from .command import Command, CommandObject
|
from .command import Command, CommandObject
|
||||||
from .content_types import ContentTypesFilter
|
from .content_types import ContentTypesFilter
|
||||||
from .exception import ExceptionMessageFilter, ExceptionTypeFilter
|
from .exception import ExceptionMessageFilter, ExceptionTypeFilter
|
||||||
|
from .magic_data import MagicData
|
||||||
from .state import StateFilter
|
from .state import StateFilter
|
||||||
from .text import Text
|
from .text import Text
|
||||||
|
|
||||||
|
|
@ -17,45 +18,80 @@ __all__ = (
|
||||||
"ExceptionMessageFilter",
|
"ExceptionMessageFilter",
|
||||||
"ExceptionTypeFilter",
|
"ExceptionTypeFilter",
|
||||||
"StateFilter",
|
"StateFilter",
|
||||||
|
"MagicData",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_ALL_EVENTS_FILTERS: Tuple[Type[BaseFilter], ...] = (MagicData,)
|
||||||
|
_TELEGRAM_EVENTS_FILTERS: Tuple[Type[BaseFilter], ...] = (StateFilter,)
|
||||||
|
|
||||||
BUILTIN_FILTERS: Dict[str, Tuple[Type[BaseFilter], ...]] = {
|
BUILTIN_FILTERS: Dict[str, Tuple[Type[BaseFilter], ...]] = {
|
||||||
"message": (
|
"message": (
|
||||||
Text,
|
Text,
|
||||||
Command,
|
Command,
|
||||||
ContentTypesFilter,
|
ContentTypesFilter,
|
||||||
StateFilter,
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
),
|
),
|
||||||
"edited_message": (
|
"edited_message": (
|
||||||
Text,
|
Text,
|
||||||
Command,
|
Command,
|
||||||
ContentTypesFilter,
|
ContentTypesFilter,
|
||||||
StateFilter,
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
),
|
),
|
||||||
"channel_post": (
|
"channel_post": (
|
||||||
Text,
|
Text,
|
||||||
ContentTypesFilter,
|
ContentTypesFilter,
|
||||||
StateFilter,
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
),
|
),
|
||||||
"edited_channel_post": (
|
"edited_channel_post": (
|
||||||
Text,
|
Text,
|
||||||
ContentTypesFilter,
|
ContentTypesFilter,
|
||||||
StateFilter,
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
),
|
),
|
||||||
"inline_query": (
|
"inline_query": (
|
||||||
Text,
|
Text,
|
||||||
StateFilter,
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"chosen_inline_result": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
),
|
),
|
||||||
"chosen_inline_result": (StateFilter,),
|
|
||||||
"callback_query": (
|
"callback_query": (
|
||||||
Text,
|
Text,
|
||||||
StateFilter,
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"shipping_query": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"pre_checkout_query": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"poll": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"poll_answer": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"my_chat_member": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"chat_member": (
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
|
*_TELEGRAM_EVENTS_FILTERS,
|
||||||
|
),
|
||||||
|
"error": (
|
||||||
|
ExceptionMessageFilter,
|
||||||
|
ExceptionTypeFilter,
|
||||||
|
*_ALL_EVENTS_FILTERS,
|
||||||
),
|
),
|
||||||
"shipping_query": (StateFilter,),
|
|
||||||
"pre_checkout_query": (StateFilter,),
|
|
||||||
"poll": (StateFilter,),
|
|
||||||
"poll_answer": (StateFilter,),
|
|
||||||
"my_chat_member": (StateFilter,),
|
|
||||||
"chat_member": (StateFilter,),
|
|
||||||
"error": (ExceptionMessageFilter, ExceptionTypeFilter),
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
aiogram/dispatcher/filters/magic_data.py
Normal file
20
aiogram/dispatcher/filters/magic_data.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from magic_filter import AttrDict, MagicFilter
|
||||||
|
|
||||||
|
from aiogram.dispatcher.filters import BaseFilter
|
||||||
|
from aiogram.types import TelegramObject
|
||||||
|
|
||||||
|
|
||||||
|
class MagicData(BaseFilter):
|
||||||
|
magic_data: MagicFilter
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
|
async def __call__(self, event: TelegramObject, *args: Any, **kwargs: Any) -> bool:
|
||||||
|
return bool(
|
||||||
|
self.magic_data.resolve(
|
||||||
|
AttrDict({"event": event, **{k: v for k, v in enumerate(args)}, **kwargs})
|
||||||
|
)
|
||||||
|
)
|
||||||
30
tests/test_dispatcher/test_filters/test_magic_data.py
Normal file
30
tests/test_dispatcher/test_filters/test_magic_data.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import pytest
|
||||||
|
from magic_filter import AttrDict
|
||||||
|
|
||||||
|
from aiogram import F
|
||||||
|
from aiogram.dispatcher.filters import MagicData
|
||||||
|
from aiogram.types import Update
|
||||||
|
|
||||||
|
|
||||||
|
class TestMagicDataFilter:
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_call(self):
|
||||||
|
called = False
|
||||||
|
|
||||||
|
def check(value):
|
||||||
|
nonlocal called
|
||||||
|
called = True
|
||||||
|
|
||||||
|
assert isinstance(value, AttrDict)
|
||||||
|
assert value[0] == "foo"
|
||||||
|
assert value[1] == "bar"
|
||||||
|
assert value["spam"] is True
|
||||||
|
assert value.spam is True
|
||||||
|
return value
|
||||||
|
|
||||||
|
f = MagicData(magic_data=F.func(check))
|
||||||
|
result = await f(Update(update_id=123), "foo", "bar", spam=True)
|
||||||
|
|
||||||
|
assert called
|
||||||
|
assert isinstance(result, bool)
|
||||||
|
assert result
|
||||||
Loading…
Add table
Add a link
Reference in a new issue