Fix docs and examples (#864)

* Update magic_data Allowed handlers

* Fix ChatMemberUpdated example

* Fix examples with `.in_(...)` and delete with `@`

* Fix commands in examples

* Change List to Set
This commit is contained in:
Gabben 2022-03-26 15:30:46 +00:00 committed by GitHub
parent b50500e28d
commit a37bbba38c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 14 deletions

View file

@ -86,10 +86,10 @@ Handle user leave or join events
from aiogram.dispatcher.filters import IS_MEMBER, IS_NOT_MEMBER
@router.chat_member(chat_member_updated=IS_MEMBER >> IS_NOT_MEMBER)
@router.chat_member(member_status_changed=IS_MEMBER >> IS_NOT_MEMBER)
async def on_user_leave(event: ChatMemberUpdated): ...
@router.chat_member(chat_member_updated=IS_NOT_MEMBER >> IS_MEMBER)
@router.chat_member(member_status_changed=IS_NOT_MEMBER >> IS_MEMBER)
async def on_user_join(event: ChatMemberUpdated): ...
Or construct your own terms via using pre-defined set of statuses and transitions.

View file

@ -31,4 +31,13 @@ Allowed update types for this filter:
- :code:`channel_post`
- :code:`edited_channel_post`
- :code:`inline_query`
- :code:`chosen_inline_result`
- :code:`callback_query`
- :code:`shipping_query`
- :code:`pre_checkout_query`
- :code:`poll`
- :code:`poll_answer`
- :code:`my_chat_member`
- :code:`chat_member`
- :code:`chat_join_request`
- :code:`error`

View file

@ -61,9 +61,8 @@ Can be used as method named :code:`in_` or as matmul operator :code:`@` with any
.. code-block:: python
F.from_user.id.in_(42, 1000, 123123) # lambda query: query.from_user.id in {42, 1000, 123123}
F.data.in_('foo', 'bar', 'baz') # lambda query: query.data in {'foo', 'bar', 'baz'}
F.text @ {'foo', 'bar'} # lambda message: message.text in {'foo', 'bar'}
F.from_user.id.in_({42, 1000, 123123}) # lambda query: query.from_user.id in {42, 1000, 123123}
F.data.in_({'foo', 'bar', 'baz'}) # lambda query: query.data in {'foo', 'bar', 'baz'}
Contains
--------
@ -117,7 +116,7 @@ All operations can be combined via bitwise and/or operators - :code:`&`/:code:`|
(F.from_user.id == 42) & (F.text == 'admin')
F.text.startswith('a') | F.text.endswith('b')
(F.from_user.id @ {42, 777, 911}) & (F.text.startswith('!') | F.text.startswith('/')) & F.text.contains('ban')
(F.from_user.id.in_({42, 777, 911})) & (F.text.startswith('!') | F.text.startswith('/')) & F.text.contains('ban')
Attribute modifiers - string manipulations
@ -130,7 +129,7 @@ Can be used only with string attributes.
.. code-block:: python
F.text.lower() == 'test' # lambda message: message.text.lower() == 'test'
F.text.upper().in_('FOO', 'BAR') # lambda message: message.text.upper() in {'FOO', 'BAR'}
F.text.upper().in_({'FOO', 'BAR'}) # lambda message: message.text.upper() in {'FOO', 'BAR'}
F.text.len() == 5 # lambda message: len(message.text) == 5
@ -157,7 +156,7 @@ Usage in *aiogram*
@router.message(F.text == 'hello')
@router.inline_query(F.data == 'button:1')
@router.message(F.text.startswith('foo'))
@router.message(F.content_type.in_('text', 'sticker'))
@router.message(F.content_type.in_({'text', 'sticker'}))
@router.message(F.text.regexp(r'\d+'))
...

View file

@ -10,7 +10,7 @@ dp = Dispatcher()
logger = logging.getLogger(__name__)
@dp.message(commands={"start"})
@dp.message(commands=["start"])
async def command_start_handler(message: Message) -> None:
"""
This handler receive messages with `/start` command
@ -23,7 +23,7 @@ async def command_start_handler(message: Message) -> None:
@dp.message()
async def echo_handler(message: types.Message) -> Any:
async def echo_handler(message: types.Message) -> None:
"""
Handler will forward received message back to the sender

View file

@ -18,7 +18,7 @@ class Form(StatesGroup):
language = State()
@form_router.message(commands={"start"})
@form_router.message(commands=["start"])
async def command_start(message: Message, state: FSMContext) -> None:
await state.set_state(Form.name)
await message.answer(
@ -27,7 +27,7 @@ async def command_start(message: Message, state: FSMContext) -> None:
)
@form_router.message(commands={"cancel"})
@form_router.message(commands=["cancel"])
@form_router.message(F.text.casefold() == "cancel")
async def cancel_handler(message: Message, state: FSMContext) -> None:
"""

View file

@ -40,7 +40,7 @@ def is_bot_token(value: str) -> Union[bool, Dict[str, Any]]:
@main_router.message(Command(commands=["add"], command_magic=F.args.func(is_bot_token)))
async def command_add_bot(message: Message, command: CommandObject, bot: Bot):
async def command_add_bot(message: Message, command: CommandObject, bot: Bot) -> Any:
new_bot = Bot(token=command.args, session=bot.session)
try:
bot_user = await new_bot.get_me()

View file

@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
@dp.message(commands={"start"})
@dp.message(commands=["start"])
async def command_start_handler(message: Message) -> None:
"""
This handler receive messages with `/start` command