Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download all files from a Telegram channel

I do know how to get all text messages using get_message_history method of Telethon, but I'm wondering if there is a way to download all files sent in a Telegram channel.

msgs = client.get_message_history('a_channel', limit=10000)

for msg in msgs:
    print(msg)
like image 998
Grender Avatar asked Jan 01 '26 09:01

Grender


1 Answers

I hope this code will help you. I used Telethon V0.19, but the previous versions are pretty much the same.

also get_message_history is deprecated, use get_messages instead.

from telethon import TelegramClient

api_id = XXXXXXX
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
phone_number = '+98XXXXXXXXX'
################################################
channel_username = 'tehrandb'
################################################

client = TelegramClient('session_name',
                    api_id,
                    api_hash)

assert client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))

# ---------------------------------------
msgs = client.get_messages(channel_username, limit=100)
for msg in msgs.data:
    if msg.media is not None:
        client.download_media(message=msg)
like image 59
Alihossein shahabi Avatar answered Jan 02 '26 21:01

Alihossein shahabi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!