1616# You should have received a copy of the GNU Lesser General Public License
1717# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818
19+ import logging
1920from asyncio import sleep
2021from datetime import datetime
21- from typing import Union , Optional , AsyncGenerator
22+ from typing import AsyncGenerator , Optional , Union
2223
2324import pyrogram
24- from pyrogram import types , raw , utils
25+ from pyrogram import raw , types , utils
26+
27+ log = logging .getLogger (__name__ )
2528
2629
2730async def get_chunk (
@@ -32,11 +35,11 @@ async def get_chunk(
3235 offset : int = 0 ,
3336 min_id : int = 0 ,
3437 max_id : int = 0 ,
35- from_message_id : int = 0 ,
38+ offset_id : int = 0 ,
3639 from_date : datetime = utils .zero_datetime (),
3740 reverse : bool = False ,
3841 is_scheduled : bool = False
39- ):
42+ ) -> list [ types . Message ] :
4043 if is_scheduled :
4144 r = await client .invoke (
4245 raw .functions .messages .GetScheduledHistory (
@@ -55,13 +58,19 @@ async def get_chunk(
5558 messages .reverse ()
5659 return messages
5760 else :
58- from_message_id = from_message_id or (1 if reverse else 0 )
59- messages = await client .invoke (
61+ if (min_id or max_id ) and not offset_id :
62+ if max_id :
63+ offset_id = max_id + 1
64+ elif min_id :
65+ offset_id = 0
66+ if min_id and max_id and not offset_id :
67+ offset_id = max_id + 1
68+ history : raw .base .messages .Messages = await client .invoke (
6069 raw .functions .messages .GetHistory (
6170 peer = await client .resolve_peer (chat_id ),
62- offset_id = from_message_id ,
71+ offset_id = offset_id ,
6372 offset_date = utils .datetime_to_timestamp (from_date ),
64- add_offset = offset * ( - 1 if reverse else 1 ) - ( limit if reverse else 0 ) ,
73+ add_offset = offset ,
6574 limit = limit ,
6675 max_id = max_id ,
6776 min_id = min_id ,
@@ -71,7 +80,7 @@ async def get_chunk(
7180 )
7281 messages = await utils .parse_messages (
7382 client ,
74- messages ,
83+ history ,
7584 is_scheduled = False ,
7685 replies = 0
7786 )
@@ -85,8 +94,9 @@ async def get_chat_history(
8594 self : "pyrogram.Client" ,
8695 chat_id : Union [int , str ],
8796 limit : int = 0 ,
97+ * ,
8898 offset : int = 0 ,
89- offset_id : int = 0 ,
99+ offset_id : int = None ,
90100 min_id : int = 0 ,
91101 max_id : int = 0 ,
92102 offset_date : datetime = utils .zero_datetime (),
@@ -95,7 +105,7 @@ async def get_chat_history(
95105 ) -> Optional [AsyncGenerator ["types.Message" , None ]]:
96106 """Get messages from a chat history.
97107
98- The messages are returned in reverse chronological order.
108+ The messages are returned in reverse chronological order by default .
99109
100110 .. include:: /_includes/usable-by/users.rst
101111
@@ -115,6 +125,8 @@ async def get_chat_history(
115125
116126 offset_id (``int``, *optional*):
117127 Identifier of the first message to be returned.
128+ This parameter is deprecated and should not be used.
129+ Use ``min_id`` / ``max_id`` instead for proper filtering.
118130
119131 min_id (``int``, *optional*):
120132 If a positive value was transferred, the method will return only messages with IDs more than min_id.
@@ -142,17 +154,28 @@ async def get_chat_history(
142154 async for message in app.get_chat_history(chat_id):
143155 print(message.text)
144156 """
145- current = 0
146- total = limit or (1 << 31 ) - 1
147- limit = min (100 , total )
157+ if offset_id is not None :
158+ log .warning (
159+ "`offset_id` is deprecated and will be removed in future updates. Use `min_id` or `max_id` instead."
160+ )
161+
162+ current : int = 0
163+ total : int = limit or (1 << 31 ) - 1
164+ limit : int = min (100 , total )
165+
166+ if reverse :
167+ offset_id = min_id if min_id else 1
168+ offset = offset - limit
169+ else :
170+ offset_id = max_id if max_id else 0
148171
149172 while True :
150173 messages = await get_chunk (
151174 client = self ,
152175 chat_id = chat_id ,
153176 limit = limit ,
154177 offset = offset ,
155- from_message_id = offset_id ,
178+ offset_id = offset_id ,
156179 min_id = min_id ,
157180 max_id = max_id ,
158181 from_date = offset_date ,
0 commit comments