Bulk download from Telegram

UPD: Made the example work by using telethon.sync import instead of just telethon.

One day I got a lot of cool photos in Telegram chat. And I asked myself: how can I download them all at once? Because MacOS client for Telegram doesn’t have such feature (there is a feature request on Github). After some searching I found out that this can be done with help of one nice library which is named Telethon. There are more of them but I decided to write my script for bulk downloading in python.

But before some preparations are required:

  1. Goto https://my.telegram.org and generate api id and api hash
  2. Install Telethon library with pip3 install telethon
  3. Run following code (replace api_id, api_hash and username with your values):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from telethon.sync import TelegramClient

api_id=123456
api_hash='secretHashYO'

client = TelegramClient('test_session', api_id, api_hash)
client.start()
# At this point you have to input your phone number and confirmation code

for message in client.get_messages('username', limit=25):
    message.download_media()

The code above downloads media from the last 25 messages from the chat with username in current directory.