Skip to content

Commit 30b3706

Browse files
committed
🔥 refactor(): Remove outdated Legacy function, and move Null from functions to types namespace.
1 parent 0d33b5f commit 30b3706

File tree

11 files changed

+377
-20
lines changed

11 files changed

+377
-20
lines changed

‎pyrogram/enums/message_entity_type.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,14 @@ class MessageEntityType(AutoName):
8383
DATE_TIME = raw.types.MessageEntityFormattedDate
8484
"Date time"
8585

86+
DIFF_INSERT = raw.types.MessageEntityDiffInsert
87+
"Inserted text in an AI diff"
88+
89+
DIFF_REPLACE = raw.types.MessageEntityDiffReplace
90+
"Replaced text in an AI diff"
91+
92+
DIFF_DELETE = raw.types.MessageEntityDiffDelete
93+
"Deleted text in an AI diff"
94+
8695
UNKNOWN = raw.types.MessageEntityUnknown
8796
"Unknown message entity type"

‎pyrogram/methods/messages/__init__.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
from .approve_suggested_post import ApproveSuggestedPost
2222
from .copy_media_group import CopyMediaGroup
2323
from .copy_message import CopyMessage
24+
from .compose_message_with_ai import ComposeMessageWithAI
2425
from .decline_suggested_post import DeclineSuggestedPost
2526
from .delete_chat_history import DeleteChatHistory
26-
from .delete_direct_messages_chat_topic_history import DeleteDirectMessagesChatTopicHistory
27+
from .delete_direct_messages_chat_topic_history import (
28+
DeleteDirectMessagesChatTopicHistory,
29+
)
2730
from .delete_messages import DeleteMessages
2831
from .download_media import DownloadMedia
2932
from .edit_inline_caption import EditInlineCaption
@@ -88,11 +91,14 @@
8891
from .send_video_note import SendVideoNote
8992
from .send_voice import SendVoice
9093
from .send_web_page import SendWebPage
91-
from .set_direct_messages_chat_topic_is_marked_as_unread import SetDirectMessagesChatTopicIsMarkedAsUnread
94+
from .set_direct_messages_chat_topic_is_marked_as_unread import (
95+
SetDirectMessagesChatTopicIsMarkedAsUnread,
96+
)
9297
from .start_bot import StartBot
9398
from .stop_poll import StopPoll
9499
from .stream_media import StreamMedia
95100
from .summarize_message import SummarizeMessage
101+
from .transcribe_audio import TranscribeAudio
96102
from .translate_message_text import TranslateMessageText
97103
from .translate_text import TranslateText
98104
from .view_messages import ViewMessages
@@ -103,6 +109,7 @@ class Messages(
103109
AddChecklistTasks,
104110
AddToGifs,
105111
ApproveSuggestedPost,
112+
ComposeMessageWithAI,
106113
DeclineSuggestedPost,
107114
DeleteMessages,
108115
EditMessageCaption,
@@ -177,9 +184,10 @@ class Messages(
177184
GetMainWebApp,
178185
StreamMedia,
179186
SummarizeMessage,
187+
TranscribeAudio,
180188
TranslateMessageText,
181189
TranslateText,
182190
GetCustomEmojiStickers,
183-
GetDirectMessagesChatTopicHistory
191+
GetDirectMessagesChatTopicHistory,
184192
):
185193
pass
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 enums, raw, types, utils
23+
24+
25+
class ComposeMessageWithAI:
26+
async def compose_message_with_ai(
27+
self: "pyrogram.Client",
28+
text: str,
29+
proofread: Optional[bool] = None,
30+
translate_to_language_code: Optional[str] = None,
31+
change_tone: Optional[str] = None,
32+
emojify: Optional[bool] = None,
33+
parse_mode: Optional["enums.ParseMode"] = None,
34+
entities: List["types.MessageEntity"] = None,
35+
) -> "types.ComposedMessageWithAI":
36+
"""Compose, rewrite or proofread a text with Telegram AI.
37+
38+
.. include:: /_includes/usable-by/users.rst
39+
40+
Parameters:
41+
text (``str``):
42+
Input text to process.
43+
44+
proofread (``bool``, *optional*):
45+
Whether to proofread the text.
46+
47+
translate_to_language_code (``str``, *optional*):
48+
Language code of the language to which the text is translated.
49+
50+
change_tone (``str``, *optional*):
51+
Desired output tone.
52+
53+
emojify (``bool``, *optional*):
54+
Whether to add emoji to the resulting text.
55+
56+
parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
57+
Parse mode of the text.
58+
59+
entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
60+
List of special entities that appear in the text.
61+
62+
Returns:
63+
:obj:`~pyrogram.types.ComposedMessageWithAI`: On success, the AI-composed message is returned.
64+
65+
Example:
66+
.. code-block:: python
67+
68+
await app.compose_message_with_ai(
69+
"hello!! can u help me rewrite this?",
70+
proofread=True,
71+
change_tone="formal"
72+
)
73+
"""
74+
message, entities = (
75+
await utils.parse_text_entities(self, text, parse_mode, entities)
76+
).values()
77+
78+
r = await self.invoke(
79+
raw.functions.messages.ComposeMessageWithAI(
80+
text=raw.types.TextWithEntities(text=message, entities=entities or []),
81+
proofread=proofread,
82+
emojify=emojify,
83+
translate_to_lang=translate_to_language_code,
84+
change_tone=change_tone,
85+
)
86+
)
87+
88+
return types.ComposedMessageWithAI._parse(self, r)

‎pyrogram/methods/messages/summarize_message.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async def summarize_message(
2828
chat_id: str,
2929
message_id: int,
3030
translate_to_language_code: Optional[str] = None,
31+
tone: Optional[str] = None,
3132
) -> "types.FormattedText":
3233
"""Summarizes content of the message with non-empty summary_language_code.
3334
@@ -50,14 +51,25 @@ async def summarize_message(
5051
"st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "ji", "yo", "zu"
5152
Defaults to the client's language code.
5253
54+
tone (``str``, *optional*):
55+
Desired output tone.
56+
5357
Returns:
5458
:obj:`~pyrogram.types.FormattedText`: On success, information about the summarized text is returned.
59+
60+
Example:
61+
.. code-block:: python
62+
63+
await app.summarize_message("pyrogram", 1)
64+
65+
await app.summarize_message("pyrogram", 1, tone="formal")
5566
"""
5667
r = await self.invoke(
5768
raw.functions.messages.SummarizeText(
5869
peer=await self.resolve_peer(chat_id),
5970
id=message_id,
60-
to_lang=translate_to_language_code or self.lang_code
71+
to_lang=translate_to_language_code or self.lang_code,
72+
tone=tone,
6173
)
6274
)
6375

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import pyrogram
20+
from pyrogram import raw, types
21+
22+
23+
class TranscribeAudio:
24+
async def transcribe_audio(
25+
self: "pyrogram.Client",
26+
chat_id: str,
27+
message_id: int,
28+
) -> "types.TranscribedAudio":
29+
"""Transcribe an audio message to text.
30+
31+
.. include:: /_includes/usable-by/users.rst
32+
33+
Parameters:
34+
chat_id (``int`` | ``str``):
35+
Unique identifier (int) or username (str) of the target chat.
36+
For your personal cloud (Saved Messages) you can simply use "me" or "self".
37+
For a contact that exists in your Telegram address book you can use his phone number (str).
38+
39+
message_id (``int``):
40+
Identifier of the audio message.
41+
42+
Returns:
43+
:obj:`~pyrogram.types.TranscribedAudio`: On success, the transcription result is returned.
44+
45+
Example:
46+
.. code-block:: python
47+
48+
await app.transcribe_audio("me", 1)
49+
"""
50+
r = await self.invoke(
51+
raw.functions.messages.TranscribeAudio(
52+
peer=await self.resolve_peer(chat_id), msg_id=message_id
53+
)
54+
)
55+
56+
return types.TranscribedAudio._parse(r)

‎pyrogram/methods/messages/translate_message_text.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async def translate_message_text(
2626
chat_id: str,
2727
message_id: int,
2828
to_language_code: str,
29+
tone: str = None,
2930
) -> "types.FormattedText":
3031
"""Extract text or caption of the given message and translates it to the given language.
3132
@@ -49,19 +50,25 @@ async def translate_message_text(
4950
"ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr",
5051
"st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "ji", "yo", "zu"
5152
53+
tone (``str``, *optional*):
54+
Desired output tone.
55+
5256
Returns:
5357
:obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned.
5458
5559
Example:
5660
.. code-block:: python
5761
5862
await app.translate_message_text("pyrogram", 1, "ru")
63+
64+
await app.translate_message_text("pyrogram", 1, "ru", tone="formal")
5965
"""
6066
r = await self.invoke(
6167
raw.functions.messages.TranslateText(
6268
to_lang=to_language_code,
69+
tone=tone,
6370
peer=await self.resolve_peer(chat_id),
64-
id=[message_id]
71+
id=[message_id],
6572
)
6673
)
6774

‎pyrogram/methods/messages/translate_text.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ async def translate_text(
2727
self: "pyrogram.Client",
2828
text: str,
2929
to_language_code: str,
30+
tone: Optional[str] = None,
3031
parse_mode: Optional["enums.ParseMode"] = None,
3132
entities: List["types.MessageEntity"] = None,
3233
) -> "types.FormattedText":
@@ -47,25 +48,30 @@ async def translate_text(
4748
"ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr",
4849
"st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "ji", "yo", "zu"
4950
51+
tone (``str``, *optional*):
52+
Desired output tone.
53+
5054
Returns:
5155
:obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned.
5256
5357
Example:
5458
.. code-block:: python
5559
5660
await app.translate_text("Hello!", "ru")
61+
62+
await app.translate_text("Hello!", "ru", tone="formal")
5763
"""
58-
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
64+
message, entities = (
65+
await utils.parse_text_entities(self, text, parse_mode, entities)
66+
).values()
5967

6068
r = await self.invoke(
6169
raw.functions.messages.TranslateText(
6270
to_lang=to_language_code,
71+
tone=tone,
6372
text=[
64-
raw.types.TextWithEntities(
65-
text=message,
66-
entities=entities or []
67-
)
68-
]
73+
raw.types.TextWithEntities(text=message, entities=entities or [])
74+
],
6975
)
7076
)
7177

0 commit comments

Comments
 (0)