Skip to content

Commit fe0aed0

Browse files
committed
✨ feat(messages): Add AI translation tones, message composition, and audio transcription.
This update also includes new AI-related message entity types for text diffs.
1 parent 1e7e39b commit fe0aed0

File tree

10 files changed

+327
-11
lines changed

10 files changed

+327
-11
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+
"AI diff inserted text"
88+
89+
DIFF_DELETE = raw.types.MessageEntityDiffDelete
90+
"AI diff deleted text"
91+
92+
DIFF_REPLACE = raw.types.MessageEntityDiffReplace
93+
"AI diff replaced text"
94+
8695
UNKNOWN = raw.types.MessageEntityUnknown
8796
"Unknown message entity type"

pyrogram/methods/messages/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .add_checklist_tasks import AddChecklistTasks
2020
from .add_to_gifs import AddToGifs
2121
from .approve_suggested_post import ApproveSuggestedPost
22+
from .compose_message_with_ai import ComposeMessageWithAI
2223
from .copy_media_group import CopyMediaGroup
2324
from .copy_message import CopyMessage
2425
from .decline_suggested_post import DeclineSuggestedPost
@@ -93,6 +94,7 @@
9394
from .stop_poll import StopPoll
9495
from .stream_media import StreamMedia
9596
from .summarize_message import SummarizeMessage
97+
from .transcribe_audio import TranscribeAudio
9698
from .translate_message_text import TranslateMessageText
9799
from .translate_text import TranslateText
98100
from .view_messages import ViewMessages
@@ -103,6 +105,7 @@ class Messages(
103105
AddChecklistTasks,
104106
AddToGifs,
105107
ApproveSuggestedPost,
108+
ComposeMessageWithAI,
106109
DeclineSuggestedPost,
107110
DeleteMessages,
108111
EditMessageCaption,
@@ -177,6 +180,7 @@ class Messages(
177180
GetMainWebApp,
178181
StreamMedia,
179182
SummarizeMessage,
183+
TranscribeAudio,
180184
TranslateMessageText,
181185
TranslateText,
182186
GetCustomEmojiStickers,
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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, 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+
emojify: Optional[bool] = None,
31+
translate_to_lang: Optional[str] = None,
32+
change_tone: Optional[str] = None,
33+
) -> "types.ComposedMessageWithAI":
34+
"""Use Telegram's AI to compose, proofread, translate, change tone, or emojify a message.
35+
36+
.. include:: /_includes/usable-by/users.rst
37+
38+
Parameters:
39+
text (``str``):
40+
The input text to compose or modify with AI.
41+
42+
proofread (``bool``, *optional*):
43+
Pass True to proofread the text.
44+
45+
emojify (``bool``, *optional*):
46+
Pass True to add emojis to the text.
47+
48+
translate_to_lang (``str``, *optional*):
49+
Language code to translate the text to.
50+
51+
change_tone (``str``, *optional*):
52+
Tone to change the text to.
53+
Must be one of "formal", "short", "tribal", "corp", "biblical", "viking", "zen".
54+
55+
Returns:
56+
:obj:`~pyrogram.types.ComposedMessageWithAI`: On success, the AI-composed message is returned.
57+
58+
Example:
59+
.. code-block:: python
60+
61+
# Proofread a message
62+
result = await app.compose_message_with_ai("Helo wrold!", proofread=True)
63+
print(result.result_text.text)
64+
65+
# Translate a message
66+
result = await app.compose_message_with_ai("Hello!", translate_to_lang="ru")
67+
print(result.result_text.text)
68+
69+
# Change tone
70+
result = await app.compose_message_with_ai("Fix this now.", change_tone="formal")
71+
print(result.result_text.text)
72+
"""
73+
if not any([proofread, emojify, translate_to_lang, change_tone]):
74+
raise ValueError(
75+
"At least one task must be specified: "
76+
"proofread, emojify, translate_to_lang, or change_tone"
77+
)
78+
79+
message, entities = (await utils.parse_text_entities(self, text, None, None)).values()
80+
81+
r = await self.invoke(
82+
raw.functions.messages.ComposeMessageWithAI(
83+
text=raw.types.TextWithEntities(
84+
text=message,
85+
entities=entities or []
86+
),
87+
proofread=proofread,
88+
emojify=emojify,
89+
translate_to_lang=translate_to_lang,
90+
change_tone=change_tone,
91+
)
92+
)
93+
94+
return types.ComposedMessageWithAI._parse(self, r)

pyrogram/methods/messages/summarize_message.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import Optional
19+
from typing import Optional, Union
2020

2121
import pyrogram
2222
from pyrogram import raw, types
@@ -25,9 +25,10 @@
2525
class SummarizeMessage:
2626
async def summarize_message(
2727
self: "pyrogram.Client",
28-
chat_id: str,
28+
chat_id: Union[int, 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,19 @@ 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+
Tone for the summary.
56+
Must be one of "formal", "short", "tribal", "corp", "biblical", "viking", "zen".
57+
5358
Returns:
5459
:obj:`~pyrogram.types.FormattedText`: On success, information about the summarized text is returned.
5560
"""
5661
r = await self.invoke(
5762
raw.functions.messages.SummarizeText(
5863
peer=await self.resolve_peer(chat_id),
5964
id=message_id,
60-
to_lang=translate_to_language_code or self.lang_code
65+
to_lang=translate_to_language_code or self.lang_code,
66+
tone=tone,
6167
)
6268
)
6369

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 raw, types
23+
24+
25+
class TranscribeAudio:
26+
async def transcribe_audio(
27+
self: "pyrogram.Client",
28+
chat_id: Union[int, str],
29+
message_id: int,
30+
) -> "types.TranscribedAudio":
31+
"""Transcribe audio from a voice or video note message.
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.
38+
For your personal cloud (Saved Messages) you can simply use "me" or "self".
39+
For a contact that exists in your Telegram address book you can use his phone number (str).
40+
41+
message_id (``int``):
42+
Identifier of the message containing the audio to transcribe.
43+
44+
Returns:
45+
:obj:`~pyrogram.types.TranscribedAudio`: On success, the transcribed audio is returned.
46+
47+
Example:
48+
.. code-block:: python
49+
50+
result = await app.transcribe_audio(chat_id, message_id)
51+
print(result.text)
52+
"""
53+
r = await self.invoke(
54+
raw.functions.messages.TranscribeAudio(
55+
peer=await self.resolve_peer(chat_id),
56+
msg_id=message_id
57+
)
58+
)
59+
60+
return types.TranscribedAudio._parse(self, r)

pyrogram/methods/messages/translate_message_text.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Optional, Union
20+
1921
import pyrogram
2022
from pyrogram import raw, types
2123

2224

2325
class TranslateMessageText:
2426
async def translate_message_text(
2527
self: "pyrogram.Client",
26-
chat_id: str,
28+
chat_id: Union[int, str],
2729
message_id: int,
2830
to_language_code: str,
31+
tone: Optional[str] = None,
2932
) -> "types.FormattedText":
3033
"""Extract text or caption of the given message and translates it to the given language.
3134
@@ -49,6 +52,10 @@ async def translate_message_text(
4952
"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",
5053
"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"
5154
55+
tone (``str``, *optional*):
56+
Tone for the translation.
57+
Must be one of "formal", "short", "tribal", "corp", "biblical", "viking", "zen".
58+
5259
Returns:
5360
:obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned.
5461
@@ -61,7 +68,8 @@ async def translate_message_text(
6168
raw.functions.messages.TranslateText(
6269
to_lang=to_language_code,
6370
peer=await self.resolve_peer(chat_id),
64-
id=[message_id]
71+
id=[message_id],
72+
tone=tone,
6573
)
6674
)
6775

pyrogram/methods/messages/translate_text.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import List, Optional
19+
from typing import Optional
2020

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

2424

2525
class TranslateText:
2626
async def translate_text(
2727
self: "pyrogram.Client",
2828
text: str,
2929
to_language_code: str,
30-
parse_mode: Optional["enums.ParseMode"] = None,
31-
entities: List["types.MessageEntity"] = None,
30+
tone: Optional[str] = None,
3231
) -> "types.FormattedText":
3332
"""Translate a text to the given language.
3433
@@ -47,6 +46,10 @@ async def translate_text(
4746
"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",
4847
"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"
4948
49+
tone (``str``, *optional*):
50+
Tone for the translation.
51+
Must be one of "formal", "short", "tribal", "corp", "biblical", "viking", "zen".
52+
5053
Returns:
5154
:obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned.
5255
@@ -55,7 +58,7 @@ async def translate_text(
5558
5659
await app.translate_text("Hello!", "ru")
5760
"""
58-
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
61+
message, entities = (await utils.parse_text_entities(self, text, None, None)).values()
5962

6063
r = await self.invoke(
6164
raw.functions.messages.TranslateText(
@@ -65,7 +68,8 @@ async def translate_text(
6568
text=message,
6669
entities=entities or []
6770
)
68-
]
71+
],
72+
tone=tone,
6973
)
7074
)
7175

pyrogram/types/messages_and_media/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from .checklist_tasks_added import ChecklistTasksAdded
3939
from .checklist_tasks_done import ChecklistTasksDone
4040
from .checklist import Checklist
41+
from .composed_message_with_ai import ComposedMessageWithAI
4142
from .contact_registered import ContactRegistered
4243
from .contact import Contact
4344
from .craft_gift_result import CraftGiftResult, CraftGiftResultSuccess, CraftGiftResultFail
@@ -133,6 +134,7 @@
133134
from .suggested_post_price import SuggestedPostPrice, SuggestedPostPriceStar, SuggestedPostPriceTon
134135
from .text_quote import TextQuote
135136
from .thumbnail import Thumbnail
137+
from .transcribed_audio import TranscribedAudio
136138
from .validated_order_info import ValidatedOrderInfo
137139
from .upgraded_gift_attribute_id_backdrop import UpgradedGiftAttributeIdBackdrop
138140
from .upgraded_gift_attribute_id_model import UpgradedGiftAttributeIdModel
@@ -175,6 +177,7 @@
175177
"ChecklistTasksAdded",
176178
"ChecklistTasksDone",
177179
"Checklist",
180+
"ComposedMessageWithAI",
178181
"ContactRegistered",
179182
"Contact",
180183
"CraftGiftResult",
@@ -276,6 +279,7 @@
276279
"SuggestedPostPriceTon",
277280
"TextQuote",
278281
"Thumbnail",
282+
"TranscribedAudio",
279283
"ValidatedOrderInfo",
280284
"UpgradedGiftAttributeIdBackdrop",
281285
"UpgradedGiftAttributeIdModel",

0 commit comments

Comments
 (0)