保存附加的声音文件远程机器人

问题描述 投票:0回答:1

我正在尝试将电报中附加的声音保存到我的计算机上,但我不知道该怎么做。我找到了类似的代码,但它不起作用。

@bot.message_handler(content_types=['document', 'photo', 'audio','video', 'voice'])

def addfile(message):
        file_name = message.document.file_name
        file_info = bot.get_file(message.document.file_id)
        downloaded_file = bot.download_file(file_info.file_path)
        with open(file_name, 'wb') as new_file:
            new_file.write(downloaded_file)

由于某种原因,发送音频文件时,机器人根本看不到它们并显示值 None

python python-3.x telegram-bot telebot
1个回答
0
投票

我找到了解决方案,如果有人需要,就在这里

@bot.message_handler(content_types=['audio'])
def process_audio(message):
    downloaded_file = bot.download_file(bot.get_file(message.audio.file_id).file_path)
    with open(message.audio.file_name, 'wb') as new_file:
        new_file.write(downloaded_file)
© www.soinside.com 2019 - 2024. All rights reserved.