Skip to content

Commit f6e3e92

Browse files
authored
add three more methods for phone calls. (#37)
* temporary workaround https://t.me/c/1220993104/1379063
1 parent a1da2c5 commit f6e3e92

File tree

9 files changed

+305
-1
lines changed

9 files changed

+305
-1
lines changed

compiler/docs/compiler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,12 @@ def get_title_list(s: str) -> list:
345345
""",
346346
phone="""
347347
Phone
348+
create_video_chat
349+
discard_group_call
350+
get_video_chat_rtmp_url
348351
invite_group_call_participants
349352
load_group_call_participants
353+
350354
""",
351355
stickers="""
352356
Stickers
@@ -608,6 +612,7 @@ def get_title_list(s: str) -> list:
608612
VideoChatParticipantsInvited
609613
VideoChatScheduled
610614
VideoChatStarted
615+
RtmpUrl
611616
""",
612617
)
613618

docs/source/releases/changes-in-this-fork.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
1414
| Scheme layer used: 181 |
1515
+------------------------+
1616

17+
- Added the methods :meth:`~pyrogram.Client.create_video_chat`, :meth:`~pyrogram.Client.discard_group_call`, :meth:`~pyrogram.Client.get_video_chat_rtmp_url` and the type :obj:`~pyrogram.types.RtmpUrl` (`#37 <https://github.com/TelegramPlayGround/pyrogram/pull/37>`_).
1718
- Added :meth:`~Client.on_story` to listen to story updates.
1819
- Ability to run in `replit` environment without creating `a deployment <https://ask.replit.com/t/pyrogram-network-issue/33679/46>`_. Set the environment variable ``PYROGRAM_REPLIT_NWTRAFIK_PORT`` value to ``5222`` if you want to connect to Production Telegram Servers, **OR** Set the environment variable ``PYROGRAM_REPLIT_WNTRAFIK_PORT`` value to ``5223`` if you want to connect to Test Telegram Servers, before starting the :obj:`~pyrogram.Client`.
1920
- Added the :meth:`~pyrogram.Client.invite_group_call_participants` (`#35 <https://github.com/TelegramPlayGround/pyrogram/pull/35>`_).

pyrogram/methods/phone/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919

2020
from .load_group_call_participants import LoadGroupCallParticipants
2121
from .invite_group_call_participants import InviteGroupCallParticipants
22+
from .create_video_chat import CreateVideoChat
23+
from .discard_group_call import DiscardGroupCall
24+
from .get_video_chat_rtmp_url import GetVideoChatRtmpUrl
2225

2326

2427
class Phone(
2528
InviteGroupCallParticipants,
2629
LoadGroupCallParticipants,
30+
CreateVideoChat,
31+
DiscardGroupCall,
32+
GetVideoChatRtmpUrl,
2733
):
2834
pass
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
from datetime import datetime
22+
23+
import pyrogram
24+
from pyrogram import types, raw, utils
25+
26+
27+
class CreateVideoChat:
28+
async def create_video_chat(
29+
self: "pyrogram.Client",
30+
chat_id: Union[int, str],
31+
title: str = None,
32+
start_date: datetime = utils.zero_datetime(),
33+
is_rtmp_stream: bool = None
34+
) -> "types.Message":
35+
"""Creates a video chat (a group call bound to a chat).
36+
37+
Available only for basic groups, supergroups and channels; requires can_manage_video_chats administrator right.
38+
39+
.. include:: /_includes/usable-by/users.rst
40+
41+
Parameters:
42+
chat_id (``int`` | ``str``):
43+
Unique identifier (int) or username (str) of the target chat in which the video chat will be created. A chat can be either a basic group, supergroup or a channel.
44+
45+
title (``str``, *optional*):
46+
Group call title; if empty, chat title will be used.
47+
48+
start_date (:py:obj:`~datetime.datetime`, *optional*):
49+
Point in time (Unix timestamp) when the group call is supposed to be started by an administrator; 0 to start the video chat immediately. The date must be at least 10 seconds and at most 8 days in the future.
50+
51+
is_rtmp_stream (``bool``, *optional*):
52+
Pass true to create an RTMP stream instead of an ordinary video chat; requires owner privileges.
53+
54+
Returns:
55+
:obj:`~pyrogram.types.Message`: On success, the sent service message is returned.
56+
57+
Example:
58+
.. code-block:: python
59+
60+
await app.create_video_chat(chat_id)
61+
62+
"""
63+
peer = await self.resolve_peer(chat_id)
64+
65+
if not isinstance(peer, (raw.types.InputPeerChat, raw.types.InputPeerChannel)):
66+
raise ValueError(
67+
"Target chat should be group, supergroup or channel."
68+
)
69+
70+
r = await self.invoke(
71+
raw.functions.phone.CreateGroupCall(
72+
rtmp_stream=is_rtmp_stream,
73+
peer=peer,
74+
# TODO: temp. workaround
75+
random_id=self.rnd_id() >> 32,
76+
title=title,
77+
schedule_date=utils.datetime_to_timestamp(start_date),
78+
)
79+
)
80+
81+
for i in r.updates:
82+
if isinstance(
83+
i,
84+
(
85+
raw.types.UpdateNewChannelMessage,
86+
raw.types.UpdateNewMessage,
87+
raw.types.UpdateNewScheduledMessage,
88+
),
89+
):
90+
return await types.Message._parse(
91+
self,
92+
i.message,
93+
{i.id: i for i in r.users},
94+
{i.id: i for i in r.chats},
95+
is_scheduled=isinstance(i, raw.types.UpdateNewScheduledMessage),
96+
)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram import types, raw
23+
24+
25+
class DiscardGroupCall:
26+
async def discard_group_call(
27+
# TODO
28+
self: "pyrogram.Client",
29+
chat_id: Union[int, str],
30+
) -> "types.Message":
31+
"""Terminate a group/channel call or livestream
32+
33+
.. include:: /_includes/usable-by/users.rst
34+
35+
Parameters:
36+
chat_id (``int`` | ``str``):
37+
Unique identifier (int) or username (str) of the target chat. A chat can be either a basic group, supergroup or a channel.
38+
39+
Returns:
40+
:obj:`~pyrogram.types.Message`: On success, the sent service message is returned.
41+
42+
Example:
43+
.. code-block:: python
44+
45+
await app.discard_group_call(chat_id)
46+
47+
"""
48+
peer = await self.resolve_peer(chat_id)
49+
50+
if isinstance(peer, raw.types.InputPeerChannel):
51+
r = await self.invoke(raw.functions.channels.GetFullChannel(channel=peer))
52+
elif isinstance(peer, raw.types.InputPeerChat):
53+
r = await self.invoke(
54+
raw.functions.messages.GetFullChat(chat_id=peer.chat_id)
55+
)
56+
else:
57+
raise ValueError("Target chat should be group, supergroup or channel.")
58+
59+
call = r.full_chat.call
60+
61+
if call is None:
62+
raise ValueError("No active group call at this chat.")
63+
64+
r = await self.invoke(
65+
raw.functions.phone.DiscardGroupCall(
66+
call=call
67+
)
68+
)
69+
70+
for i in r.updates:
71+
if isinstance(
72+
i,
73+
(
74+
raw.types.UpdateNewChannelMessage,
75+
raw.types.UpdateNewMessage,
76+
raw.types.UpdateNewScheduledMessage,
77+
),
78+
):
79+
return await types.Message._parse(
80+
self,
81+
i.message,
82+
{i.id: i for i in r.users},
83+
{i.id: i for i in r.chats},
84+
is_scheduled=isinstance(i, raw.types.UpdateNewScheduledMessage),
85+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram import types, raw
23+
24+
25+
class GetVideoChatRtmpUrl:
26+
async def get_video_chat_rtmp_url(
27+
self: "pyrogram.Client",
28+
chat_id: Union[int, str],
29+
replace: bool = False
30+
) -> "types.RtmpUrl":
31+
"""Returns RTMP URL for streaming to the chat; requires owner privileges.
32+
33+
.. include:: /_includes/usable-by/users.rst
34+
35+
Parameters:
36+
chat_id (``int`` | ``str``):
37+
Unique identifier (int) or username (str) of the target chat. A chat can be either a basic group, supergroup or a channel.
38+
39+
replace (``bool``, *optional*):
40+
Whether to replace the previous stream key or simply return the existing one. Defaults to False, i.e., return the existing one.
41+
42+
Returns:
43+
:obj:`~pyrogram.types.RtmpUrl`: On success, the RTMP URL and stream key is returned.
44+
45+
Example:
46+
.. code-block:: python
47+
48+
await app.get_stream_rtmp_url(chat_id)
49+
50+
"""
51+
peer = await self.resolve_peer(chat_id)
52+
53+
if not isinstance(peer, (raw.types.InputPeerChat, raw.types.InputPeerChannel)):
54+
raise ValueError("Target chat should be group, supergroup or channel.")
55+
56+
r = await self.invoke(
57+
raw.functions.phone.GetGroupCallStreamRtmpUrl(
58+
peer=peer,
59+
revoke=replace
60+
)
61+
)
62+
63+
return types.RtmpUrl._parse(r)

pyrogram/methods/phone/invite_group_call_participants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import Union, List
2020

2121
import pyrogram
22-
from pyrogram import types, raw, utils
22+
from pyrogram import types, raw
2323

2424

2525
class InviteGroupCallParticipants:

pyrogram/types/user_and_chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from .video_chat_participants_invited import VideoChatParticipantsInvited
4545
from .video_chat_scheduled import VideoChatScheduled
4646
from .video_chat_started import VideoChatStarted
47+
from .rtmp_url import RtmpUrl
4748

4849
__all__ = [
4950
"Birthdate",
@@ -74,4 +75,5 @@
7475
"VideoChatParticipantsInvited",
7576
"VideoChatScheduled",
7677
"VideoChatStarted",
78+
"RtmpUrl",
7779
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from pyrogram import raw
20+
from ..object import Object
21+
22+
23+
class RtmpUrl(Object):
24+
"""Represents an RTMP URL and stream key to be used in streaming software.
25+
26+
Parameters:
27+
url (``str``):
28+
The URL.
29+
30+
stream_key (``str``):
31+
Stream key.
32+
33+
"""
34+
35+
def __init__(self, *, url: str, stream_key: str):
36+
super().__init__(None)
37+
38+
self.url = url
39+
self.stream_key = stream_key
40+
41+
@staticmethod
42+
def _parse(rtmp_url: "raw.types.GroupCallStreamRtmpUrl") -> "RtmpUrl":
43+
return RtmpUrl(
44+
url=rtmp_url.url,
45+
stream_key=rtmp_url.key
46+
)

0 commit comments

Comments
 (0)