From a425e00a96a9f27f7ad9218cbeab5ed7a1a8c120 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 4 Jun 2019 19:16:30 +0200 Subject: [PATCH] Add read_history method --- docs/source/api/methods.rst | 2 + pyrogram/client/methods/messages/__init__.py | 4 +- .../client/methods/messages/read_history.py | 65 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 pyrogram/client/methods/messages/read_history.py diff --git a/docs/source/api/methods.rst b/docs/source/api/methods.rst index b9fa180c..ed150e4c 100644 --- a/docs/source/api/methods.rst +++ b/docs/source/api/methods.rst @@ -67,6 +67,7 @@ Messages - :meth:`~Client.get_messages` - :meth:`~Client.get_history` - :meth:`~Client.get_history_count` + - :meth:`~Client.read_history` - :meth:`~Client.iter_history` - :meth:`~Client.send_poll` - :meth:`~Client.vote_poll` @@ -200,6 +201,7 @@ Details .. automethod:: Client.get_messages() .. automethod:: Client.get_history() .. automethod:: Client.get_history_count() +.. automethod:: Client.read_history() .. automethod:: Client.iter_history() .. automethod:: Client.send_poll() .. automethod:: Client.vote_poll() diff --git a/pyrogram/client/methods/messages/__init__.py b/pyrogram/client/methods/messages/__init__.py index 9ed6e33b..07df7a64 100644 --- a/pyrogram/client/methods/messages/__init__.py +++ b/pyrogram/client/methods/messages/__init__.py @@ -27,6 +27,7 @@ from .get_history import GetHistory from .get_history_count import GetHistoryCount from .get_messages import GetMessages from .iter_history import IterHistory +from .read_history import ReadHistory from .retract_vote import RetractVote from .send_animated_sticker import SendAnimatedSticker from .send_animation import SendAnimation @@ -80,6 +81,7 @@ class Messages( IterHistory, SendCachedMedia, GetHistoryCount, - SendAnimatedSticker + SendAnimatedSticker, + ReadHistory ): pass diff --git a/pyrogram/client/methods/messages/read_history.py b/pyrogram/client/methods/messages/read_history.py new file mode 100644 index 00000000..f0278e91 --- /dev/null +++ b/pyrogram/client/methods/messages/read_history.py @@ -0,0 +1,65 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram 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. +# +# Pyrogram 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 Pyrogram. If not, see . + +from typing import Union + +from pyrogram.api import functions, types +from ...ext import BaseClient + + +class ReadHistory(BaseClient): + def read_history( + self, + chat_id: Union[int, str], + max_id: int = 0 + ) -> bool: + """Mark a chat's message history as read. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + For your personal cloud (Saved Messages) you can simply use "me" or "self". + For a contact that exists in your Telegram address book you can use his phone number (str). + + max_id (``int``, *optional*): + The id of the last message you want to mark as read; all the messages before this one will be marked as + read as well. Defaults to 0 (mark every unread message as read). + + Returns: + ``bool`` - On success, True is returned. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + + peer = self.resolve_peer(chat_id) + + if isinstance(peer, types.InputPeerChannel): + q = functions.channels.ReadHistory( + channel=peer, + max_id=max_id + ) + else: + q = functions.messages.ReadHistory( + peer=peer, + max_id=max_id + ) + + self.send(q) + + return True