【问题标题】:How to download all shared media files from a telegram group?如何从电报组下载所有共享媒体文件?
【发布时间】:2018-03-20 07:29:23
【问题描述】:

有一个电报群,里面有超过 40,000 个共享文件。
是否有任何机器人可以一次下载所有这些?如果没有,有没有使用python下载共享媒体文件的电报api脚本方法?

【问题讨论】:

    标签: telegram telegram-bot python-telegram-bot


    【解决方案1】:

    您可以使用 Telegram 客户端 Telethon 下载公共组中的所有文件:

    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('vic')
    messages = client.get_messages('intothestates', limit=2000)
    print(len(messages))
    for msg in tqdm(messages):
        client.download_media(msg)
    

    【讨论】:

    【解决方案2】:

    似乎Telethon 自从 victorvs 的回答后发生了变化。 (Docs)

    这应该可行:

    from telethon.sync import TelegramClient, events
    from tqdm import  tqdm
    import os
    
    # These example values won't work. You must get your own api_id and
    # api_hash from https://my.telegram.org, under API Development.
    api_id = <api_id>
    api_hash = '<api_hash>'
    
    
    with TelegramClient('name', api_id, api_hash) as client:
        messages = client.get_messages('<channel/chat>', limit=50) # limit defaults to 1
        for msg in tqdm(messages):
            msg.download_media(file=os.path.join('media', '<file_name>'))
    

    【讨论】:

      【解决方案3】:

      遗憾的是,电报机器人 API 不允许查看旧消息(或文件)。

      做到这一点的唯一方法是使用诸如Telethon 之类的API,就电报而言,它充当用户。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-17
        • 2021-08-13
        • 2022-06-13
        • 2023-03-31
        • 2016-09-02
        • 2016-11-04
        相关资源
        最近更新 更多