I’m working on a Telegram bot that should save images when users send them to the bot. I’m using the latest python-telegram-bot library version 21 and having trouble getting it to work properly.
hey! jpg images in telegram come as Photo messages, not documents. use filters.PHOTO instead of filters.Document.IMAGE. also set a download path with await photo_file.download_to_drive('path/filename.jpg') or it’ll just dump everything in temp
You’re mixing up photo types in Telegram. Regular JPG images sent through the photo button use filters.PHOTO, but images dragged and dropped as files need filters.Document.IMAGE. Set up two separate handlers to catch both.
For downloads, download_to_drive() without parameters just saves to a temp directory that gets wiped. Control where files go with something like await photo_file.download_to_drive(f'images/{photo_file.file_id}.jpg').
BTW, update.message.effective_attachment[-1] grabs the highest resolution photo, but for documents just use update.message.document directly. Your v21 async syntax looks good though.
Your code needs separate handlers for different attachment types. When users send photos through Telegram’s normal photo picker, they come as Photo objects and need filters.PHOTO. But if someone sends an image file as a document, you’ll need filters.Document.IMAGE. I hit the same confusion building my first image bot. The trick is understanding how Telegram categorizes uploads internally. Also, effective_attachment[-1] works for photos but breaks on document handlers since documents don’t have multiple resolution variants. For file paths, download_to_drive() without arguments dumps files in system temp with random names. First create your target directory with os.makedirs('saved_images', exist_ok=True), then specify the full path in the download method. This stops files from disappearing and gives you predictable naming.