Search code examples
pythontelegrampython-telegram-bot

Download Image Python Telegram API


I followed their brief tutorial on downloading an image but I'm encountering an exception:

telegram.photosize.PhotoSize object at ... is not JSON serializable

the function for catching the images looks like this:

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)

At this point I have no idea what I'm doing wrong and can't find any solution on the net.


Solution

  • Turns out I misunderstood the data shape. I thought originally that the update.message.photo collection held just file IDs. This led me to pass the wrong kind of object in when trying to fetch the file by ID. In order to pull out the file ID, I needed to get the file_id off the last photo:

    file_id = update.message.photo[-1].file_id