Skip to content

Commit

Permalink
Merge pull request #43 from yasirarism/reactions
Browse files Browse the repository at this point in the history
Pyrofork: Message Reaction Update
  • Loading branch information
wulan17 authored Jan 22, 2024
2 parents 1258c47 + 87d4bff commit f662d33
Show file tree
Hide file tree
Showing 18 changed files with 658 additions and 39 deletions.
4 changes: 4 additions & 0 deletions docs/source/api/decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Index
- :meth:`~Client.on_message`
- :meth:`~Client.on_edited_message`
- :meth:`~Client.on_callback_query`
- :meth:`~Client.on_message_reaction_updated`
- :meth:`~Client.on_message_reaction_count_updated`
- :meth:`~Client.on_inline_query`
- :meth:`~Client.on_chosen_inline_result`
- :meth:`~Client.on_chat_member_updated`
Expand All @@ -58,6 +60,8 @@ Details
.. autodecorator:: pyrogram.Client.on_message()
.. autodecorator:: pyrogram.Client.on_edited_message()
.. autodecorator:: pyrogram.Client.on_callback_query()
.. autodecorator:: pyrogram.Client.on_message_reaction_updated()
.. autodecorator:: pyrogram.Client.on_message_reaction_count_updated()
.. autodecorator:: pyrogram.Client.on_inline_query()
.. autodecorator:: pyrogram.Client.on_chosen_inline_result()
.. autodecorator:: pyrogram.Client.on_chat_member_updated()
Expand Down
4 changes: 4 additions & 0 deletions docs/source/api/handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Index
- :class:`EditedMessageHandler`
- :class:`DeletedMessagesHandler`
- :class:`CallbackQueryHandler`
- :class:`MessageReactionUpdatedHandler`
- :class:`MessageReactionCountUpdatedHandler`
- :class:`InlineQueryHandler`
- :class:`ChosenInlineResultHandler`
- :class:`ChatMemberUpdatedHandler`
Expand All @@ -58,6 +60,8 @@ Details
.. autoclass:: EditedMessageHandler()
.. autoclass:: DeletedMessagesHandler()
.. autoclass:: CallbackQueryHandler()
.. autoclass:: MessageReactionUpdatedHandler()
.. autoclass:: MessageReactionCountUpdatedHandler()
.. autoclass:: InlineQueryHandler()
.. autoclass:: ChosenInlineResultHandler()
.. autoclass:: ChatMemberUpdatedHandler()
Expand Down
26 changes: 21 additions & 5 deletions pyrogram/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import pyrogram
from pyrogram import utils
from pyrogram.handlers import (
CallbackQueryHandler, MessageHandler, EditedMessageHandler, DeletedMessagesHandler,
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler, ConversationHandler,
ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler, StoryHandler
CallbackQueryHandler, MessageHandler, EditedMessageHandler, DeletedMessagesHandler, MessageReactionUpdatedHandler, MessageReactionCountUpdatedHandler, UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler, ConversationHandler, ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler, StoryHandler
)
from pyrogram.raw.types import (
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
Expand All @@ -36,7 +34,9 @@
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant,
UpdateBotChatInviteRequester, UpdateStory
UpdateBotChatInviteRequester, UpdateStory,
UpdateBotMessageReaction,
UpdateBotMessageReactions
)

log = logging.getLogger(__name__)
Expand All @@ -54,6 +54,8 @@ class Dispatcher:
CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,)
CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,)
NEW_STORY_UPDATES = (UpdateStory,)
MESSAGE_BOT_NA_REACTION_UPDATES = (UpdateBotMessageReaction,)
MESSAGE_BOT_A_REACTION_UPDATES = (UpdateBotMessageReactions,)

def __init__(self, client: "pyrogram.Client"):
self.client = client
Expand Down Expand Up @@ -137,6 +139,18 @@ async def story_parser(update, users, chats):
await pyrogram.types.Story._parse(self.client, update.story, update.peer),
StoryHandler
)

async def message_bot_na_reaction_parser(update, users, chats):
return (
pyrogram.types.MessageReactionUpdated._parse(self.client, update, users, chats),
MessageReactionUpdatedHandler
)

async def message_bot_a_reaction_parser(update, users, chats):
return (
pyrogram.types.MessageReactionCountUpdated._parse(self.client, update, users, chats),
MessageReactionCountUpdatedHandler
)

self.update_parsers = {
Dispatcher.NEW_MESSAGE_UPDATES: message_parser,
Expand All @@ -149,7 +163,9 @@ async def story_parser(update, users, chats):
Dispatcher.CHOSEN_INLINE_RESULT_UPDATES: chosen_inline_result_parser,
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser,
Dispatcher.CHAT_JOIN_REQUEST_UPDATES: chat_join_request_parser,
Dispatcher.NEW_STORY_UPDATES: story_parser
Dispatcher.NEW_STORY_UPDATES: story_parser,
Dispatcher.MESSAGE_BOT_NA_REACTION_UPDATES: message_bot_na_reaction_parser,
Dispatcher.MESSAGE_BOT_A_REACTION_UPDATES: message_bot_a_reaction_parser
}

self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
from .raw_update_handler import RawUpdateHandler
from .user_status_handler import UserStatusHandler
from .story_handler import StoryHandler
from .message_reaction_updated_handler import MessageReactionUpdatedHandler
from .message_reaction_count_updated_handler import MessageReactionCountUpdatedHandler
51 changes: 51 additions & 0 deletions pyrogram/handlers/message_reaction_count_updated_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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 Callable

from .handler import Handler


class MessageReactionCountUpdatedHandler(Handler):
"""The MessageReactionCountUpdated handler class.
Used to handle changes in the anonymous reaction of a message.
It is intended to be used with :meth:`~pyrogram.Client.add_handler`.
For a nicer way to register this handler, have a look at the
:meth:`~pyrogram.Client.on_message_reaction_count_updated` decorator.
Parameters:
callback (``Callable``):
Pass a function that will be called when a new MessageReactionCountUpdated event arrives. It takes
*(client, message_reaction_count_updated)* as positional arguments (look at the section below for a detailed
description).
filters (:obj:`Filters`):
Pass one or more filters to allow only a subset of updates to be passed in your callback function.
Other parameters:
client (:obj:`~pyrogram.Client`):
The Client itself, useful when you want to call other API methods inside the handler.
message_reaction_count_updated (:obj:`~pyrogram.types.MessageReactionCountUpdated`):
The received message reaction count update.
"""

def __init__(self, callback: Callable, filters=None):
super().__init__(callback, filters)
51 changes: 51 additions & 0 deletions pyrogram/handlers/message_reaction_updated_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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 Callable

from .handler import Handler


class MessageReactionUpdatedHandler(Handler):
"""The MessageReactionUpdated handler class.
Used to handle changes in the reaction of a message.
It is intended to be used with :meth:`~pyrogram.Client.add_handler`.
For a nicer way to register this handler, have a look at the
:meth:`~pyrogram.Client.on_message_reaction_updated` decorator.
Parameters:
callback (``Callable``):
Pass a function that will be called when a new MessageReactionUpdated event arrives. It takes
*(client, message_reaction_updated)* as positional arguments (look at the section below for a detailed
description).
filters (:obj:`Filters`):
Pass one or more filters to allow only a subset of updates to be passed in your callback function.
Other parameters:
client (:obj:`~pyrogram.Client`):
The Client itself, useful when you want to call other API methods inside the handler.
message_reaction_updated (:obj:`~pyrogram.types.MessageReactionUpdated`):
The received message reaction update.
"""

def __init__(self, callback: Callable, filters=None):
super().__init__(callback, filters)
6 changes: 5 additions & 1 deletion pyrogram/methods/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from .on_raw_update import OnRawUpdate
from .on_user_status import OnUserStatus
from .on_story import OnStory
from .on_message_reaction_updated import OnMessageReactionUpdated
from .on_message_reaction_count_updated import OnMessageReactionCountUpdated


class Decorators(
Expand All @@ -45,6 +47,8 @@ class Decorators(
OnChosenInlineResult,
OnChatMemberUpdated,
OnChatJoinRequest,
OnStory
OnStory,
OnMessageReactionUpdated,
OnMessageReactionCountUpdated
):
pass
60 changes: 60 additions & 0 deletions pyrogram/methods/decorators/on_message_reaction_count_updated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 Callable

import pyrogram
from pyrogram.filters import Filter


class OnMessageReactionCountUpdated:
def on_message_reaction_count_updated(
self=None,
filters=None,
group: int = 0
) -> Callable:
"""Decorator for handling anonymous reaction changes on messages.
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
:obj:`~pyrogram.handlers.MessageReactionCountUpdatedHandler`.
Parameters:
filters (:obj:`~pyrogram.filters`, *optional*):
Pass one or more filters to allow only a subset of updates to be passed in your function.
group (``int``, *optional*):
The group identifier, defaults to 0.
"""

def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.MessageReactionCountUpdatedHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
if not hasattr(func, "handlers"):
func.handlers = []

func.handlers.append(
(
pyrogram.handlers.MessageReactionCountUpdatedHandler(func, self),
group if filters is None else filters
)
)

return func

return decorator
60 changes: 60 additions & 0 deletions pyrogram/methods/decorators/on_message_reaction_updated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 Callable

import pyrogram
from pyrogram.filters import Filter


class OnMessageReactionUpdated:
def on_message_reaction_updated(
self=None,
filters=None,
group: int = 0
) -> Callable:
"""Decorator for handling reaction changes on messages.
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
:obj:`~pyrogram.handlers.MessageReactionUpdatedHandler`.
Parameters:
filters (:obj:`~pyrogram.filters`, *optional*):
Pass one or more filters to allow only a subset of updates to be passed in your function.
group (``int``, *optional*):
The group identifier, defaults to 0.
"""

def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.MessageReactionUpdatedHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
if not hasattr(func, "handlers"):
func.handlers = []

func.handlers.append(
(
pyrogram.handlers.MessageReactionUpdatedHandler(func, self),
group if filters is None else filters
)
)

return func

return decorator
Loading

0 comments on commit f662d33

Please sign in to comment.