Skip to content

Commit

Permalink
Pyrofork: Add delete_chat_history method
Browse files Browse the repository at this point in the history
Signed-off-by: Yasir Aris <[email protected]>
  • Loading branch information
KurimuzonAkuma authored and yasirarism committed Feb 27, 2025
1 parent c1816f2 commit d731e77
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def get_title_list(s: str) -> list:
transcribe_audio
translate_message_text
start_bot
delete_chat_history
""",
chats="""
Chats
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .copy_media_group import CopyMediaGroup
from .copy_message import CopyMessage
from .delete_chat_history import DeleteChatHistory
from .delete_messages import DeleteMessages
from .delete_scheduled_messages import DeleteScheduledMessages
from .download_media import DownloadMedia
Expand Down Expand Up @@ -78,6 +79,7 @@
from .translate_text import TranslateText

class Messages(
DeleteChatHistory,
DeleteMessages,
DeleteScheduledMessages,
EditMessageCaption,
Expand Down
102 changes: 102 additions & 0 deletions pyrogram/methods/messages/delete_chat_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrofork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from datetime import datetime
import logging
from typing import Union

import pyrogram
from pyrogram import raw
from pyrogram import utils

log = logging.getLogger(__name__)


class DeleteChatHistory:
async def delete_chat_history(
self: "pyrogram.Client",
chat_id: Union[int, str],
max_id: int = 0,
revoke: bool = None,
just_clear = None,
min_date: datetime = None,
max_date: datetime = None,
) -> int:
"""Delete the history of a chat.
.. include:: /_includes/usable-by/users.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
max_id (``int``, *optional*):
Maximum ID of message to delete.
revoke (``bool``, *optional*):
Deletes messages history for everyone.
Required ``True`` if using in channel.
just_clear (``bool``, *optional*):
If True, clear history for the current user, without actually removing chat.
For private and simple group chats only.
min_date (:py:obj:`~datetime.datetime`, *optional*):
Delete all messages newer than this time.
For private and simple group chats only.
max_date (:py:obj:`~datetime.datetime`, *optional*):
Delete all messages older than this time.
For private and simple group chats only.
Returns:
``int``: Amount of affected messages
Example:
.. code-block:: python
# Delete all messages in channel
await app.delete_chat_history(chat_id, revoke=True)
"""
peer = await self.resolve_peer(chat_id)

if isinstance(peer, raw.types.InputPeerChannel):
r = await self.invoke(
raw.functions.channels.DeleteHistory(
channel=raw.types.InputChannel(
channel_id=peer.channel_id,
access_hash=peer.access_hash
),
max_id=max_id,
for_everyone=revoke
)
)
else:
r = await self.invoke(
raw.functions.messages.DeleteHistory(
peer=peer,
max_id=max_id,
just_clear=just_clear,
revoke=revoke,
min_date=utils.datetime_to_timestamp(min_date),
max_date=utils.datetime_to_timestamp(max_date)
)
)

return len(r.updates[0].messages) if isinstance(peer, raw.types.InputPeerChannel) else r.pts_count

0 comments on commit d731e77

Please sign in to comment.