Skip to content

Commit ced2e9e

Browse files
Add translate_text and translate_message_text method
1 parent ed360c2 commit ced2e9e

6 files changed

Lines changed: 210 additions & 0 deletions

File tree

compiler/docs/compiler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ def get_title_list(s: str) -> list:
276276
search_global_count
277277
download_media
278278
stream_media
279+
translate_message_text
280+
translate_text
279281
get_discussion_message
280282
get_discussion_replies
281283
get_discussion_replies_count
@@ -643,6 +645,7 @@ def get_title_list(s: str) -> list:
643645
Document
644646
ExternalReplyInfo
645647
FactCheck
648+
FormattedText
646649
ForumTopic
647650
ForumTopicClosed
648651
ForumTopicCreated

pyrogram/methods/messages/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
from .start_bot import StartBot
8080
from .stop_poll import StopPoll
8181
from .stream_media import StreamMedia
82+
from .translate_message_text import TranslateMessageText
83+
from .translate_text import TranslateText
8284
from .view_messages import ViewMessages
8385
from .vote_poll import VotePoll
8486

@@ -147,6 +149,8 @@ class Messages(
147149
GetDiscussionReplies,
148150
GetDiscussionRepliesCount,
149151
StreamMedia,
152+
TranslateMessageText,
153+
TranslateText,
150154
GetCustomEmojiStickers,
151155
GetDirectMessagesChatTopicHistory
152156
):
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 TranslateMessageText:
24+
async def translate_message_text(
25+
self: "pyrogram.Client",
26+
chat_id: str,
27+
message_id: int,
28+
to_language_code: str,
29+
) -> "types.FormattedText":
30+
"""Extract text or caption of the given message and translates it to the given language.
31+
32+
If the current user is a Telegram Premium user, then text formatting is preserved.
33+
34+
.. include:: /_includes/usable-by/users.rst
35+
36+
Parameters:
37+
chat_id (``int`` | ``str``):
38+
Unique identifier (int) or username (str) of the target chat.
39+
For your personal cloud (Saved Messages) you can simply use "me" or "self".
40+
For a contact that exists in your Telegram address book you can use his phone number (str).
41+
42+
message_id (``int``):
43+
Identifier of the message.
44+
45+
to_language_code (``str``):
46+
Language code of the language to which the message is translated.
47+
Must be one of "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh-CN", "zh", "zh-Hans", "zh-TW", "zh-Hant", "co", "hr", "cs", "da", "nl", "en", "eo", "et",
48+
"fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "iw", "hi", "hmn", "hu", "is", "ig", "id", "in", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko",
49+
"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",
50+
"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"
51+
52+
Returns:
53+
:obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned.
54+
55+
Example:
56+
.. code-block:: python
57+
58+
await app.translate_message_text("pyrogram", 1, "ru")
59+
"""
60+
r = await self.invoke(
61+
raw.functions.messages.TranslateText(
62+
to_lang=to_language_code,
63+
peer=await self.resolve_peer(chat_id),
64+
id=[message_id]
65+
)
66+
)
67+
68+
return types.FormattedText._parse(self, r.result[0])
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 List, Optional
20+
21+
import pyrogram
22+
from pyrogram import enums, raw, types, utils
23+
24+
25+
class TranslateText:
26+
async def translate_text(
27+
self: "pyrogram.Client",
28+
text: str,
29+
to_language_code: str,
30+
parse_mode: Optional["enums.ParseMode"] = None,
31+
entities: List["types.MessageEntity"] = None,
32+
) -> "types.FormattedText":
33+
"""Translate a text to the given language.
34+
35+
If the current user is a Telegram Premium user, then text formatting is preserved.
36+
37+
.. include:: /_includes/usable-by/users.rst
38+
39+
Parameters:
40+
text (``str``):
41+
Text to translate.
42+
43+
to_language_code (``str``):
44+
Language code of the language to which the message is translated.
45+
Must be one of "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh-CN", "zh", "zh-Hans", "zh-TW", "zh-Hant", "co", "hr", "cs", "da", "nl", "en", "eo", "et",
46+
"fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "iw", "hi", "hmn", "hu", "is", "ig", "id", "in", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko",
47+
"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",
48+
"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"
49+
50+
Returns:
51+
:obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned.
52+
53+
Example:
54+
.. code-block:: python
55+
56+
await app.translate_text("Hello!", "ru")
57+
"""
58+
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
59+
60+
r = await self.invoke(
61+
raw.functions.messages.TranslateText(
62+
to_lang=to_language_code,
63+
text=[
64+
raw.types.TextWithEntities(
65+
text=message,
66+
entities=entities or []
67+
)
68+
]
69+
)
70+
)
71+
72+
return types.FormattedText._parse(self, r.result[0])

pyrogram/types/messages_and_media/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from .document import Document
3434
from .external_reply_info import ExternalReplyInfo
3535
from .fact_check import FactCheck
36+
from .formatted_text import FormattedText
3637
from .forum_topic import ForumTopic
3738
from .forum_topic_closed import ForumTopicClosed
3839
from .forum_topic_created import ForumTopicCreated
@@ -117,6 +118,7 @@
117118
"Document",
118119
"ExternalReplyInfo",
119120
"FactCheck",
121+
"FormattedText",
120122
"ForumTopic",
121123
"ForumTopicClosed",
122124
"ForumTopicCreated",
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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, types
23+
24+
from ..object import Object
25+
26+
27+
class FormattedText(Object):
28+
"""Contains information about a text with some entities.
29+
30+
Parameters:
31+
text (``str``):
32+
The text.
33+
34+
entities (List of :obj:`~pyrogram.types.MessageEntity`):
35+
Entities contained in the text. Entities can be nested, but must not mutually intersect with each other.
36+
"""
37+
38+
def __init__(
39+
self,
40+
*,
41+
text: str,
42+
entities: Optional[List["types.MessageEntity"]] = None,
43+
):
44+
super().__init__()
45+
46+
self.text = text
47+
self.entities = entities
48+
49+
@staticmethod
50+
def _parse(client: "pyrogram.Client", text: "raw.types.TextWithEntities") -> "FormattedText":
51+
entities = types.List(
52+
filter(
53+
lambda x: x is not None,
54+
[types.MessageEntity._parse(client, entity, {}) for entity in text.entities]
55+
)
56+
)
57+
58+
return FormattedText(
59+
text=text.text,
60+
entities=entities or None,
61+
)

0 commit comments

Comments
 (0)