17

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

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
M P
  • 173
  • 1
  • 1
  • 9

4 Answers4

13

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:

screenshot

You'll need to install ffmpeg using the package manager of your choice.

Besi
  • 22,579
  • 24
  • 131
  • 223
  • Until I started to convert ogg vorbis to opus I was getting weird results. Sometimes telegram was showing audio files as voice messages, other times as a audio files you need to download and play in external player. – italankin Jan 28 '21 at 04:38
  • 4
    This works but only if uploaded from Android, if uploaded from PC it doesn't have the bars. BTW: no need for Python, I called ffmpeg directly from commandline… `ffmpeg -i input.mp3 -acodec libopus output.ogg -y` – lapo Nov 05 '21 at 09:47
3

The MIME-Type for the sendVoice method must be set to audio/ogg. Your sample is set to video/ogg.

More infos on the sendVoice method with an url can be found here

Shiffty
  • 2,086
  • 2
  • 26
  • 31
3

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)
Mos
  • 31
  • 2
1

if you want it to display the audio spectrogram, make sure the ogg is encoded with opus codec

Yoily L
  • 412
  • 3
  • 8