1818
1919import logging
2020from datetime import datetime
21- from typing import AsyncGenerator , Union
21+ from typing import AsyncIterator , Union , List
2222
2323import pyrogram
2424from pyrogram import raw , types , utils
2525
2626log = logging .getLogger (__name__ )
2727
28+
2829async def get_chunk (
29- * ,
3030 client : "pyrogram.Client" ,
3131 chat_id : Union [int , str ],
32+ * ,
3233 limit : int = 0 ,
3334 offset : int = 0 ,
3435 offset_id : int = 0 ,
3536 from_date : datetime = utils .zero_datetime (),
36- min_id : int = 0 ,
37- max_id : int = 0 ,
38- reverse : bool = False
39- ):
37+ min_id : int = 0 , # Inclusive
38+ max_id : int = 0 , # Inclusive
39+ reverse : bool = False ,
40+ ) -> List ["types.Message" ]:
41+ # Telegram API requires `offset_id` as starting point, boundaries alone don't work
4042 if (min_id or max_id ) and not offset_id :
4143 if max_id :
4244 offset_id = max_id + 1
@@ -46,7 +48,7 @@ async def get_chunk(
4648 if min_id and max_id and not offset_id :
4749 offset_id = max_id + 1
4850
49- messages = await client .invoke (
51+ history : raw . base . messages . Messages = await client .invoke (
5052 raw .functions .messages .GetHistory (
5153 peer = await client .resolve_peer (chat_id ),
5254 offset_id = offset_id ,
@@ -60,25 +62,26 @@ async def get_chunk(
6062 sleep_threshold = 60
6163 )
6264
63- messages = await utils .parse_messages (client , messages , replies = 0 )
65+ messages = await utils .parse_messages (client , history , replies = 0 )
6466
6567 if reverse :
6668 messages .reverse ()
67-
69+
6870 return messages
6971
72+
7073class GetChatHistory :
7174 async def get_chat_history (
7275 self : "pyrogram.Client" ,
7376 chat_id : Union [int , str ],
7477 limit : int = 0 ,
7578 offset : int = 0 ,
79+ offset_id : int = None ,
7680 offset_date : datetime = utils .zero_datetime (),
7781 min_id : int = 0 ,
7882 max_id : int = 0 ,
7983 reverse : bool = False ,
80- offset_id : int = None
81- ) -> AsyncGenerator ["types.Message" , None ]:
84+ ) -> AsyncIterator ["types.Message" ]:
8285 """Get messages from a chat history.
8386
8487 The messages are returned in reverse chronological order.
@@ -99,14 +102,21 @@ async def get_chat_history(
99102 Sequential number of the first message to be returned.
100103 Negative values are also accepted and become useful in case you set offset_id or offset_date.
101104
105+ offset_id (``int``, *optional*):
106+ Identifier of the first message to be returned.
107+ This parameter is deprecated and should not be used.
108+ Use `min_id` / `max_id` instead for proper filtering.
109+
102110 offset_date (:py:obj:`~datetime.datetime`, *optional*):
103111 Pass a date as offset to retrieve only older messages starting from that date.
104112
105113 min_id (``int``, *optional*):
106- If a positive value was provided, the method will return only messages with IDs more than min_id (inclusive).
114+ If a positive value was provided, the method will return only messages
115+ with IDs more than or equal to min_id (inclusive).
107116
108117 max_id (``int``, *optional*):
109- If a positive value was provided, the method will return only messages with IDs less than max_id (inclusive).
118+ If a positive value was provided, the method will return only messages
119+ with IDs less than or equal to max_id (inclusive).
110120
111121 reverse (``bool``, *optional*):
112122 Pass True to retrieve the messages from oldest to newest.
@@ -120,13 +130,15 @@ async def get_chat_history(
120130 async for message in app.get_chat_history(chat_id):
121131 print(message.text)
122132 """
123- log .warning (
124- "`offset_id` is deprecated and will be removed in future updates. Use `min_id` or `max_id` instead."
125- )
133+ if offset_id is not None :
134+ log .warning (
135+ "`offset_id` is deprecated and will be removed in future updates. " \
136+ "Use `min_id` or `max_id` instead."
137+ )
126138
127- current = 0
128- total = limit or (1 << 31 ) - 1
129- limit = min (100 , total )
139+ current : int = 0
140+ total : int = limit or (1 << 31 ) - 1
141+ limit : int = min (100 , total ) # We can only retrieve up to 100 messages at a time
130142
131143 min_id = (min_id - 1 ) if min_id else 0 # Make `min_id` inclusive
132144 max_id = (max_id + 1 ) if max_id else 0 # Make `max_id` inclusive
@@ -149,16 +161,15 @@ async def get_chat_history(
149161 min_id = min_id ,
150162 reverse = reverse
151163 )
152-
164+
153165 if not messages :
154166 return
155167
156168 offset_id = messages [- 1 ].id + (1 if reverse else 0 )
157-
158169 for message in messages :
159170 yield message
160171
161172 current += 1
162-
173+
163174 if current >= total :
164175 return
0 commit comments