4

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.

marqueewinq
  • 367
  • 2
  • 12

2 Answers2

7

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
Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
marqueewinq
  • 367
  • 2
  • 12
  • Please have a look at [Can I answer my own question?](http://stackoverflow.com/help/self-answer) and come back two days later and check as answered. But add some information for other users. – help-info.de May 11 '17 at 18:03
  • 1
    'newFile = bot.getFile(file_id)', i can't able to fetch getFile, error says bot object has no attribute 'getFile'.. can you please help me. – Nilanj Jan 24 '20 at 07:32
  • @Nilanj , Try like this `bot = context.bot getFile = bot.getFile(update.message.photo[-1].file_id)` . – KbiR Jul 28 '20 at 04:27
0
# 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)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '22 at 17:19