MTPyroger/examples/get_history.py

32 lines
1012 B
Python
Raw Normal View History

2018-02-26 14:43:03 +00:00
import time
from pyrogram import Client
from pyrogram.api.errors import FloodWait
2018-05-06 09:56:25 +00:00
"""This example shows how to retrieve the full message history of a chat"""
2018-04-14 16:46:54 +00:00
app = Client("my_account")
2018-02-26 14:43:03 +00:00
target = "me" # "me" refers to your own chat (Saved Messages)
2018-05-11 16:00:26 +00:00
messages = [] # List that will contain all the messages of the target chat
offset_id = 0 # ID of the last message of the chunk
2018-02-26 14:43:03 +00:00
2018-10-09 12:48:43 +00:00
with app:
while True:
try:
m = app.get_history(target, offset_id=offset_id)
except FloodWait as e: # For very large chats the method call can raise a FloodWait
print("waiting {}".format(e.x))
time.sleep(e.x) # Sleep X seconds before continuing
continue
if not m.messages:
break
messages += m.messages
offset_id = m.messages[-1].message_id
print("Messages: {}".format(len(messages)))
2018-02-26 16:01:33 +00:00
2018-05-11 16:00:26 +00:00
# Now the "messages" list contains all the messages sorted by date in
2018-02-26 14:43:03 +00:00
# descending order (from the most recent to the oldest one)