added option to reverse get_history order

added an argument `reverse` to get_history which if set to True returns the messages from first to last instead of from newest to oldest.
This commit is contained in:
YoilyL 2018-07-01 21:34:05 +03:00 committed by GitHub
parent 3ea7ea2c83
commit 357253b63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,8 @@ class GetHistory(BaseClient):
offset: int = 0,
limit: int = 100,
offset_id: int = 0,
offset_date: int = 0):
offset_date: int = 0,
reverse: bool = False):
"""Use this method to retrieve the history of a chat.
You can get up to 100 messages at once.
@ -52,6 +53,9 @@ class GetHistory(BaseClient):
offset_date (``int``, *optional*):
Pass a date in Unix time as offset to retrieve only older messages starting from that date.
reverse (``bool``, *optional*):
get the messages in order from first to last (instead of newest to oldest).
Returns:
On success, a :obj:`Messages <pyrogram.Messages>` object is returned.
@ -59,7 +63,9 @@ class GetHistory(BaseClient):
Raises:
:class:`Error <pyrogram.Error>`
"""
if reverse:
offset = -limit*(offset + 1)
if offset_id: offset_id +=1
r = self.send(
functions.messages.GetHistory(
peer=self.resolve_peer(chat_id),
@ -107,5 +113,5 @@ class GetHistory(BaseClient):
return pyrogram.Messages(
total_count=getattr(r, "count", len(r.messages)),
messages=messages
messages=messages if not reverse else messages[::-1]
)