From 975ff219f2279065a2246b0cc2b857a0a6fb32ca Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 17 Mar 2021 13:23:18 +0100 Subject: [PATCH] Add support for voice chat service messages --- pyrogram/types/messages_and_media/message.py | 30 ++++++++++- pyrogram/types/user_and_chats/__init__.py | 8 ++- .../types/user_and_chats/voice_chat_ended.py | 41 +++++++++++++++ .../voice_chat_members_invited.py | 50 +++++++++++++++++++ .../user_and_chats/voice_chat_started.py | 29 +++++++++++ 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 pyrogram/types/user_and_chats/voice_chat_ended.py create mode 100644 pyrogram/types/user_and_chats/voice_chat_members_invited.py create mode 100644 pyrogram/types/user_and_chats/voice_chat_started.py diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 65e51e45..91c5f9d5 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -264,6 +264,15 @@ class Message(Object, Update): E.g.: "/start 1 2 3" would produce ["start", "1", "2", "3"]. Only applicable when using :obj:`~pyrogram.filters.command`. + voice_chat_started (:obj:`~pyrogram.types.VoiceChatStarted`, *optional*): + Service message: the voice chat started. + + voice_chat_ended (:obj:`~pyrogram.types.VoiceChatEnded`, *optional*): + Service message: the voice chat has ended. + + voice_chat_members_invited (:obj:`~pyrogram.types.VoiceChatParticipantsInvited`, *optional*): + Service message: new members were invited to the voice chat. + reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup` | :obj:`~pyrogram.types.ReplyKeyboardMarkup` | :obj:`~pyrogram.types.ReplyKeyboardRemove` | :obj:`~pyrogram.types.ForceReply`, *optional*): Additional interface options. An object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. @@ -335,6 +344,9 @@ class Message(Object, Update): outgoing: bool = None, matches: List[Match] = None, command: List[str] = None, + voice_chat_started: "types.VoiceChatStarted" = None, + voice_chat_ended: "types.VoiceChatEnded" = None, + voice_chat_members_invited: "types.VoiceChatMemberInvited" = None, reply_markup: Union[ "types.InlineKeyboardMarkup", "types.ReplyKeyboardMarkup", @@ -402,6 +414,9 @@ class Message(Object, Update): self.matches = matches self.command = command self.reply_markup = reply_markup + self.voice_chat_started = voice_chat_started + self.voice_chat_ended = voice_chat_ended + self.voice_chat_members_invited = voice_chat_members_invited @staticmethod async def _parse( @@ -427,6 +442,9 @@ class Message(Object, Update): group_chat_created = None channel_chat_created = None new_chat_photo = None + voice_chat_started = None + voice_chat_ended = None + voice_chat_members_invited = None if isinstance(action, raw.types.MessageActionChatAddUser): new_chat_members = [types.User._parse(client, users[i]) for i in action.users] @@ -448,6 +466,13 @@ class Message(Object, Update): channel_chat_created = True elif isinstance(action, raw.types.MessageActionChatEditPhoto): new_chat_photo = types.Photo._parse(client, action.photo) + elif isinstance(action, raw.types.MessageActionGroupCall): + if action.duration: + voice_chat_ended = types.VoiceChatEnded._parse(action) + else: + voice_chat_started = types.VoiceChatStarted() + elif isinstance(action, raw.types.MessageActionInviteToGroupCall): + voice_chat_members_invited = types.VoiceChatMembersInvited._parse(client, action, users) user = utils.get_raw_peer_id(message.from_id) or utils.get_raw_peer_id(message.peer_id) from_user = types.User._parse(client, users.get(user, None)) @@ -469,7 +494,10 @@ class Message(Object, Update): migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None, group_chat_created=group_chat_created, channel_chat_created=channel_chat_created, - client=client + client=client, + voice_chat_started=voice_chat_started, + voice_chat_ended=voice_chat_ended, + voice_chat_members_invited=voice_chat_members_invited # TODO: supergroup_chat_created ) diff --git a/pyrogram/types/user_and_chats/__init__.py b/pyrogram/types/user_and_chats/__init__.py index 7c4a65da..3fc4eeaf 100644 --- a/pyrogram/types/user_and_chats/__init__.py +++ b/pyrogram/types/user_and_chats/__init__.py @@ -29,6 +29,9 @@ from .dialog import Dialog from .invite_link_importer import InviteLinkImporter from .restriction import Restriction from .user import User +from .voice_chat_ended import VoiceChatEnded +from .voice_chat_members_invited import VoiceChatMembersInvited +from .voice_chat_started import VoiceChatStarted __all__ = [ "Chat", @@ -43,5 +46,8 @@ __all__ = [ "ChatEventFilter", "ChatInviteLink", "InviteLinkImporter", - "ChatAdminWithInviteLinks" + "ChatAdminWithInviteLinks", + "VoiceChatStarted", + "VoiceChatEnded", + "VoiceChatMembersInvited" ] diff --git a/pyrogram/types/user_and_chats/voice_chat_ended.py b/pyrogram/types/user_and_chats/voice_chat_ended.py new file mode 100644 index 00000000..febb8137 --- /dev/null +++ b/pyrogram/types/user_and_chats/voice_chat_ended.py @@ -0,0 +1,41 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# 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 . + +from pyrogram import raw +from ..object import Object + + +class VoiceChatEnded(Object): + """A service message about a voice chat ended in the chat. + + Parameters: + duration (``int``): + Voice chat duration; in seconds. + """ + + def __init__( + self, *, + duration: int + ): + super().__init__() + + self.duration = duration + + @staticmethod + def _parse(action: "raw.types.MessageActionGroupCall") -> "VoiceChatEnded": + return VoiceChatEnded(duration=action.duration) diff --git a/pyrogram/types/user_and_chats/voice_chat_members_invited.py b/pyrogram/types/user_and_chats/voice_chat_members_invited.py new file mode 100644 index 00000000..0a093cbf --- /dev/null +++ b/pyrogram/types/user_and_chats/voice_chat_members_invited.py @@ -0,0 +1,50 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# 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 . + +from typing import List, Dict + +from pyrogram import raw, types +from ..object import Object + + +class VoiceChatMembersInvited(Object): + """A service message about new members invited to a voice chat. + + + Parameters: + users (List of :obj:`~pyrogram.types.User`): + New members that were invited to the voice chat. + """ + + def __init__( + self, *, + users: List["types.User"] + ): + super().__init__() + + self.users = users + + @staticmethod + def _parse( + client, + action: "raw.types.MessageActionInviteToGroupCall", + users: Dict[int, "raw.types.User"] + ) -> "VoiceChatMembersInvited": + users = [types.User._parse(client, users[i]) for i in action.users] + + return VoiceChatMembersInvited(users=users) diff --git a/pyrogram/types/user_and_chats/voice_chat_started.py b/pyrogram/types/user_and_chats/voice_chat_started.py new file mode 100644 index 00000000..6ac546dc --- /dev/null +++ b/pyrogram/types/user_and_chats/voice_chat_started.py @@ -0,0 +1,29 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# 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 . + +from ..object import Object + + +class VoiceChatStarted(Object): + """A service message about a voice chat started in the chat. + + Currently holds no information. + """ + + def __init__(self): + super().__init__()