A
A
Andrew2020-05-10 19:46:19
Python
Andrew, 2020-05-10 19:46:19

How to download an audio message from a user in a telegram bot?

Expensive time of day. There is a question. I want to make a telegram bot that could read voice messages from the user and translate them into the desired language. The question is how to make the bot automatically understand that he was sent audio and save it to the specified directory?
PS I use the TelebotPyAPI library

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2020-05-10
@OdAs

import telebot
import requests

token = 'Токен'
bot = telebot.TeleBot(token)

@bot.message_handler(content_types=['voice','text'])
def repeat_all_message(message):
  file_info = bot.get_file(message.voice.file_id)
  file = requests.get('https://api.telegram.org/file/bot{0}/{1}'.format(token, file_info.file_path))

  with open('voice.ogg','wb') as f:
    f.write(file.content)

if __name__ == '__main__':
    bot.polling(none_stop=True)

The simplest code. It receives a voice message from the user and saves it to the bot folder named voice.ogg.
Here all records are overwritten in one file. So create a directory, specify the user id as the name. And save files there. I think you'll understand.

D
DellRosso, 2021-01-30
@DellRosso

Is it possible to download ALL audio messages from a specific user?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question