LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.87k stars 1.4k forks source link

DocumentAttributeAudio ignored voice=False with io.BytesIO() object #4170

Open Platon2612 opened 1 year ago

Platon2612 commented 1 year ago

Code that causes the issue

import io from gtts import gTTS

img_byte_arr = io.BytesIO() tts = gTTS(text=text, lang="ru", lang_check=True) tts.write_to_fp(img_byte_arr) img_byte_arr.name = "name.ogg" img_byte_arr.seek(0) await client.send_file(entity=chat_id, caption="", file=img_byte_arr, voice_note=False, attributes=[telethon.tl.types.DocumentAttributeAudio(duration=10, title="Title", performer="Performer", voice=False)]) # Sends only a voice message ignoring voice_note=False and voice=False (bug)

await client.send_file(entity=chat_id, caption="", file="audio.mp3", voice_note=False, attributes=[telethon.tl.types.DocumentAttributeAudio(duration=10, title="Title", performer="Performer", voice=False)]) # Sends the document as needed

await client.send_file(entity=chat_id, caption="", file=img_byte_arr, voice_note=False) # Sends the document as needed

Expected behavior

Must send as a file.

Actual behavior

When sending a BytesIO() object, the parameters of sending as a file are ignored (sent only as a voice message).

If you send a file recorded on disk, then audio is sent as a file (as needed).

Traceback

No response

Telethon version

1.28.5, 1.29.2

Python version

3.7

Operating system (including distribution name and version)

Ubuntu 18.04, Windows 8.1

Other details

No response

Checklist

NotStatilko commented 1 year ago

Try the force_document keyword for send_file

https://docs.telethon.dev/en/stable/modules/client.html#telethon.client.uploads.UploadMethods.send_file

Telegram voices is not an MP3 files, it's OGG. Obviously audio.mp3 will be not voice.

Platon2612 commented 1 year ago

I checked your option, send as a file, but the telethon.tl.types.DocumentAttribute Audio attribute is ignored and is displayed in the artist column as "Unknown artist".

`await client.send_file(entity=chat_id, caption="", file=img_byte_arr, force_document=True, voice_note=False, attributes=[telethon.tl.types.DocumentAttributeAudio(duration=10, title="Title", performer="Performer", voice=False)]) # Sends the audio document as needed but ignore telethon.tl.types.DocumentAttributeAudio

await client.send_file(entity=chat_id, caption="", file="audio.ogg", force_document=True, voice_note=False, attributes=[telethon.tl.types.DocumentAttributeAudio(duration=10, title="Title", performer="Performer", voice=False)]) # Sends the audio document as needed but ignore telethon.tl.types.DocumentAttributeAudio`

As I understand it, the voice_note flag is no longer required if the force_document=True flag is present.