Skip to content

Commit 51b92e5

Browse files
author
Pyrogram-Mod - Dev
committed
Remove raw return types from all new methods (main..dev)
- forward_welcome_message: raw.base.Updates -> types.Message - get_welcome_messages: raw.base.WelcomeMessages -> types.WelcomeMessages - compose_rich_message_with_ai: raw.base.messages.ComposedRichMessageWithAI -> types.RichMessage - request_chat_join_web_view: raw.base.WebViewResult -> types.WebViewResult Add new high-level types: - WelcomeMessages (hash, messages list) - WebViewResult (url, title, description, users)
1 parent 69beec1 commit 51b92e5

7 files changed

Lines changed: 171 additions & 14 deletions

File tree

pyrogram/methods/ephemeral/forward_welcome_message.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from typing import Union
1+
from typing import Union, Optional
22

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

67

78
class ForwardWelcomeMessage:
89
async def forward_welcome_message(
910
self: "pyrogram.Client",
1011
chat_id: Union[int, str],
1112
message_id: int,
12-
) -> "raw.base.Updates":
13+
) -> Optional["types.Message"]:
1314
"""Forward a welcome message into its chat, sending it as a regular message.
1415
1516
.. include:: /_includes/usable-by/users.rst
@@ -22,17 +23,30 @@ async def forward_welcome_message(
2223
Identifier of the welcome message to forward.
2324
2425
Returns:
25-
:obj:`~pyrogram.raw.base.Updates`: On success.
26+
:obj:`~pyrogram.types.Message` | ``None``: On success, the forwarded message is returned,
27+
otherwise None.
2628
2729
Example:
2830
.. code-block:: python
2931
3032
await app.forward_welcome_message(chat_id, 123)
3133
"""
32-
return await self.invoke(
34+
r = await self.invoke(
3335
raw.functions.messages.ForwardMessage(
3436
peer=await self.resolve_peer(chat_id),
3537
id=message_id,
3638
random_id=self.rnd_id()
3739
)
3840
)
41+
42+
users = {i.id: i for i in r.users}
43+
chats = {i.id: i for i in r.chats}
44+
45+
for i in r.updates:
46+
if isinstance(i, (raw.types.UpdateNewMessage,
47+
raw.types.UpdateNewChannelMessage)):
48+
return await types.Message._parse(
49+
self, i.message, users, chats
50+
)
51+
52+
return None

pyrogram/methods/ephemeral/get_welcome_messages.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

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

67

78
class GetWelcomeMessages:
89
async def get_welcome_messages(
910
self: "pyrogram.Client",
1011
chat_id: Union[int, str],
11-
) -> "raw.base.WelcomeMessages":
12+
) -> "types.WelcomeMessages":
1213
"""Get the welcome messages configured for a chat.
1314
1415
Welcome messages are ephemeral messages a bot sends automatically the first time a user
@@ -21,16 +22,20 @@ async def get_welcome_messages(
2122
The peer context to get welcome messages for.
2223
2324
Returns:
24-
:obj:`~pyrogram.raw.base.WelcomeMessages`: The raw welcome messages list.
25+
:obj:`~pyrogram.types.WelcomeMessages`: The welcome messages list.
2526
2627
Example:
2728
.. code-block:: python
2829
29-
messages = await app.get_welcome_messages(chat_id)
30+
welcome = await app.get_welcome_messages(chat_id)
31+
for msg in welcome.messages:
32+
print(msg.message)
3033
"""
31-
return await self.invoke(
34+
r = await self.invoke(
3235
raw.functions.ephemeral.GetWelcomeMessages(
3336
peer=await self.resolve_peer(chat_id),
3437
hash=0
3538
)
3639
)
40+
41+
return types.WelcomeMessages._parse(self, r)

pyrogram/methods/messages/compose_rich_message_with_ai.py

Lines changed: 8 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 ComposeRichMessageWithAI:
@@ -12,7 +13,7 @@ async def compose_rich_message_with_ai(
1213
text: Optional["raw.base.InputRichMessage"] = None,
1314
translate_to_lang: Optional[str] = None,
1415
tone: Optional["raw.base.InputAiComposeTone"] = None
15-
) -> "raw.base.messages.ComposedRichMessageWithAI":
16+
) -> "types.RichMessage":
1617
"""Compose or improve a rich message using Telegram's AI.
1718
1819
.. include:: /_includes/usable-by/users.rst
@@ -34,7 +35,7 @@ async def compose_rich_message_with_ai(
3435
The composition tone/style to apply.
3536
3637
Returns:
37-
:obj:`~pyrogram.raw.base.messages.ComposedRichMessageWithAI`: The AI-composed message.
38+
:obj:`~pyrogram.types.RichMessage`: The AI-composed rich message.
3839
3940
Example:
4041
.. code-block:: python
@@ -43,9 +44,11 @@ async def compose_rich_message_with_ai(
4344
proofread=True,
4445
text=raw.types.InputRichMessage(blocks=[...])
4546
)
47+
for block in result.blocks:
48+
print(block)
4649
"""
4750

48-
return await self.invoke(
51+
r = await self.invoke(
4952
raw.functions.messages.ComposeRichMessageWithAI(
5053
proofread=proofread or None,
5154
emojify=emojify or None,
@@ -54,3 +57,5 @@ async def compose_rich_message_with_ai(
5457
tone=tone
5558
)
5659
)
60+
61+
return types.RichMessage._parse(self, r.result)

pyrogram/methods/messages/request_chat_join_web_view.py

Lines changed: 6 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 RequestChatJoinWebView:
@@ -10,7 +11,7 @@ async def request_chat_join_web_view(
1011
query_id: int,
1112
platform: str,
1213
theme_params: Optional[dict] = None
13-
) -> "raw.base.WebViewResult":
14+
) -> "types.WebViewResult":
1415
"""Request a web view URL for a chat join button.
1516
1617
Used when a bot sends a web view button as part of a
@@ -29,7 +30,7 @@ async def request_chat_join_web_view(
2930
Theme parameters as a JSON-serializable dict.
3031
3132
Returns:
32-
:obj:`~pyrogram.raw.base.WebViewResult`: The web view result.
33+
:obj:`~pyrogram.types.WebViewResult`: The web view result with the URL.
3334
3435
Example:
3536
.. code-block:: python
@@ -47,10 +48,12 @@ async def request_chat_join_web_view(
4748
if theme_params is not None:
4849
theme = raw.types.DataJSON(data=json.dumps(theme_params))
4950

50-
return await self.invoke(
51+
r = await self.invoke(
5152
raw.functions.messages.RequestChatJoinWebView(
5253
query_id=query_id,
5354
platform=platform,
5455
theme_params=theme
5556
)
5657
)
58+
59+
return types.WebViewResult._parse(self, r)

pyrogram/types/messages_and_media/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@
4949
from .link_preview_options import LinkPreviewOptions
5050
from .message_reactions import MessageReactions
5151
from .star_gift import StarGift, StarGiftUnique
52+
from .welcome_messages import WelcomeMessages
53+
from .web_view_result import WebViewResult
5254

5355
__all__ = [
5456
"Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
5557
"StrippedThumbnail", "Poll", "PollLink", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice",
5658
"WebPage", "LinkPreviewOptions", "Dice", "Reaction", "WebAppData", "MessageReactions", "Story", "Giveaway", "AlternativeVideo",
5759
"StarGift", "StarGiftUnique",
5860
"InputRichMessage", "RichBlock", "RichBlockTableCell", "RichMessage", "RichText",
61+
"WelcomeMessages", "WebViewResult",
5962
]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 List, Optional
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
from pyrogram import types
24+
from ..object import Object
25+
26+
27+
class WebViewResult(Object):
28+
"""Result of a web view interaction.
29+
30+
Parameters:
31+
url (``str``, *optional*):
32+
The web view URL.
33+
34+
title (``str``, *optional*):
35+
Title of the result.
36+
37+
description (``str``, *optional*):
38+
Description of the result.
39+
40+
users (List of :obj:`~pyrogram.types.User`):
41+
List of users associated with the result.
42+
"""
43+
44+
def __init__(
45+
self,
46+
*,
47+
client: "pyrogram.Client" = None,
48+
url: Optional[str] = None,
49+
title: Optional[str] = None,
50+
description: Optional[str] = None,
51+
users: Optional[List["types.User"]] = None,
52+
):
53+
super().__init__(client)
54+
self.url = url
55+
self.title = title
56+
self.description = description
57+
self.users = users or []
58+
59+
@staticmethod
60+
def _parse(client: "pyrogram.Client", raw_result: "raw.base.messages.WebViewResult") -> "WebViewResult":
61+
result = raw_result.result
62+
users = [types.User._parse(client, u) for u in raw_result.users]
63+
64+
return WebViewResult(
65+
client=client,
66+
url=getattr(result, "url", None),
67+
title=getattr(result, "title", None),
68+
description=getattr(result, "description", None),
69+
users=users,
70+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 List
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
from ..object import Object
24+
25+
26+
class WelcomeMessages(Object):
27+
"""Welcome messages configured for a chat.
28+
29+
Parameters:
30+
hash (``int``):
31+
Hash for caching.
32+
33+
messages (List of :obj:`~pyrogram.raw.base.EphemeralMessage`):
34+
List of ephemeral welcome messages.
35+
"""
36+
37+
def __init__(
38+
self,
39+
*,
40+
client: "pyrogram.Client" = None,
41+
hash: int,
42+
messages: List["raw.base.EphemeralMessage"],
43+
):
44+
super().__init__(client)
45+
self.hash = hash
46+
self.messages = messages
47+
48+
@staticmethod
49+
def _parse(client: "pyrogram.Client", raw_welcome: "raw.base.WelcomeMessages") -> "WelcomeMessages":
50+
if isinstance(raw_welcome, raw.types.WelcomeMessagesNotModified):
51+
return WelcomeMessages(client=client, hash=0, messages=[])
52+
53+
return WelcomeMessages(
54+
client=client,
55+
hash=raw_welcome.hash,
56+
messages=raw_welcome.messages,
57+
)

0 commit comments

Comments
 (0)