mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Add chat member tools (#1527)
* feat: add ChatMemberAdapter * chore: apply formatter * docs: added changelog * docs: rm redundant import * feat: add pre-defined groups
This commit is contained in:
parent
5f05dfc664
commit
46e033e6da
4 changed files with 200 additions and 6 deletions
|
|
@ -26,7 +26,7 @@ On this page, you can read about the changes made in relation to the last stable
|
|||
Feel free to contribute to this page, if you find something that is not mentioned here.
|
||||
|
||||
Dependencies
|
||||
==========
|
||||
============
|
||||
|
||||
- The dependencies required for :code:`i18n` are no longer part of the default package.
|
||||
If your application uses translation functionality, be sure to add an optional dependency:
|
||||
|
|
@ -178,7 +178,7 @@ Here are some usage examples:
|
|||
|
||||
- Creating an object from a dictionary representation of an object
|
||||
|
||||
.. code-block::
|
||||
.. code-block::
|
||||
|
||||
# Version 2.x
|
||||
message_dict = {"id": 42, ...}
|
||||
|
|
@ -188,6 +188,8 @@ Here are some usage examples:
|
|||
print(type(message_obj))
|
||||
# <class 'aiogram.types.message.Message'>
|
||||
|
||||
.. code-block::
|
||||
|
||||
# Version 3.x
|
||||
message_dict = {"id": 42, ...}
|
||||
message_obj = Message.model_validate(message_dict)
|
||||
|
|
@ -198,17 +200,20 @@ Here are some usage examples:
|
|||
|
||||
- Creating a json representation of an object
|
||||
|
||||
.. code-block::
|
||||
.. code-block::
|
||||
|
||||
# Version 2.x
|
||||
async def handler(message: Message) -> None:
|
||||
# Version 2.x
|
||||
message_json = message.as_json()
|
||||
print(message_json)
|
||||
# {"id": 42, ...}
|
||||
print(type(message_json))
|
||||
# <class 'str'>
|
||||
|
||||
# Version 3.x
|
||||
.. code-block::
|
||||
|
||||
# Version 3.x
|
||||
async def handler(message: Message) -> None:
|
||||
message_json = json.dumps(deserialize_telegram_object_to_python(message))
|
||||
print(message_json)
|
||||
# {"id": 42, ...}
|
||||
|
|
@ -217,7 +222,7 @@ Here are some usage examples:
|
|||
|
||||
- Creating a dictionary representation of an object
|
||||
|
||||
.. code-block::
|
||||
.. code-block::
|
||||
|
||||
async def handler(message: Message) -> None:
|
||||
# Version 2.x
|
||||
|
|
@ -227,9 +232,65 @@ Here are some usage examples:
|
|||
print(type(message_dict))
|
||||
# <class 'dict'>
|
||||
|
||||
.. code-block::
|
||||
|
||||
async def handler(message: Message) -> None:
|
||||
# Version 3.x
|
||||
message_dict = deserialize_telegram_object_to_python(message)
|
||||
print(message_dict)
|
||||
# {"id": 42, ...}
|
||||
print(type(message_dict))
|
||||
# <class 'dict'>
|
||||
|
||||
|
||||
ChatMember tools
|
||||
================
|
||||
|
||||
- Now :class:`aiogram.types.chat_member.ChatMember` no longer contains tools to resolve an object with the appropriate status.
|
||||
|
||||
.. code-block::
|
||||
|
||||
# Version 2.x
|
||||
from aiogram.types import ChatMember
|
||||
|
||||
chat_member = ChatMember.resolve(**dict_data)
|
||||
|
||||
.. code-block::
|
||||
|
||||
# Version 3.x
|
||||
from aiogram.utils.chat_member import ChatMemberAdapter
|
||||
|
||||
chat_member = ChatMemberAdapter.validate_python(dict_data)
|
||||
|
||||
|
||||
- Now :class:`aiogram.types.chat_member.ChatMember` and all its child classes no longer
|
||||
contain methods for checking for membership in certain logical groups.
|
||||
As a substitute, you can use pre-defined groups or create such groups yourself
|
||||
and check their entry using the :func:`isinstance` function
|
||||
|
||||
.. code-block::
|
||||
|
||||
# Version 2.x
|
||||
|
||||
if chat_member.is_chat_admin():
|
||||
print("ChatMember is chat admin")
|
||||
|
||||
if chat_member.is_chat_member():
|
||||
print("ChatMember is in the chat")
|
||||
|
||||
.. code-block::
|
||||
|
||||
# Version 3.x
|
||||
|
||||
from aiogram.utils.chat_member import ADMINS, MEMBERS
|
||||
|
||||
if isinstance(chat_member, ADMINS):
|
||||
print("ChatMember is chat admin")
|
||||
|
||||
if isinstance(chat_member, MEMBERS):
|
||||
print("ChatMember is in the chat")
|
||||
|
||||
.. note::
|
||||
You also can independently create group similar to ADMINS that fits the logic of your application.
|
||||
|
||||
E.g., you can create a PUNISHED group and include banned and restricted members there!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue