简体   繁体   中英

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM