How to download all shared media files from a telegram group?

Mohsen Haddadi picture Mohsen Haddadi · Oct 9, 2017 · Viewed 24.5k times · Source

There is a telegram group with more than 40,000 shared files in it.
Is there any bot to download all of them all at once? If not is there any telegram api script method using python to download shared media files?

Answer

victor vs picture victor vs · Mar 2, 2019

you can use telethon ,a telegram client to download all the files in a public group

from telethon import TelegramClient
from tqdm import tqdm
# These example values won't work. You must get your own api_id and
# api_hash from `my.telegram.org` , under API Development.
api_id = APIID
api_hash = 'APIHASH'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
print(client.get_me().stringify())
# client.send_message('username', 'Hello! Talking to you from Telethon')
# client.send_file('username', '/home/myself/Pictures/holidays.jpg')
# client.download_profile_photo('hamidzr')
messages = client.get_messages('intothestates', limit=2000)
print(len(messages))
for msg in tqdm(messages):
client.download_media(msg)