我正在尝试将电报中附加的声音保存到我的计算机上,但我不知道该怎么做。我找到了类似的代码,但它不起作用。
@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
我找到了解决方案,如果有人需要,就在这里
@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)