how save photo in telegram python bot?


how save photo in telegram python bot?



i want to write a telegram bot that save photos .
this is my code , but its not working.
and i don't know what is my problem?


def image_handler(bot, update):
file = bot.getFile(update.message.photo.file_id)
print ("file_id: " + str(update.message.photo.file_id))
file.download('image.jpg')

updater.dispatcher.add_handler(MessageHandler(Filters.photo, image_handler))
updater.start_polling()
updater.idle()



pleas help me to solve my problem.




1 Answer
1



update.message.photo is an array of photos sizes (PhotoSize objects).


update.message.photo



Use file = bot.getFile(update.message.photo[-1].file_id). This will get the image with biggest size available.


file = bot.getFile(update.message.photo[-1].file_id)





why i should write "[-1]" ? what is this?
– Ali
May 19 at 20:27





@Ali [-1] is the last item of array. This is how indices work in python. I recommend getting familiar with it as it's very common feature. You actually don't have to use the last item, but last image is the biggest image according to docs (github.com/python-telegram-bot/python-telegram-bot/wiki/…)
– dev4Fun
May 19 at 21:05






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter