Skip to content

Commit

Permalink
pyrofork: Add transcribe_audio method
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Jan 26, 2025
1 parent bd340cd commit baa27a0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
""",
Expand Down Expand Up @@ -534,6 +535,7 @@ def get_title_list(s: str) -> list:
WebPage
WebPageEmpty
WebPagePreview
TranscribedAudio
TranslatedText
Poll
PollOption
Expand Down Expand Up @@ -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
""",
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -133,6 +134,7 @@ class Messages(
GetDiscussionRepliesCount,
StreamMedia,
GetCustomEmojiStickers,
TranscribeAudio,
TranslateText,
StartBot
):
Expand Down
52 changes: 52 additions & 0 deletions pyrogram/methods/messages/transcribe_audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/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 <http://www.gnu.org/licenses/>.

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)
2 changes: 2 additions & 0 deletions pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down Expand Up @@ -124,5 +125,6 @@
"ExportedStoryLink",
"Wallpaper",
"WallpaperSettings",
"TranscribedAudio",
"TranslatedText"
]
28 changes: 28 additions & 0 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions pyrogram/types/messages_and_media/transcribe_audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/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 <http://www.gnu.org/licenses/>.

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
)

0 comments on commit baa27a0

Please sign in to comment.