From 53ab40fef93355c54c7ffd749a94fe69660793a9 Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Sun, 5 May 2024 19:47:04 +0300 Subject: [PATCH] Add increment_message_views method --- compiler/docs/compiler.py | 3 + pyrogram/methods/messages/__init__.py | 2 + .../messages/increment_message_views.py | 61 +++++++++++++++++++ pyrogram/types/messages_and_media/message.py | 56 +++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 pyrogram/methods/messages/increment_message_views.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 610f640b..eabd7fdd 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -178,6 +178,7 @@ def pyrogram_api(): get_messages get_scheduled_messages get_stickers + increment_message_views get_media_group get_chat_history get_chat_history_count @@ -651,6 +652,8 @@ def pyrogram_api(): Message.reply_web_page Message.get_media_group Message.react + Message.read + Message.view """, chat=""" Chat diff --git a/pyrogram/methods/messages/__init__.py b/pyrogram/methods/messages/__init__.py index d7b7c499..2019dfdb 100644 --- a/pyrogram/methods/messages/__init__.py +++ b/pyrogram/methods/messages/__init__.py @@ -39,6 +39,7 @@ from .get_media_group import GetMediaGroup from .get_messages import GetMessages from .get_scheduled_messages import GetScheduledMessages from .get_stickers import GetStickers +from .increment_message_views import IncrementMessageViews from .read_chat_history import ReadChatHistory from .read_mentions import ReadMentions from .read_reactions import ReadReactions @@ -107,6 +108,7 @@ class Messages( GetChatHistory, SendCachedMedia, GetChatHistoryCount, + IncrementMessageViews, ReadChatHistory, ReadMentions, ReadReactions, diff --git a/pyrogram/methods/messages/increment_message_views.py b/pyrogram/methods/messages/increment_message_views.py new file mode 100644 index 00000000..4d8734e5 --- /dev/null +++ b/pyrogram/methods/messages/increment_message_views.py @@ -0,0 +1,61 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# 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, List + +import pyrogram +from pyrogram import raw + + +class IncrementMessageViews: + async def increment_message_views( + self: "pyrogram.Client", + chat_id: Union[int, str], + message_id: Union[int, List[int]], + ) -> bool: + """Increment message views. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + message_id (``int`` | List of ``int``): + Identifier or list of message identifiers of the target message. + + Returns: + ``bool``: On success, True is returned. + + Example: + .. code-block:: python + + # Increment message views + await app.increment_message_views(chat_id, 1) + """ + ids = [message_id] if not isinstance(message_id, list) else message_id + + r = await self.invoke( + raw.functions.messages.GetMessagesViews( + peer=await self.resolve_peer(chat_id), + id=ids, + increment=True + ) + ) + + return bool(r) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 41c2af0c..7f55ad2f 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -4664,3 +4664,59 @@ class Message(Object, Update): chat_id=self.chat.id, message_id=self.id ) + + async def read(self) -> bool: + """Bound method *read* of :obj:`~pyrogram.types.Message`. + + Use as a shortcut for: + + .. code-block:: python + + await client.read_chat_history( + chat_id=message.chat.id, + max_id=message_id + ) + + Example: + .. code-block:: python + + await message.read() + + Returns: + True on success. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + return await self._client.read_chat_history( + chat_id=self.chat.id, + max_id=self.id + ) + + async def view(self) -> bool: + """Bound method *view* of :obj:`~pyrogram.types.Message`. + + Use as a shortcut for: + + .. code-block:: python + + await client.increment_message_views( + chat_id=message.chat.id, + message_id=message_id + ) + + Example: + .. code-block:: python + + await message.view() + + Returns: + True on success. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + return await self._client.increment_message_views( + chat_id=self.chat.id, + message_id=self.id + )