Skip to content

Commit 4ebd7ea

Browse files
feat: Added the get_top_chats method
1 parent 0908d92 commit 4ebd7ea

5 files changed

Lines changed: 164 additions & 0 deletions

File tree

compiler/docs/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def get_title_list(s: str) -> list:
392392
get_chat_settings
393393
transfer_chat_ownership
394394
get_suitable_discussion_chats
395+
get_top_chats
395396
set_chat_discussion_group
396397
set_chat_member_tag
397398
set_main_profile_tab
@@ -1273,6 +1274,7 @@ def get_title_list(s: str) -> list:
12731274
MaskPointType
12741275
SuggestedPostRefundReason
12751276
SuggestedPostState
1277+
TopChatCategory
12761278
""",
12771279
)
12781280

pyrogram/enums/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from .stories_privacy_rules import StoriesPrivacyRules
5656
from .suggested_post_refund_reason import SuggestedPostRefundReason
5757
from .suggested_post_state import SuggestedPostState
58+
from .top_chat_category import TopChatCategory
5859
from .upgraded_gift_origin import UpgradedGiftOrigin
5960
from .user_status import UserStatus
6061

@@ -98,6 +99,7 @@
9899
'StoriesPrivacyRules',
99100
'SuggestedPostRefundReason',
100101
'SuggestedPostState',
102+
'TopChatCategory',
101103
'UpgradedGiftOrigin',
102104
'UserStatus'
103105
]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from enum import auto
20+
21+
from .auto_name import AutoName
22+
23+
24+
class TopChatCategory(AutoName):
25+
"""Represents the categories of chats for which a list of frequently used chats can be retrieved.
26+
Used in :meth:`~pyrogram.Client.get_top_chats`.
27+
"""
28+
29+
USERS = auto()
30+
"A category containing frequently used private chats with non-bot users"
31+
32+
BOTS = auto()
33+
"A category containing frequently used private chats with bot users"
34+
35+
GROUPS = auto()
36+
"A category containing frequently used basic groups and supergroups"
37+
38+
CHANNELS = auto()
39+
"A category containing frequently used channels"
40+
41+
INLINE_BOTS = auto()
42+
"A category containing frequently used chats with inline bots sorted by their usage in inline mode"
43+
44+
GUEST_BOTS = auto()
45+
"A category containing frequently used chats with bots, which were used as guest bots"
46+
47+
WEB_APP_BOTS = auto()
48+
"A category containing frequently used chats with bots, which Web Apps were opened"
49+
50+
CALLS = auto()
51+
"A category containing frequently used chats used for calls"
52+
53+
FORWARD_CHATS = auto()
54+
"A category containing frequently used chats used to forward messages"

pyrogram/methods/chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from .get_chat_settings import GetChatSettings
4646
from .get_similar_channels import GetSimilarChannels
4747
from .get_suitable_discussion_chats import GetSuitableDiscussionChats
48+
from .get_top_chats import GetTopChats
4849
from .get_dialogs import GetDialogs
4950
from .get_direct_messages_topics_by_id import GetDirectMessagesTopicsByID
5051
from .get_direct_messages_topics import GetDirectMessagesTopics
@@ -168,6 +169,7 @@ class Chats(
168169
GetChatSettings,
169170
GetSimilarChannels,
170171
GetSuitableDiscussionChats,
172+
GetTopChats,
171173
GetSendAsChats,
172174
SetSendAsChat,
173175
SetChatProtectedContent
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import AsyncGenerator
20+
21+
import pyrogram
22+
from pyrogram import enums, raw, types, utils
23+
24+
25+
class GetTopChats:
26+
async def get_top_chats(
27+
self: "pyrogram.Client",
28+
category: "enums.TopChatCategory",
29+
limit: int = 0,
30+
) -> AsyncGenerator["types.Chat", None]:
31+
"""Returns a list of frequently used chats.
32+
33+
.. include:: /_includes/usable-by/users.rst
34+
35+
Parameters:
36+
category (:obj:`~pyrogram.enums.TopChatCategory`):
37+
Category of chats to be returned.
38+
39+
limit (``int``, *optional*):
40+
The maximum number of chats to be returned.
41+
By default, no limit is applied and all chats are returned.
42+
43+
Returns:
44+
``Generator``: A generator yielding :obj:`~pyrogram.types.Chat` objects.
45+
46+
Example:
47+
.. code-block:: python
48+
49+
# Iterate through all top chats in the "users" category
50+
async for chat in app.get_top_chats(enums.TopChatCategory.USERS):
51+
print(chat.full_name)
52+
"""
53+
current = 0
54+
total = limit or (1 << 31) - 1
55+
limit = min(30, total)
56+
57+
offset = 0
58+
59+
while True:
60+
r = await self.invoke(
61+
raw.functions.contacts.GetTopPeers(
62+
offset=offset,
63+
limit=limit,
64+
hash=0,
65+
correspondents=category == enums.TopChatCategory.USERS,
66+
bots_pm=category == enums.TopChatCategory.BOTS,
67+
bots_inline=category == enums.TopChatCategory.INLINE_BOTS,
68+
phone_calls=category == enums.TopChatCategory.CALLS,
69+
forward_users=category == enums.TopChatCategory.FORWARD_CHATS,
70+
forward_chats=category == enums.TopChatCategory.FORWARD_CHATS,
71+
groups=category == enums.TopChatCategory.GROUPS,
72+
channels=category == enums.TopChatCategory.CHANNELS,
73+
bots_app=category == enums.TopChatCategory.WEB_APP_BOTS,
74+
bots_guestchat=category == enums.TopChatCategory.GUEST_BOTS,
75+
),
76+
sleep_threshold=60
77+
)
78+
79+
if not isinstance(r, raw.types.contacts.TopPeers):
80+
return
81+
82+
users = {i.id: i for i in r.users}
83+
chats = {i.id: i for i in r.chats}
84+
85+
chats = []
86+
87+
for cat in r.categories:
88+
for top_peer in cat.peers:
89+
peer_id = utils.get_raw_peer_id(top_peer.peer)
90+
91+
chats.append(types.Chat._parse_chat(self, users.get(peer_id) or chats.get(peer_id)))
92+
93+
if not chats:
94+
return
95+
96+
offset += len(chats)
97+
98+
for chat in chats:
99+
yield chat
100+
101+
current += 1
102+
103+
if current >= total:
104+
return

0 commit comments

Comments
 (0)