Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

python 3.x - How do I download Telegram messages in reverse?

I am getting all messages of a Telegram channel via GetHistoryRequest. I would like to run a script every day, which gives me the new messages. Is it possible to reverse the order of the messages inside GetHistoryRequest? I have seen an additional property reverse=True, but this gives me an error message. Here is the code (which is available online)

history = client(GetHistoryRequest(
            peer=my_channel,
            offset_id=0,
            offset_date=None,
            add_offset=0,
            limit=limit,
            max_id=0,
            min_id=0,
            hash=0
        ))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works here . Downaload 100 message

from telethon.tl import functions, types

channel_link = "https://t.me/joinchat/xxxxxx-xxx"
channel_hash = channel_link.strip().split("joinchat/",1)[1]


channel = await client(functions.messages.CheckChatInviteRequest(hash=channel_hash))
    async for message in client.iter_messages(channel.chat, limit = 100,reverse=True):

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...