|
19 | 19 | import time |
20 | 20 |
|
21 | 21 | from pyrogram import Client |
22 | | -from pyrogram.api import functions |
23 | 22 | from pyrogram.api.errors import FloodWait |
24 | 23 |
|
25 | 24 | """This example shows how to retrieve the full message history of a chat""" |
|
28 | 27 | app.start() |
29 | 28 |
|
30 | 29 | target = "me" # "me" refers to your own chat (Saved Messages) |
31 | | -history = [] # List that will contain all the messages of the target chat |
32 | | -limit = 100 # Amount of messages to retrieve for each API call |
33 | | -offset = 0 # Offset starts at 0 |
| 30 | +messages = [] # List that will contain all the messages of the target chat |
| 31 | +offset_id = 0 # ID of the last message of the chunk |
34 | 32 |
|
35 | 33 | while True: |
36 | 34 | try: |
37 | | - messages = app.send( |
38 | | - functions.messages.GetHistory( |
39 | | - app.resolve_peer(target), |
40 | | - 0, 0, offset, limit, 0, 0, 0 |
41 | | - ) |
42 | | - ) |
| 35 | + m = app.get_history(target, offset_id=offset_id) |
43 | 36 | except FloodWait as e: |
44 | 37 | # For very large chats the method call can raise a FloodWait |
| 38 | + print("waiting {}".format(e.x)) |
45 | 39 | time.sleep(e.x) # Sleep X seconds before continuing |
46 | 40 | continue |
47 | 41 |
|
48 | | - if not messages.messages: |
49 | | - break # No more messages left |
| 42 | + if not m.messages: |
| 43 | + break |
50 | 44 |
|
51 | | - history.extend(messages.messages) |
52 | | - offset += limit |
| 45 | + messages += m.messages |
| 46 | + offset_id = m.messages[-1].message_id |
| 47 | + |
| 48 | + print("Messages: {}".format(len(messages))) |
53 | 49 |
|
54 | 50 | app.stop() |
55 | 51 |
|
56 | | -# Now the "history" list contains all the messages sorted by date in |
| 52 | +# Now the "messages" list contains all the messages sorted by date in |
57 | 53 | # descending order (from the most recent to the oldest one) |
0 commit comments