Skip to content

Commit

Permalink
Add chat_join_type to differentiate on three different types of new_c…
Browse files Browse the repository at this point in the history
…hat_members

Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
SpEcHiDe authored and wulan17 committed Aug 19, 2024
1 parent 1625e92 commit ce6b67c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pyrogram/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .business_schedule import BusinessSchedule
from .chat_action import ChatAction
from .chat_event_action import ChatEventAction
from .chat_join_type import ChatJoinType
from .chat_member_status import ChatMemberStatus
from .chat_members_filter import ChatMembersFilter
from .chat_type import ChatType
Expand All @@ -45,6 +46,7 @@
'BusinessSchedule',
'ChatAction',
'ChatEventAction',
'ChatJoinType',
'ChatMemberStatus',
'ChatMembersFilter',
'ChatType',
Expand Down
34 changes: 34 additions & 0 deletions pyrogram/enums/chat_join_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from enum import auto

from .auto_name import AutoName


class ChatJoinType(AutoName):
"""How the service message :obj:`~pyrogram.enums.MessageServiceType.NEW_CHAT_MEMBERS` was used for the member to join the chat."""

BY_ADD = auto()
"A new member joined the chat via an invite link"

BY_LINK = auto()
"A new member joined the chat via an invite link"

BY_REQUEST = auto()
"A new member was accepted to the chat by an administrator"
3 changes: 0 additions & 3 deletions pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class MessageServiceType(AutoName):
NEW_CHAT_MEMBERS = auto()
"New members join"

CHAT_JOINED_BY_REQUEST = auto()
"a member chat join request approved by admin."

LEFT_CHAT_MEMBERS = auto()
"Left chat members"

Expand Down
13 changes: 12 additions & 1 deletion pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ class Message(Object, Update):
from_scheduled (``bool``, *optional*):
Message is a scheduled message and has been sent.
chat_join_type (:obj:`~pyrogram.enums.ChatJoinType`, *optional*):
The message is a service message of the type :obj:`~pyrogram.enums.MessageServiceType.NEW_CHAT_MEMBERS`.
This field will contain the enumeration type of how the user had joined the chat.
"""

# TODO: Add game missing field, Also connected_website
Expand Down Expand Up @@ -537,6 +541,7 @@ def __init__(
"types.ForceReply"
] = None,
reactions: List["types.Reaction"] = None,
chat_join_type: "enums.ChatJoinType" = None,
raw: "raw.types.Message" = None
):
super().__init__(client)
Expand Down Expand Up @@ -643,6 +648,7 @@ def __init__(
self.successful_payment = successful_payment
self.payment_refunded = payment_refunded
self.reactions = reactions
self.chat_join_type = chat_join_type
self.raw = raw

async def wait_for_click(
Expand Down Expand Up @@ -752,19 +758,23 @@ async def _parse(
boosts_applied = None

service_type = None
chat_join_type = None

from_user = types.User._parse(client, users.get(user_id, None))
sender_chat = types.Chat._parse(client, message, users, chats, is_chat=False) if not from_user else None

if isinstance(action, raw.types.MessageActionChatAddUser):
new_chat_members = [types.User._parse(client, users[i]) for i in action.users]
service_type = enums.MessageServiceType.NEW_CHAT_MEMBERS
chat_join_type = enums.ChatJoinType.BY_ADD
elif isinstance(action, raw.types.MessageActionChatJoinedByLink):
new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])]
service_type = enums.MessageServiceType.NEW_CHAT_MEMBERS
chat_join_type = enums.ChatJoinType.BY_LINK
elif isinstance(action, raw.types.MessageActionChatJoinedByRequest):
chat_joined_by_request = types.ChatJoinedByRequest()
service_type = enums.MessageServiceType.CHAT_JOINED_BY_REQUEST
service_type = enums.MessageServiceType.NEW_CHAT_MEMBERS
chat_join_type = enums.ChatJoinType.BY_REQUEST
elif isinstance(action, raw.types.MessageActionChatDeleteUser):
left_chat_member = types.User._parse(client, users[action.user_id])
service_type = enums.MessageServiceType.LEFT_CHAT_MEMBERS
Expand Down Expand Up @@ -889,6 +899,7 @@ async def _parse(
payment_refunded=payment_refunded,
boosts_applied=boosts_applied,
raw=message,
chat_join_type=chat_join_type,
client=client
# TODO: supergroup_chat_created
)
Expand Down

0 comments on commit ce6b67c

Please sign in to comment.