skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. # Download Image Python Telegram 
    @bot.message_handler(content_types=['photo'])
    def download_image(message):
        fileID = message.photo[-1].file_id
        file_info = bot.get_file(fileID)
        downloaded_file = bot.download_file(file_info.file_path)
        # guardamos en la carpeta del proyecto
        with open("image.jpg", 'wb') as new_file:
            new_file.write(downloaded_file)
        bot.reply_to(message, "Image Downloaded type: " + fileID)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search