我知道如何使用Telethon的get_message_history
方法获取所有短信,但我想知道是否有办法下载电报频道中发送的所有文件。
msgs = client.get_message_history('a_channel', limit=10000)
for msg in msgs:
print(msg)
答案 0 :(得分:4)
我希望这段代码可以帮到你。我使用Telethon V0.19
,但以前的版本几乎相同。
也不推荐使用get_message_history
,而是使用get_messages
。
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)