2
votes

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)
1
@Sean Ehm... What? - Grender

1 Answers

3
votes

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)