|
| 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