From baa27a037339fbc0e31cc1d6c582336c109616bf Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sun, 26 Jan 2025 20:23:41 +0700 Subject: [PATCH] pyrofork: Add transcribe_audio method Signed-off-by: wulan17 --- compiler/docs/compiler.py | 3 + pyrogram/methods/messages/__init__.py | 2 + pyrogram/methods/messages/transcribe_audio.py | 52 +++++++++++++++ pyrogram/types/messages_and_media/__init__.py | 2 + pyrogram/types/messages_and_media/message.py | 28 ++++++++ .../messages_and_media/transcribe_audio.py | 66 +++++++++++++++++++ 6 files changed, 153 insertions(+) create mode 100644 pyrogram/methods/messages/transcribe_audio.py create mode 100644 pyrogram/types/messages_and_media/transcribe_audio.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 6cde68108..ede69577a 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -220,6 +220,7 @@ def get_title_list(s: str) -> list: get_discussion_replies get_discussion_replies_count get_custom_emoji_stickers + transcribe_audio translate_message_text start_bot """, @@ -534,6 +535,7 @@ def get_title_list(s: str) -> list: WebPage WebPageEmpty WebPagePreview + TranscribedAudio TranslatedText Poll PollOption @@ -780,6 +782,7 @@ def get_title_list(s: str) -> list: Message.reply_web_page Message.get_media_group Message.react + Message.transcribe Message.translate Message.wait_for_click """, diff --git a/pyrogram/methods/messages/__init__.py b/pyrogram/methods/messages/__init__.py index ab66ffac6..1dde4137e 100644 --- a/pyrogram/methods/messages/__init__.py +++ b/pyrogram/methods/messages/__init__.py @@ -74,6 +74,7 @@ from .stop_poll import StopPoll from .stream_media import StreamMedia from .vote_poll import VotePoll +from .transcribe_audio import TranscribeAudio from .translate_text import TranslateText class Messages( @@ -133,6 +134,7 @@ class Messages( GetDiscussionRepliesCount, StreamMedia, GetCustomEmojiStickers, + TranscribeAudio, TranslateText, StartBot ): diff --git a/pyrogram/methods/messages/transcribe_audio.py b/pyrogram/methods/messages/transcribe_audio.py new file mode 100644 index 000000000..2175e2797 --- /dev/null +++ b/pyrogram/methods/messages/transcribe_audio.py @@ -0,0 +1,52 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# Copyright (C) 2022-present Mayuri-Chan +# +# This file is part of Pyrofork. +# +# Pyrofork is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrofork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrofork. If not, see . + +from typing import Union + +import pyrogram +from pyrogram import raw, types + + +class TranscribeAudio: + async def transcribe_audio( + self: "pyrogram.Client", + chat_id: Union[int, str], + message_id: int + ) -> "types.TranscribeAudio": + """Transcribes the audio of a voice message. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + message_id (``int``): + Identifier of the message containing the voice message. + + Returns: + :obj:`~pyrogram.types.TranscribeAudio`: On success. + """ + chat = await self.resolve_peer(chat_id) + r = await self.invoke( + raw.functions.messages.TranscribeAudio( + peer=chat, + msg_id=message_id + ) + ) + + return types.TranscribeAudio._parse(r) diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index 1acf6d5a6..486f4664d 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -69,6 +69,7 @@ from .exported_story_link import ExportedStoryLink from .wallpaper import Wallpaper from .wallpaper_settings import WallpaperSettings +from .transcribed_audio import TranscribedAudio from .translated_text import TranslatedText __all__ = [ @@ -124,5 +125,6 @@ "ExportedStoryLink", "Wallpaper", "WallpaperSettings", + "TranscribedAudio", "TranslatedText" ] diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 5bee66457..f2de74ed8 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -5434,6 +5434,34 @@ async def ask( reply_message.request = request return reply_message + async def transcribe(self) -> "types.TranscribeAudio": + """Bound method *transcribe* of :obj:`~pyrogram.types.Message`. + + Use as a shortcut for: + + .. code-block:: python + + await client.transcribe_audio( + chat_id=message.chat.id, + message_id=message.id + ) + + Example: + .. code-block:: python + + await message.transcribe() + + Returns: + :obj:`~pyrogram.types.TranscribeAudio`: On success. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + return await self._client.transcribe_audio( + chat_id=self.chat.id, + message_id=self.id + ) + async def translate( self, to_language_code: str diff --git a/pyrogram/types/messages_and_media/transcribe_audio.py b/pyrogram/types/messages_and_media/transcribe_audio.py new file mode 100644 index 000000000..9fbe8fb1d --- /dev/null +++ b/pyrogram/types/messages_and_media/transcribe_audio.py @@ -0,0 +1,66 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# Copyright (C) 2022-present Mayuri-Chan +# +# This file is part of Pyrofork. +# +# Pyrofork is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrofork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrofork. If not, see . + +from pyrogram import raw + + +class TranscribeAudio: + """Transcribes the audio of a voice message. + + Parameters: + transcription_id (``int``): + Unique identifier of the transcription. + + text (``str``): + Transcribed text. + + pending (``bool``, *optional*): + Whether the transcription is pending. + + trial_remains_num (``int``, *optional*): + Number of trials remaining. + + trial_remains_until_date (``int``, *optional*): + Date the trial remains until. + """ + + def __init__( + self, + *, + transcription_id: int, + text: str, + pending: bool = None, + trial_remains_num: int = None, + trial_remains_until_date: int = None + ): + self.transcription_id = transcription_id + self.text = text + self.pending = pending + self.trial_remains_num = trial_remains_num + self.trial_remains_until_date = trial_remains_until_date + + @staticmethod + def _parse(transcribe_result: "raw.types.messages.TranscribedAudio") -> "TranscribeAudio": + return TranscribeAudio( + transcription_id=transcribe_result.id, + text=transcribe_result.text, + pending=transcribe_result.pending, + trial_remains_num=transcribe_result.trial_remains_num, + trial_remains_until_date=transcribe_result.trial_remains_until_date + )