如何从电报组下载所有共享媒体文件?

Moh*_*adi 5 telegram python-telegram-bot telegram-bot

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

vic*_*vic 9

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

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)
Run Code Online (Sandbox Code Playgroud)


mh *_*ian 6

自胜利者的回答以来,电视马拉松似乎已经发生了变化。(文档

这应该有效:

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>'))
Run Code Online (Sandbox Code Playgroud)


jsm*_*bom 3

不幸的是,telegram bot api 不允许查看旧消息(或文件)。

实现此目的的唯一方法是使用Telethon等 API ,就 telegram 而言,它充当用户。