Skip to content

Commit 0578cf6

Browse files
committed
Update get_history.py example
1 parent ef93fee commit 0578cf6

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

examples/get_history.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import time
2020

2121
from pyrogram import Client
22-
from pyrogram.api import functions
2322
from pyrogram.api.errors import FloodWait
2423

2524
"""This example shows how to retrieve the full message history of a chat"""
@@ -28,30 +27,27 @@
2827
app.start()
2928

3029
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
3432

3533
while True:
3634
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)
4336
except FloodWait as e:
4437
# For very large chats the method call can raise a FloodWait
38+
print("waiting {}".format(e.x))
4539
time.sleep(e.x) # Sleep X seconds before continuing
4640
continue
4741

48-
if not messages.messages:
49-
break # No more messages left
42+
if not m.messages:
43+
break
5044

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)))
5349

5450
app.stop()
5551

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
5753
# descending order (from the most recent to the oldest one)

0 commit comments

Comments
 (0)