Skip to content

Commit 6daa501

Browse files
committed
Add a friendly interface for getting chat event logs
Add get_chat_event_log method Add ChatEvent and ChatEventFilter types
1 parent b5c3912 commit 6daa501

6 files changed

Lines changed: 835 additions & 4 deletions

File tree

compiler/docs/compiler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def get_title_list(s: str) -> list:
221221
delete_user_history
222222
set_slow_mode
223223
mark_chat_unread
224+
get_chat_event_log
224225
""",
225226
users="""
226227
Users
@@ -245,7 +246,7 @@ def get_title_list(s: str) -> list:
245246
delete_contacts
246247
""",
247248
password="""
248-
Pssword
249+
Password
249250
enable_cloud_password
250251
change_cloud_password
251252
remove_cloud_password
@@ -331,6 +332,8 @@ def get_title_list(s: str) -> list:
331332
ChatPhoto
332333
ChatMember
333334
ChatPermissions
335+
ChatEvent
336+
ChatEventFilter
334337
Dialog
335338
Restriction
336339
""",

pyrogram/methods/chats/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .delete_user_history import DeleteUserHistory
2828
from .export_chat_invite_link import ExportChatInviteLink
2929
from .get_chat import GetChat
30+
from .get_chat_event_log import GetChatEventLog
3031
from .get_chat_member import GetChatMember
3132
from .get_chat_members import GetChatMembers
3233
from .get_chat_members_count import GetChatMembersCount
@@ -38,6 +39,7 @@
3839
from .join_chat import JoinChat
3940
from .kick_chat_member import KickChatMember
4041
from .leave_chat import LeaveChat
42+
from .mark_chat_unread import MarkChatUnread
4143
from .pin_chat_message import PinChatMessage
4244
from .promote_chat_member import PromoteChatMember
4345
from .restrict_chat_member import RestrictChatMember
@@ -52,7 +54,6 @@
5254
from .unpin_all_chat_messages import UnpinAllChatMessages
5355
from .unpin_chat_message import UnpinChatMessage
5456
from .update_chat_username import UpdateChatUsername
55-
from .mark_chat_unread import MarkChatUnread
5657

5758

5859
class Chats(
@@ -92,6 +93,7 @@ class Chats(
9293
SetSlowMode,
9394
DeleteUserHistory,
9495
UnpinAllChatMessages,
95-
MarkChatUnread
96+
MarkChatUnread,
97+
GetChatEventLog
9698
):
9799
pass
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 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 Union, List, AsyncGenerator, Optional
20+
21+
from pyrogram import raw
22+
from pyrogram import types
23+
from pyrogram.scaffold import Scaffold
24+
25+
26+
class GetChatEventLog(Scaffold):
27+
async def get_chat_event_log(
28+
self,
29+
chat_id: Union[int, str],
30+
query: str = "",
31+
offset_id: int = 0,
32+
limit: int = 0,
33+
filters: "types.ChatEventFilter" = None,
34+
user_ids: List[Union[int, str]] = None
35+
) -> Optional[AsyncGenerator["types.ChatEvent", None]]:
36+
"""Get the actions taken by chat members and administrators in the last 48h.
37+
38+
Only available for supergroups and channels. Requires administrator rights.
39+
Results are returned in reverse chronological order (i.e., newest first).
40+
41+
Args:
42+
chat_id (``int`` | ``str``):
43+
Unique identifier (int) or username (str) of the target chat.
44+
45+
query (``str``, *optional*):
46+
Search query to filter events based on text.
47+
By default, an empty query is applied and all events will be returned.
48+
49+
offset_id (``int``, *optional*):
50+
Offset event identifier from which to start returning results.
51+
By default, no offset is applied and events will be returned starting from the latest.
52+
53+
limit (``int``, *optional*):
54+
Maximum amount of events to be returned.
55+
By default, all events will be returned.
56+
57+
filters (:obj:`~pyrogram.types.ChatEventFilter`, *optional*):
58+
The types of events to return.
59+
By default, all types will be returned.
60+
61+
user_ids (List of ``int`` | ``str``, *optional*):
62+
User identifiers (int) or usernames (str) by which to filter events.
63+
By default, events relating to all users will be returned.
64+
65+
Yields:
66+
:obj:`~pyrogram.types.ChatEvent` objects.
67+
"""
68+
current = 0
69+
total = abs(limit) or (1 << 31)
70+
limit = min(100, total)
71+
72+
while True:
73+
r: raw.base.channels.AdminLogResults = await self.send(
74+
raw.functions.channels.GetAdminLog(
75+
channel=await self.resolve_peer(chat_id),
76+
q=query,
77+
min_id=0,
78+
max_id=offset_id,
79+
limit=limit,
80+
events_filter=filters.write(),
81+
admins=(
82+
[await self.resolve_peer(i) for i in user_ids]
83+
if user_ids is not None
84+
else user_ids
85+
)
86+
)
87+
)
88+
89+
if not r.events:
90+
return
91+
92+
last = r.events[-1]
93+
offset_id = last.id
94+
95+
for event in r.events:
96+
yield await types.ChatEvent._parse(self, event, r.users, r.chats)
97+
98+
current += 1
99+
100+
if current >= total:
101+
return

pyrogram/types/user_and_chats/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from .chat import Chat
20+
from .chat_event import ChatEvent
2021
from .chat_member import ChatMember
2122
from .chat_permissions import ChatPermissions
2223
from .chat_photo import ChatPhoto
2324
from .chat_preview import ChatPreview
2425
from .dialog import Dialog
2526
from .restriction import Restriction
2627
from .user import User
28+
from .chat_event_filter import ChatEventFilter
2729

2830
__all__ = [
29-
"Chat", "ChatMember", "ChatPermissions", "ChatPhoto", "ChatPreview", "Dialog", "User", "Restriction"
31+
"Chat", "ChatMember", "ChatPermissions", "ChatPhoto", "ChatPreview", "Dialog", "User", "Restriction", "ChatEvent",
32+
"ChatEventFilter"
3033
]

0 commit comments

Comments
 (0)