我需要从用户那里得到一个文件,下载它,传递给一个函数来处理它,然后把它发回给用户。
我已经多次阅读文档,但我仍然不明白如何获取 file_id 并使用它。
这是我尝试过的回声代码。它显示一个 TypeError: 'NoneType' object is not subscriptable on the fileID 行。
update.message.reply_text('give a file')
chat_id = update.message.chat_id
fileID = update.message['document']['file_id']
context.bot.sendDocument(chat_id = chat_id,
caption = 'This is the file that you have sent to bot',
document = fileID)
像这样接收您的文件::
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("filename", '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()
--now 像这样发回文件:
import requests
files= { 'file' : open('filepath','rb')}
resp = requests.post('https://api/telegram.org/bot<<token>>/sendfile?chat_id=<>',files=files)
#check response
print(resp.status_code)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句