【发布时间】:2020-06-08 01:29:30
【问题描述】:
我对 python 电报机器人中的文件消息有疑问。如何接收文件并读取该文件?或者保存它。
【问题讨论】:
-
你尝试了什么?你的代码在哪里?有telegram.Voice.get_file
标签: python python-3.x telegram-bot python-telegram-bot
我对 python 电报机器人中的文件消息有疑问。如何接收文件并读取该文件?或者保存它。
【问题讨论】:
标签: python python-3.x telegram-bot python-telegram-bot
你可以:
Document的处理程序
File 对象(在侦听器内部使用get_file).download() 下载文档这里有一个示例代码可以帮助您入门:
from telegram.ext import Updater, MessageHandler, Filters
BOT_TOKEN = ' ... '
def downloader(update, context):
context.bot.get_file(update.message.document).download()
# writing to a custom file
with open("custom/file.doc", 'wb') as f:
context.bot.get_file(update.message.document).download(out=f)
updater = Updater(BOT_TOKEN, use_context=True)
updater.dispatcher.add_handler(MessageHandler(Filters.document, downloader))
updater.start_polling()
updater.idle()
【讨论】:
out 传递。我已经更新了答案,向您展示如何做到这一点