如何在 python-telegram-bot 中接收文件?
How can I receive file in python-telegram-bot?
我对 python 电报机器人中的文件消息有疑问。我如何接收文件并读取该文件?或者保存。
您可以:
- 注册一个监听
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()
我对 python 电报机器人中的文件消息有疑问。我如何接收文件并读取该文件?或者保存。
您可以:
- 注册一个监听
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()