mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Added possibility to get command magic result as handler arguments
This commit is contained in:
parent
e8ed1b64d7
commit
21b9eecfd3
4 changed files with 25 additions and 9 deletions
1
CHANGES/889.feature.rst
Normal file
1
CHANGES/889.feature.rst
Normal file
|
|
@ -0,0 +1 @@
|
|||
Added possibility to get command magic result as handler argument
|
||||
3
Makefile
3
Makefile
|
|
@ -94,9 +94,6 @@ test: test-run-services
|
|||
test-coverage: test-run-services
|
||||
mkdir -p $(reports_dir)/tests/
|
||||
$(py) pytest --cov=aiogram --cov-config .coveragerc --html=$(reports_dir)/tests/index.html tests/ --redis $(redis_connection)
|
||||
|
||||
.PHONY: test-coverage-report
|
||||
test-coverage-report:
|
||||
$(py) coverage html -d $(reports_dir)/coverage
|
||||
|
||||
.PHONY: test-coverage-view
|
||||
|
|
|
|||
|
|
@ -59,7 +59,10 @@ class Command(BaseFilter):
|
|||
command = await self.parse_command(text=text, bot=bot)
|
||||
except CommandException:
|
||||
return False
|
||||
return {"command": command}
|
||||
result = {"command": command}
|
||||
if command.magic_result and isinstance(command.magic_result, dict):
|
||||
result.update(command.magic_result)
|
||||
return result
|
||||
|
||||
def extract_command(self, text: str) -> CommandObject:
|
||||
# First step: separate command with arguments
|
||||
|
|
@ -110,20 +113,22 @@ class Command(BaseFilter):
|
|||
self.validate_prefix(command=command)
|
||||
await self.validate_mention(bot=bot, command=command)
|
||||
command = self.validate_command(command)
|
||||
self.do_magic(command=command)
|
||||
command = self.do_magic(command=command)
|
||||
return command
|
||||
|
||||
def do_magic(self, command: CommandObject) -> None:
|
||||
def do_magic(self, command: CommandObject) -> Any:
|
||||
if not self.command_magic:
|
||||
return
|
||||
if not self.command_magic.resolve(command):
|
||||
return command
|
||||
result = self.command_magic.resolve(command)
|
||||
if not result:
|
||||
raise CommandException("Rejected via magic filter")
|
||||
return replace(command, magic_result=result)
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True)
|
||||
class CommandObject:
|
||||
"""
|
||||
Instance of this object is always has command and it prefix.
|
||||
|
|
@ -140,6 +145,7 @@ class CommandObject:
|
|||
"""Command argument"""
|
||||
regexp_match: Optional[Match[str]] = field(repr=False, default=None)
|
||||
"""Will be presented match result if the command is presented as regexp in filter"""
|
||||
magic_result: Optional[Any] = field(repr=False, default=None)
|
||||
|
||||
@property
|
||||
def mentioned(self) -> bool:
|
||||
|
|
|
|||
|
|
@ -92,6 +92,18 @@ class TestCommandFilter:
|
|||
command = Command(commands=["test"])
|
||||
assert bool(await command(message=message, bot=bot)) is result
|
||||
|
||||
async def test_command_magic_result(self, bot: MockedBot):
|
||||
message = Message(
|
||||
message_id=0,
|
||||
text="/test 42",
|
||||
chat=Chat(id=42, type="private"),
|
||||
date=datetime.datetime.now(),
|
||||
)
|
||||
command = Command(commands=["test"], command_magic=(F.args.as_("args")))
|
||||
result = await command(message=message, bot=bot)
|
||||
assert "args" in result
|
||||
assert result["args"] == "42"
|
||||
|
||||
|
||||
class TestCommandObject:
|
||||
@pytest.mark.parametrize(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue