I'm trying to send a voice message through SendVoice method in telegram bot, but it sends the voice as a document file (not play).
ogg file by ffmpeg converted to opus encoding.
https://api.telegram.org/bot<token>/sendVoice?chat_id=x&voice=http://music-farsi.ir/ogg/voice.ogg
What is the difference between the my ogg file and telegram voice message?
My ogg file: ogg file
Thanks to YoilyL I'm able to send voice messages with the spectrogram.
Here's my python script which converts my .wav
file to .ogg
:
import os
import requests
import subprocess
token = YYYYYYY
chat_id = XXXXXXXX
upload_audio_url = "https://api.telegram.org/bot%s/sendAudio?chat_id=%s" % (token, chat_id)
audio_path_wav = '/Users/me/some-file.wav'
# Convert the file from wav to ogg
filename = os.path.splitext(audio_path_wav)[0]
audio_path_ogg = filename + '.ogg'
subprocess.run(["ffmpeg", '-i', audio_path_wav, '-acodec', 'libopus', audio_path_ogg, '-y'])
with open(audio_path_ogg, 'rb') as f:
data = f.read()
# An arbitrary .ogg filename has to be present so that the spectogram is shown
file = {'audio': ('Message.ogg', data)}
result = requests.post(upload_audio_url, files=file)
This results in the following rendering of the voice message:
You'll need to install ffmpeg
using the package manager of your choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With