Skip to content

Commit ca3a7fc

Browse files
author
Pyrogram-Mod - Dev
committed
Fix raw return types for all new methods since v2.0.107
- create_community: raw.base.Updates -> types.Message - toggle_community_collapsed: raw.base.Updates -> bool - send_ephemeral_message: raw.base.Updates -> types.Message - get_joined_communities: raw.base.messages.Chats -> types.List[types.Chat] - report_ephemeral_message: raw.base.ReportResult -> bool - get_ephemeral_callback_answer: raw.base.messages.BotCallbackAnswer -> types.CallbackAnswer - translate_rich_message: raw.base.messages.TranslatedRichMessage -> types.List[types.RichMessage] New high-level types: CallbackAnswer Remaining raw returns left as-is (complex types without high-level equivalents): - AccessSettings, SavedDialogs, WebBrowserSettings, PollStats - ParticipantJoinedChats, PeerLinkRequests
1 parent 51b92e5 commit ca3a7fc

9 files changed

Lines changed: 151 additions & 22 deletions

File tree

pyrogram/methods/communities/create_community.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pyrogram
44
from pyrogram import raw
5+
from pyrogram import types
56

67

78
class CreateCommunity:
@@ -11,7 +12,7 @@ async def create_community(
1112
title: str,
1213
about: Optional[str] = None,
1314
hidden: Optional[bool] = None
14-
) -> "raw.base.Updates":
15+
) -> Optional["types.Message"]:
1516
"""Create a community linked to a channel or supergroup.
1617
1718
.. include:: /_includes/usable-by/users.rst
@@ -30,7 +31,8 @@ async def create_community(
3031
If True, the community is created as hidden.
3132
3233
Returns:
33-
:obj:`~pyrogram.raw.base.Updates`: On success.
34+
:obj:`~pyrogram.types.Message` | ``None``: On success, the service message
35+
is returned, otherwise None.
3436
3537
Raises:
3638
~pyrogram.errors.ChatAdminRequired: The user is not an admin of
@@ -44,11 +46,23 @@ async def create_community(
4446

4547
peer = await self.resolve_peer(chat_id)
4648

47-
return await self.invoke(
49+
r = await self.invoke(
4850
raw.functions.communities.Create(
4951
title=title,
5052
peer=peer,
5153
about=about,
5254
hidden=hidden
5355
)
5456
)
57+
58+
users = {i.id: i for i in r.users}
59+
chats = {i.id: i for i in r.chats}
60+
61+
for i in r.updates:
62+
if isinstance(i, (raw.types.UpdateNewMessage,
63+
raw.types.UpdateNewChannelMessage)):
64+
return await types.Message._parse(
65+
self, i.message, users, chats
66+
)
67+
68+
return None
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
import pyrogram
22
from pyrogram import raw
3+
from pyrogram import types
34

45

56
class GetJoinedCommunities:
67
async def get_joined_communities(
78
self: "pyrogram.Client"
8-
) -> "raw.base.messages.Chats":
9+
) -> "types.List":
910
"""Get the list of communities the current user has joined.
1011
1112
.. include:: /_includes/usable-by/users.rst
1213
1314
Returns:
14-
:obj:`~pyrogram.raw.base.messages.Chats`: List of community chats.
15+
:obj:`~pyrogram.types.List` of :obj:`~pyrogram.types.Chat`: List of community chats.
1516
1617
Example:
1718
.. code-block:: python
1819
1920
communities = await app.get_joined_communities()
21+
for chat in communities:
22+
print(chat.title)
2023
"""
2124

22-
return await self.invoke(raw.functions.communities.GetJoinedCommunities())
25+
r = await self.invoke(raw.functions.communities.GetJoinedCommunities())
26+
27+
chats = types.List()
28+
29+
for chat in r.chats:
30+
chats.append(types.Chat._parse(self, chat))
31+
32+
return chats

pyrogram/methods/communities/toggle_community_collapsed.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async def toggle_community_collapsed(
99
self: "pyrogram.Client",
1010
community_id: Union[int, str],
1111
collapsed: Optional[bool] = None
12-
) -> "raw.base.Updates":
12+
) -> bool:
1313
"""Toggle whether a community is collapsed in the dialogs list.
1414
1515
.. include:: /_includes/usable-by/users.rst
@@ -22,7 +22,7 @@ async def toggle_community_collapsed(
2222
Pass True to collapse, False to expand.
2323
2424
Returns:
25-
:obj:`~pyrogram.raw.base.Updates`: On success.
25+
``bool``: True on success.
2626
2727
Raises:
2828
~pyrogram.errors.ChatAdminRequired: The user is not an admin of
@@ -36,9 +36,11 @@ async def toggle_community_collapsed(
3636

3737
community = await self.resolve_peer(community_id)
3838

39-
return await self.invoke(
39+
await self.invoke(
4040
raw.functions.communities.ToggleCommunityCollapsedInDialogs(
4141
community=community,
4242
collapsed=collapsed
4343
)
4444
)
45+
46+
return True

pyrogram/methods/ephemeral/get_ephemeral_callback_answer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pyrogram
44
from pyrogram import raw
5+
from pyrogram import types
56

67

78
class GetEphemeralCallbackAnswer:
@@ -10,7 +11,7 @@ async def get_ephemeral_callback_answer(
1011
chat_id: Union[int, str],
1112
message_id: int,
1213
data: Optional[bytes] = None
13-
) -> "raw.base.messages.BotCallbackAnswer":
14+
) -> "types.CallbackAnswer":
1415
"""Get a callback answer for an ephemeral message inline button.
1516
1617
.. include:: /_includes/usable-by/users.rst
@@ -26,20 +27,23 @@ async def get_ephemeral_callback_answer(
2627
Callback data.
2728
2829
Returns:
29-
:obj:`~pyrogram.raw.base.messages.BotCallbackAnswer`: The bot callback answer.
30+
:obj:`~pyrogram.types.CallbackAnswer`: The bot callback answer.
3031
3132
Example:
3233
.. code-block:: python
3334
3435
answer = await app.get_ephemeral_callback_answer(chat_id, 123, b"action")
36+
print(answer.message)
3537
"""
3638

3739
peer = await self.resolve_peer(chat_id)
3840

39-
return await self.invoke(
41+
r = await self.invoke(
4042
raw.functions.ephemeral.GetCallbackAnswer(
4143
peer=peer,
4244
id=message_id,
4345
data=data
4446
)
4547
)
48+
49+
return types.CallbackAnswer._parse(self, r)

pyrogram/methods/ephemeral/report_ephemeral_message.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async def report_ephemeral_message(
1111
message_id: int,
1212
option: bytes,
1313
message: str = ""
14-
) -> "raw.base.ReportResult":
14+
) -> bool:
1515
"""Report an ephemeral message.
1616
1717
.. include:: /_includes/usable-by/users.rst
@@ -30,7 +30,7 @@ async def report_ephemeral_message(
3030
Additional report comment. Defaults to empty string.
3131
3232
Returns:
33-
:obj:`~pyrogram.raw.base.ReportResult`: The report result.
33+
``bool``: True on success.
3434
3535
Example:
3636
.. code-block:: python
@@ -40,11 +40,13 @@ async def report_ephemeral_message(
4040

4141
peer = await self.resolve_peer(chat_id)
4242

43-
return await self.invoke(
43+
await self.invoke(
4444
raw.functions.ephemeral.ReportMessage(
4545
peer=peer,
4646
id=message_id,
4747
option=option,
4848
message=message
4949
)
5050
)
51+
52+
return True

pyrogram/methods/ephemeral/send_ephemeral_message.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pyrogram
44
from pyrogram import raw
5+
from pyrogram import types
56

67

78
class SendEphemeralMessage:
@@ -11,7 +12,7 @@ async def send_ephemeral_message(
1112
receiver_id: Union[int, str],
1213
text: str,
1314
query_id: Optional[int] = None
14-
) -> "raw.base.Updates":
15+
) -> Optional["types.Message"]:
1516
"""Send an ephemeral message to a bot within a peer context.
1617
1718
Ephemeral messages are visible only to the receiver bot and disappear
@@ -33,7 +34,8 @@ async def send_ephemeral_message(
3334
Callback query ID, if replying to a bot query.
3435
3536
Returns:
36-
:obj:`~pyrogram.raw.base.Updates`: On success.
37+
:obj:`~pyrogram.types.Message` | ``None``: On success, the sent message is returned,
38+
otherwise None.
3739
3840
Example:
3941
.. code-block:: python
@@ -49,7 +51,7 @@ async def send_ephemeral_message(
4951
access_hash=receiver_peer.access_hash
5052
)
5153

52-
return await self.invoke(
54+
r = await self.invoke(
5355
raw.functions.ephemeral.SendMessage(
5456
peer=peer,
5557
receiver_id=receiver_peer,
@@ -58,3 +60,15 @@ async def send_ephemeral_message(
5860
query_id=query_id
5961
)
6062
)
63+
64+
users = {i.id: i for i in r.users}
65+
chats = {i.id: i for i in r.chats}
66+
67+
for i in r.updates:
68+
if isinstance(i, (raw.types.UpdateNewMessage,
69+
raw.types.UpdateNewChannelMessage)):
70+
return await types.Message._parse(
71+
self, i.message, users, chats
72+
)
73+
74+
return None

pyrogram/methods/messages/translate_rich_message.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pyrogram
44
from pyrogram import raw
5+
from pyrogram import types
56

67

78
class TranslateRichMessage:
@@ -11,7 +12,7 @@ async def translate_rich_message(
1112
chat_id: Optional[Union[int, str]] = None,
1213
message_ids: Optional[List[int]] = None,
1314
tone: Optional[str] = None
14-
) -> "raw.base.messages.TranslatedRichMessage":
15+
) -> "types.List":
1516
"""Translate a rich message.
1617
1718
.. include:: /_includes/usable-by/users.rst
@@ -30,21 +31,30 @@ async def translate_rich_message(
3031
Optional tone/style for the translation.
3132
3233
Returns:
33-
:obj:`~pyrogram.raw.base.messages.TranslatedRichMessage`: Translated content.
34+
:obj:`~pyrogram.types.List` of :obj:`~pyrogram.types.RichMessage`: Translated messages.
3435
3536
Example:
3637
.. code-block:: python
3738
3839
result = await app.translate_rich_message("en", chat_id=chat_id, message_ids=[123])
40+
for msg in result:
41+
print(msg)
3942
"""
4043

4144
peer = await self.resolve_peer(chat_id) if chat_id is not None else None
4245

43-
return await self.invoke(
46+
r = await self.invoke(
4447
raw.functions.messages.TranslateRichMessage(
4548
to_lang=to_lang,
4649
peer=peer,
4750
id=message_ids,
4851
tone=tone
4952
)
5053
)
54+
55+
messages = types.List()
56+
57+
for rich_msg in r.result:
58+
messages.append(types.RichMessage._parse(self, rich_msg))
59+
60+
return messages

pyrogram/types/messages_and_media/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@
5151
from .star_gift import StarGift, StarGiftUnique
5252
from .welcome_messages import WelcomeMessages
5353
from .web_view_result import WebViewResult
54+
from .callback_answer import CallbackAnswer
5455

5556
__all__ = [
5657
"Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
5758
"StrippedThumbnail", "Poll", "PollLink", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice",
5859
"WebPage", "LinkPreviewOptions", "Dice", "Reaction", "WebAppData", "MessageReactions", "Story", "Giveaway", "AlternativeVideo",
5960
"StarGift", "StarGiftUnique",
6061
"InputRichMessage", "RichBlock", "RichBlockTableCell", "RichMessage", "RichText",
61-
"WelcomeMessages", "WebViewResult",
62+
"WelcomeMessages", "WebViewResult", "CallbackAnswer",
6263
]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 Optional
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
from ..object import Object
24+
25+
26+
class CallbackAnswer(Object):
27+
"""Bot callback answer.
28+
29+
Parameters:
30+
message (``str``, *optional*):
31+
Alert message.
32+
33+
alert (``bool``):
34+
True if an alert should be shown instead of a toast.
35+
36+
has_url (``bool``):
37+
True if the answer contains a URL.
38+
39+
url (``str``, *optional*):
40+
URL to open.
41+
42+
cache_time (``int``):
43+
Time in seconds the answer should be cached.
44+
"""
45+
46+
def __init__(
47+
self,
48+
*,
49+
client: "pyrogram.Client" = None,
50+
message: Optional[str] = None,
51+
alert: bool = False,
52+
has_url: bool = False,
53+
url: Optional[str] = None,
54+
cache_time: int = 0,
55+
):
56+
super().__init__(client)
57+
self.message = message
58+
self.alert = alert
59+
self.has_url = has_url
60+
self.url = url
61+
self.cache_time = cache_time
62+
63+
@staticmethod
64+
def _parse(client: "pyrogram.Client", raw_answer: "raw.base.messages.BotCallbackAnswer") -> "CallbackAnswer":
65+
return CallbackAnswer(
66+
client=client,
67+
message=getattr(raw_answer, "message", None),
68+
alert=getattr(raw_answer, "alert", False),
69+
has_url=getattr(raw_answer, "has_url", False),
70+
url=getattr(raw_answer, "url", None),
71+
cache_time=getattr(raw_answer, "cache_time", 0),
72+
)

0 commit comments

Comments
 (0)