下载图像 Python Telegram API

mar*_*inq 4 python telegram python-telegram-bot

我遵循了他们关于下载图像的简短教程,但遇到了一个例外:

telegram.photosize.PhotoSize object at ... is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

捕捉图像的功能如下所示:

def photo(bot, update):
    file_id = update.message.photo[-1]
    newFile = bot.getFile(file_id)
    newFile.download('test.jpg')
    bot.sendMessage(chat_id=update.message.chat_id, text="download succesfull")

photo_handler = MessageHandler(Filters.photo, photo)
dispatcher.add_handler(photo_handler)
Run Code Online (Sandbox Code Playgroud)

在这一点上,我不知道我做错了什么,也无法在网上找到任何解决方案。

mar*_*inq 6

原来我误解了数据形状。我最初认为该update.message.photo集合仅包含文件 ID。这导致我在尝试通过 ID 获取文件时传递了错误类型的对象。为了拉出文件ID,我需要file_id关闭最后一张照片:

file_id = update.message.photo[-1].file_id
Run Code Online (Sandbox Code Playgroud)