Skip to content

Commit

Permalink
Bot API 7.0 Reactions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe authored Jan 1, 2024
1 parent 51a42e8 commit d621859
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 107 deletions.
4 changes: 2 additions & 2 deletions pyrogram/methods/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
from .send_message import SendMessage
from .send_photo import SendPhoto
from .send_poll import SendPoll
from .send_reaction import SendReaction
from .set_message_reaction import SetMessageReaction
from .send_sticker import SendSticker
from .send_venue import SendVenue
from .send_video import SendVideo
Expand Down Expand Up @@ -110,7 +110,7 @@ class Messages(
SearchMessagesCount,
SearchGlobalCount,
GetDiscussionMessage,
SendReaction,
SetMessageReaction,
GetDiscussionReplies,
GetDiscussionRepliesCount,
StreamMedia,
Expand Down
90 changes: 0 additions & 90 deletions pyrogram/methods/messages/send_reaction.py

This file was deleted.

88 changes: 88 additions & 0 deletions pyrogram/methods/messages/set_message_reaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

from typing import Union
from typing import List

import pyrogram
from pyrogram import raw, types


class SetMessageReaction:
async def set_message_reaction(
self: "pyrogram.Client",
chat_id: Union[int, str],
message_id: int = None,
reaction: List["types.ReactionType"] = [],
is_big: bool = False
) -> "types.MessageReactions":
"""Use this method to change the chosen reactions on a message.
Service messages can't be reacted to.
Automatically forwarded messages from
a channel to its discussion group have the
same available reactions as messages in the channel.
.. include:: /_includes/usable-by/users-bots.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
message_id (``int``):
Identifier of the target message. If the message belongs to a media group, the reaction is set to the first non-deleted message in the group instead.
reaction (List of :obj:`~pyrogram.types.ReactionType` *optional*):
New list of reaction types to set on the message.
Pass None as emoji (default) to retract the reaction.
is_big (``bool``, *optional*):
Pass True to set the reaction with a big animation.
Defaults to False.
Returns:
:obj: `~pyrogram.types.MessageReactions`: On success, True is returned.
Example:
.. code-block:: python
# Send a reaction as a bot
await app.set_message_reaction(chat_id, message_id, [ReactionTypeEmoji(emoji="👍")])
# Send multiple reaction as a premium user
await app.set_message_reaction(chat_id, message_id, [ReactionTypeEmoji(emoji="👍"),ReactionTypeEmoji(emoji="😍")],True)
# Retract a reaction
await app.set_message_reaction(chat_id, message_id=message_id)
"""
if message_id is not None:
r = await self.invoke(
raw.functions.messages.SendReaction(
peer=await self.resolve_peer(chat_id),
msg_id=message_id,
reaction=[
r.write(self)
for r in reaction
] if reaction else [raw.types.ReactionEmpty()],
big=is_big
)
)
for i in r.updates:
if isinstance(i, raw.types.UpdateMessageReactions):
return types.MessageReactions._parse(self, i.reactions)
else:
raise ValueError("You need to pass one of message_id!")
5 changes: 3 additions & 2 deletions pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .photo import Photo
from .poll import Poll
from .poll_option import PollOption
from .reaction import Reaction
from .reaction import Reaction, ReactionType, ReactionTypeEmoji, ReactionTypeCustomEmoji
from .sticker import Sticker
from .stripped_thumbnail import StrippedThumbnail
from .thumbnail import Thumbnail
Expand All @@ -43,5 +43,6 @@
__all__ = [
"Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage", "Dice",
"Reaction", "WebAppData", "MessageReactions"
"Reaction", "WebAppData", "MessageReactions",
"ReactionType", "ReactionTypeEmoji", "ReactionTypeCustomEmoji"
]
30 changes: 17 additions & 13 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3353,17 +3353,21 @@ async def click(self, x: Union[int, str] = 0, y: int = None, quote: bool = None,
else:
await self.reply(button, quote=quote)

async def react(self, emoji: str = "", big: bool = False) -> bool:
async def react(
self,
reaction: List["types.ReactionType"] = [],
is_big: bool = False
) -> "types.MessageReactions":
"""Bound method *react* of :obj:`~pyrogram.types.Message`.
Use as a shortcut for:
.. code-block:: python
await client.send_reaction(
await client.set_message_reaction(
chat_id=chat_id,
message_id=message.id,
emoji="🔥"
reaction=[ReactionTypeEmoji(emoji="👍")]
)
Example:
Expand All @@ -3372,26 +3376,26 @@ async def react(self, emoji: str = "", big: bool = False) -> bool:
await message.react(emoji="🔥")
Parameters:
emoji (``str``, *optional*):
Reaction emoji.
Pass "" as emoji (default) to retract the reaction.
big (``bool``, *optional*):
Pass True to show a bigger and longer reaction.
reaction (List of :obj:`~pyrogram.types.ReactionType` *optional*):
New list of reaction types to set on the message.
Pass None as emoji (default) to retract the reaction.
is_big (``bool``, *optional*):
Pass True to set the reaction with a big animation.
Defaults to False.
Returns:
``bool``: On success, True is returned.
:obj: `~pyrogram.types.MessageReactions`: On success, True is returned.
Raises:
RPCError: In case of a Telegram RPC error.
"""

return await self._client.send_reaction(
return await self._client.set_message_reaction(
chat_id=self.chat.id,
message_id=self.id,
emoji=emoji,
big=big
reaction=reaction,
is_big=is_big
)

async def retract_vote(
Expand Down
56 changes: 56 additions & 0 deletions pyrogram/types/messages_and_media/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,59 @@ def _parse_count(
reaction.chosen_order = reaction_count.chosen_order

return reaction


class ReactionType(Object):
def __init__(
self,
*,
type: str = None,
emoji: str = None,
custom_emoji_id: str = None
):
super().__init__()
self.type = type
self.emoji = emoji
self.custom_emoji_id = custom_emoji_id


def write(self, client: "pyrogram.Client"):
raise NotImplementedError


class ReactionTypeEmoji(ReactionType):
"""The reaction is based on an emoji.
"""

def __init__(
self,
*,
emoji: str = None
):
super().__init__(
type="emoji",
emoji=emoji
)

def write(self, client: "pyrogram.Client") -> "raw.base.Reaction":
return raw.types.ReactionEmoji(
emoticon=self.emoji
)


class ReactionTypeCustomEmoji(ReactionType):

def __init__(
self,
*,
custom_emoji_id: str = None
):
super().__init__(
type="custom_emoji",
custom_emoji_id=custom_emoji_id
)

def write(self, client: "pyrogram.Client") -> "raw.base.Reaction":
return raw.types.ReactionCustomEmoji(
document_id=self.custom_emoji_id
)

0 comments on commit d621859

Please sign in to comment.