Telegram has two APIs for sending Audio files: sendVoice
and sendAudio
To have spectrogram, you need to use the sendVoice API.
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
Also, note the other limitations thats mentioned in the docs:
To use sendVoice, the file must have the type audio/ogg and be no more than 1MB in size. 1-20MB voice notes will be sent as files.
Therefore, if you send a ogg file in opus format that is less than 1MB in size, you will get spectogram. If the ogg file is larger than 1MB, it will be still uploaded as a voice however with no spectogram bars.
You can use ffmpeg in linux with a low bitrate to reduce the size of your file:
ffmpeg -i input.mp3 -vn -acodec libopus -b:a 16k audio.ogg
Here is a sample python code that uses to send a sample ogg file to a channel using sendVoice
import os, sys
import requests
# Bot token
token = ''
if len(sys.argv) <=1:
print("Usage: send_voice.py destchannelid filename")
print("\te.g.")
print("\tsend_voice.py -10011111111 audio.ogg")
print("Convert audio files to ogg using the following command")
print("ffmpeg -i input.mp3 -vn -acodec libopus -b:a 16k audio.ogg")
exit()
upload_audio_url = "https://api.telegram.org/bot{}/sendVoice?chat_id={}".format(token,chat_id)
audio_path_ogg = os.getcwd() + "/" + sys.argv[2]
with open(audio_path_ogg, 'rb') as f:
data = f.read()
file = {'voice': ('Message.ogg', data)}
result = requests.post(upload_audio_url, files=file)
print(result.content)