Python Telegram Bot: How to send audio voice files using bot API

I’m working on a telegram bot using Python and I can already send text messages and images without problems. Now I want to add functionality to send voice recordings as responses.

I read the telegram bot documentation and it mentions using send_voice method. They suggest using file_id from files that are already stored on telegram servers. I got a file_id by sending a voice message to @RawDataBot which gave me the identifier.

But when I test my code I keep getting this error: telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong file identifier/HTTP URL specified

What could be causing this issue? Here’s my code:

import telebot

TOKEN = <MY_BOT_TOKEN>

mybot = telebot.TeleBot(TOKEN)

@mybot.message_handler(commands=['begin'])
def welcome_msg(msg):
    mybot.send_message(msg.chat.id, welcome_text)

@mybot.message_handler(commands=['image'])
def send_picture(msg):
    mybot.send_photo(msg.chat.id, "https://example.com/sample-image.jpg")

# This is where I try to send voice message when user types /voice
@mybot.message_handler(commands=['voice'])
def send_audio_file(msg):
    mybot.send_voice(msg.chat.id, "AwACAgIAAxkBAAEWjl5i5bjyudWAM9IISKWhE1Gjs5ntQgACLx8AApcNKEv97pVasPhBoCkE")

mybot.polling()

I hit the same issue when I started working with voice messages in telegram bots. Your file_id is probably expired or got corrupted when you copied it. File IDs expire, and manual copying often breaks them. I switched to using a URL instead of file_id and it worked perfectly. Just host your voice file on a web server: mybot.send_voice(msg.chat.id, "https://yourserver.com/voice.ogg"). Make sure it’s OGG format with OPUS encoding - that’s what Telegram wants for voice messages. If you’d rather stick with file_ids, upload a voice message to your bot first, then grab the file_id from that message. It’ll be valid for your bot instance.

File IDs from rawdatabot won’t work with other bots. Use with open('voice.ogg', 'rb') as voice: mybot.send_voice(msg.chat.id, voice) instead. Just make sure your audio file’s in ogg format or it’ll break.

The file_id you obtained from @RawDataBot won’t be valid for your bot. File IDs are specific to the bot that received them and can’t be used across different bots. To resolve this, you should upload your own audio file directly using a local file path. You can achieve this by modifying your send_audio_file function to use local storage, like this: mybot.send_voice(msg.chat.id, open('path/to/your/audio.ogg', 'rb')). Additionally, if you want to use existing audio files from Telegram, you’ll need to receive those messages through your own bot and capture the file_id from there.