Skip to content

Commit a2f79e0

Browse files
Add delete_chat_history
Co-authored-by: KurimuzonAkuma <[email protected]>
1 parent 122b3ea commit a2f79e0

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

docs/source/releases/changes-in-this-fork.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Changes in this Fork
3333
| Scheme layer used: 200 |
3434
+------------------------+
3535

36+
- Added :meth:`~pyrogram.Client.delete_chat_history`.
3637
- Rename ``UserGift`` to :obj:`~pyrogram.types.ReceivedGift`, ``get_user_gifts`` to :meth:`~pyrogram.Client.get_received_gifts` and the corresponding fields appropriately.
3738
- Added the field ``paid_message_star_count`` to the classes :obj:`~pyrogram.types.Chat`, :obj:`~pyrogram.types.Message` and :obj:`~pyrogram.types.User`.
3839
- Added the parameter ``paid_message_star_count`` to the methods :meth:`~pyrogram.Client.copy_media_group`, :meth:`~pyrogram.Client.send_game`, :meth:`~pyrogram.Client.send_invoice`, :meth:`~pyrogram.Client.forward_messages`, :meth:`~pyrogram.Client.send_animation`, :meth:`~pyrogram.Client.send_audio`, :meth:`~pyrogram.Client.send_cached_media`, :meth:`~pyrogram.Client.send_contact`, :meth:`~pyrogram.Client.send_dice`, :meth:`~pyrogram.Client.send_document`, :meth:`~pyrogram.Client.send_location`, :meth:`~pyrogram.Client.send_media_group`, :meth:`~pyrogram.Client.send_message`, :meth:`~pyrogram.Client.send_paid_media`, :meth:`~pyrogram.Client.send_photo`, :meth:`~pyrogram.Client.send_poll`, :meth:`~pyrogram.Client.send_sticker`, :meth:`~pyrogram.Client.send_venue`, :meth:`~pyrogram.Client.send_video_note`, :meth:`~pyrogram.Client.send_video`, :meth:`~pyrogram.Client.send_voice` and the bound methods :meth:`~pyrogram.types.Message.forward` and :meth:`~pyrogram.types.Message.copy`.

pyrogram/methods/messages/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from .copy_media_group import CopyMediaGroup
2020
from .copy_message import CopyMessage
21+
from .delete_chat_history import DeleteChatHistory
2122
from .delete_messages import DeleteMessages
2223
from .download_media import DownloadMedia
2324
from .edit_cached_media import EditCachedMedia
@@ -76,6 +77,7 @@
7677
class Messages(
7778
CopyMediaGroup,
7879
CopyMessage,
80+
DeleteChatHistory,
7981
DeleteMessages,
8082
DownloadMedia,
8183
EditCachedMedia,
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present <https://github.com/TelegramPlayGround>
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 datetime import datetime
20+
import logging
21+
from typing import Optional, Union
22+
23+
import pyrogram
24+
from pyrogram import raw, utils
25+
26+
log = logging.getLogger(__name__)
27+
28+
29+
class DeleteChatHistory:
30+
async def delete_chat_history(
31+
self: "pyrogram.Client",
32+
chat_id: Union[int, str],
33+
max_id: Optional[int] = 0,
34+
# TODO
35+
revoke: Optional[bool] = None,
36+
just_clear: Optional[bool] = None,
37+
min_date: Optional[datetime] = None,
38+
max_date: Optional[datetime] = None
39+
) -> int:
40+
"""Deletes all messages in the chat. For group chats this will release the usernames and remove all members.
41+
42+
.. include:: /_includes/usable-by/users.rst
43+
44+
Parameters:
45+
chat_id (``int`` | ``str``):
46+
Unique identifier (int) or username (str) of the target chat.
47+
48+
max_id (``int``, *optional*):
49+
Maximum ID of message to delete.
50+
Defaults to 0.
51+
52+
revoke (``bool``, *optional*):
53+
Pass True to delete messages for all chat members.
54+
Always True if using in :obj:`~pyrogram.enums.ChatType.CHANNEL` and :obj:`~pyrogram.enums.ChatType.SUPERGROUP` chats.
55+
56+
just_clear (``bool``, *optional*):
57+
Pass True to clear history for the current user, without actually removing chat.
58+
For :obj:`~pyrogram.enums.ChatType.PRIVATE` and :obj:`~pyrogram.enums.ChatType.GROUP` chats only.
59+
60+
min_date (:py:obj:`~datetime.datetime`, *optional*):
61+
The minimum date of the messages to delete.
62+
Delete all messages newer than this time.
63+
For :obj:`~pyrogram.enums.ChatType.PRIVATE` and :obj:`~pyrogram.enums.ChatType.GROUP` chats only.
64+
65+
max_date (:py:obj:`~datetime.datetime`, *optional*):
66+
The maximum date of the messages to delete.
67+
Delete all messages older than this time.
68+
For :obj:`~pyrogram.enums.ChatType.PRIVATE` and :obj:`~pyrogram.enums.ChatType.GROUP` chats only.
69+
70+
Returns:
71+
``int``: Amount of affected messages
72+
73+
Example:
74+
.. code-block:: python
75+
76+
# Delete all messages in channel
77+
await app.delete_chat_history(chat_id, revoke=True)
78+
79+
"""
80+
peer = await self.resolve_peer(chat_id)
81+
82+
if isinstance(peer, raw.types.InputPeerChannel):
83+
r = await self.invoke(
84+
raw.functions.channels.DeleteHistory(
85+
channel=raw.types.InputChannel(
86+
channel_id=peer.channel_id,
87+
access_hash=peer.access_hash
88+
),
89+
max_id=max_id,
90+
for_everyone=revoke
91+
)
92+
)
93+
else:
94+
r = await self.invoke(
95+
raw.functions.messages.DeleteHistory(
96+
peer=peer,
97+
max_id=max_id,
98+
just_clear=just_clear,
99+
revoke=revoke,
100+
min_date=utils.datetime_to_timestamp(min_date),
101+
max_date=utils.datetime_to_timestamp(max_date)
102+
)
103+
)
104+
105+
return len(r.updates[0].messages) if isinstance(peer, raw.types.InputPeerChannel) else r.pts_count

0 commit comments

Comments
 (0)