1818
1919import logging
2020from datetime import datetime
21- from typing import AsyncGenerator , Union
21+ from typing import AsyncIterator , Union , List
2222
23- import pyrogram
2423from pyrogram import raw , types , utils
2524
2625log = logging .getLogger (__name__ )
2726
27+
2828async def get_chunk (
29- * ,
3029 client : "pyrogram.Client" ,
3130 chat_id : Union [int , str ],
31+ * ,
3232 limit : int = 0 ,
3333 offset : int = 0 ,
3434 offset_id : int = 0 ,
3535 from_date : datetime = utils .zero_datetime (),
36- min_id : int = 0 ,
37- max_id : int = 0 ,
38- reverse : bool = False
39- ):
36+ min_id : int = 0 , # Inclusive
37+ max_id : int = 0 , # Inclusive
38+ reverse : bool = False ,
39+ ) -> List [types .Message ]:
40+ # Telegram API requires `offset_id` as starting point, boundaries alone don't work
4041 if (min_id or max_id ) and not offset_id :
4142 if max_id :
4243 offset_id = max_id + 1
@@ -46,7 +47,7 @@ async def get_chunk(
4647 if min_id and max_id and not offset_id :
4748 offset_id = max_id + 1
4849
49- messages = await client .invoke (
50+ history : raw . base . messages . Messages = await client .invoke (
5051 raw .functions .messages .GetHistory (
5152 peer = await client .resolve_peer (chat_id ),
5253 offset_id = offset_id ,
@@ -60,25 +61,25 @@ async def get_chunk(
6061 sleep_threshold = 60
6162 )
6263
63- messages = await utils .parse_messages (client , messages , replies = 0 )
64+ messages = await utils .parse_messages (client , history , replies = 0 )
6465
6566 if reverse :
6667 messages .reverse ()
67-
6868 return messages
6969
70+
7071class GetChatHistory :
7172 async def get_chat_history (
7273 self : "pyrogram.Client" ,
7374 chat_id : Union [int , str ],
7475 limit : int = 0 ,
7576 offset : int = 0 ,
77+ offset_id : int = 0 ,
7678 offset_date : datetime = utils .zero_datetime (),
7779 min_id : int = 0 ,
7880 max_id : int = 0 ,
7981 reverse : bool = False ,
80- offset_id : int = None
81- ) -> AsyncGenerator ["types.Message" , None ]:
82+ ) -> AsyncIterator [types .Message ]:
8283 """Get messages from a chat history.
8384
8485 The messages are returned in reverse chronological order.
@@ -99,14 +100,21 @@ async def get_chat_history(
99100 Sequential number of the first message to be returned.
100101 Negative values are also accepted and become useful in case you set offset_id or offset_date.
101102
103+ offset_id (``int``, *optional*):
104+ Identifier of the first message to be returned.
105+ This parameter is deprecated and should not be used.
106+ Use `min_id` / `max_id` instead for proper filtering.
107+
102108 offset_date (:py:obj:`~datetime.datetime`, *optional*):
103109 Pass a date as offset to retrieve only older messages starting from that date.
104110
105111 min_id (``int``, *optional*):
106- If a positive value was provided, the method will return only messages with IDs more than min_id (inclusive).
112+ If a positive value was provided, the method will return only messages
113+ with IDs more than or equal to min_id (inclusive).
107114
108115 max_id (``int``, *optional*):
109- If a positive value was provided, the method will return only messages with IDs less than max_id (inclusive).
116+ If a positive value was provided, the method will return only messages
117+ with IDs less than or equal to max_id (inclusive).
110118
111119 reverse (``bool``, *optional*):
112120 Pass True to retrieve the messages from oldest to newest.
@@ -121,12 +129,13 @@ async def get_chat_history(
121129 print(message.text)
122130 """
123131 log .warning (
124- "`offset_id` is deprecated and will be removed in future updates. Use `min_id` or `max_id` instead."
132+ "`offset_id` is deprecated and will be removed in future updates. " \
133+ "Use `min_id` or `max_id` instead."
125134 )
126135
127- current = 0
128- total = limit or (1 << 31 ) - 1
129- limit = min (100 , total )
136+ current : int = 0
137+ total : int = limit or (1 << 31 ) - 1
138+ limit : int = min (100 , total ) # We can only retrieve up to 100 messages at a time
130139
131140 min_id = (min_id - 1 ) if min_id else 0 # Make `min_id` inclusive
132141 max_id = (max_id + 1 ) if max_id else 0 # Make `max_id` inclusive
@@ -149,16 +158,13 @@ async def get_chat_history(
149158 min_id = min_id ,
150159 reverse = reverse
151160 )
152-
153161 if not messages :
154162 return
155163
156164 offset_id = messages [- 1 ].id + (1 if reverse else 0 )
157-
158165 for message in messages :
159166 yield message
160167
161168 current += 1
162-
163169 if current >= total :
164- return
170+ return
0 commit comments