Skip to content

Commit

Permalink
Fix send_audio not working as expected in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Feb 8, 2024
1 parent 91f3ffa commit b33cb63
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 4 additions & 2 deletions pyrogram/methods/messages/send_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ async def progress(current, total):
try:
if isinstance(audio, str):
if os.path.isfile(audio):
mime_type = utils.voiceAudioUrlFuxUps(self, audio, 1)
thumb = await self.save_file(thumb)
file = await self.save_file(audio, progress=progress, progress_args=progress_args)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(audio) or "audio/mpeg",
mime_type=mime_type,
file=file,
thumb=thumb,
attributes=[
Expand All @@ -219,10 +220,11 @@ async def progress(current, total):
else:
media = utils.get_input_media_from_file_id(audio, FileType.AUDIO)
else:
mime_type = utils.voiceAudioUrlFuxUps(self, file_name or audio.name, 1)
thumb = await self.save_file(thumb)
file = await self.save_file(audio, progress=progress, progress_args=progress_args)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(file_name or audio.name) or "audio/mpeg",
mime_type=mime_type,
file=file,
thumb=thumb,
attributes=[
Expand Down
6 changes: 4 additions & 2 deletions pyrogram/methods/messages/send_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ async def send_voice(
try:
if isinstance(voice, str):
if os.path.isfile(voice):
mime_type = utils.voiceAudioUrlFuxUps(self, voice, 2)
file = await self.save_file(voice, progress=progress, progress_args=progress_args)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(voice) or "audio/mpeg",
mime_type=mime_type,
file=file,
attributes=[
raw.types.DocumentAttributeAudio(
Expand All @@ -195,9 +196,10 @@ async def send_voice(
else:
media = utils.get_input_media_from_file_id(voice, FileType.VOICE)
else:
mime_type = utils.voiceAudioUrlFuxUps(self, voice.name, 2)
file = await self.save_file(voice, progress=progress, progress_args=progress_args)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(voice.name) or "audio/mpeg",
mime_type=mime_type,
file=file,
attributes=[
raw.types.DocumentAttributeAudio(
Expand Down
21 changes: 21 additions & 0 deletions pyrogram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,24 @@ def get_first_url(text):
matches = re.findall(r"(https?):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])", text)

return f"{matches[0][0]}://{matches[0][1]}{matches[0][2]}" if matches else None


def voiceAudioUrlFuxUps(
client: "pyroram.Client",
file_name: str,
dinxe: int
) -> str:
un_posi_mt = [
"application/zip", # 0
# https://t.me/c/1220993104/1360174
"audio/mpeg", # 1
"audio/ogg", # 2
]
mime_type = client.guess_mime_type(file_name) or un_posi_mt[dinxe]
# BEWARE: https://t.me/c/1279877202/31475
if dinxe == 1 and mime_type == "audio/ogg":
mime_type = "audio/opus"
elif dinxe == 2 and mime_type == "audio/mpeg":
mime_type = "audio/ogg"
# BEWARE: https://t.me/c/1279877202/74
return mime_type

0 comments on commit b33cb63

Please sign in to comment.