我在python电报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", 'w') 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()