D
D
Danya  2020-08-25 21:43:42
Bots
Danya  , 2020-08-25 21:43:42

TelegramBOT saving voice messages?

How to save a voice message to a folder
Example: I send a voice message to a bot, it saves it as an audio file and sends it back to me (file)
How can I implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-08-25
@MrBrainop

When will we learn to google and study documentation , examples ?
The code below, when sending a voice note, saves the file in *. ogg with the name userId_unixtime (I cut the microseconds clumsily, but it will do anyway). By command / start - the bot will send the saved files of voice notes to the user who recorded them.

# -*- coding: utf-8 -*-
import telebot
from time import time
import os

bot = telebot.TeleBot(token_test)


@bot.message_handler(content_types=['voice'])
def voice_processing(message):
    file_info = bot.get_file(message.voice.file_id)
    downloaded_file = bot.download_file(file_info.file_path)
    with open(f'{message.chat.id}_{int(time())}.ogg', 'wb') as new_file:
        new_file.write(downloaded_file)


@bot.message_handler(commands=['start'])
def voice_send(message):
    l_send = [filename for filename in os.listdir() if filename.startswith(f'{message.chat.id}')]
    for f in l_send:
        voice = open(f'{f}', 'rb')
        bot.send_voice(chat_id=message.chat.id, voice=voice)


if __name__ == "__main__":
    try:
        bot.polling(none_stop=True)
    except Exception as e:
        print(e)

K
kirillinyakin, 2020-08-25
@kirillinyakin

As far as I remember, when you send a voice message to the bot, then in the object that comes to the handler, there is a voice file id that you can save and then send it back

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question