From c655607bc1fc3096d75cbf0cea1cd14cb1dec097 Mon Sep 17 00:00:00 2001 From: wulan17 Date: Fri, 29 Sep 2023 00:55:30 +0700 Subject: [PATCH] Pyrofork: Add StoryHandler Signed-off-by: wulan17 --- docs/source/api/decorators.rst | 2 + docs/source/api/handlers.rst | 2 + pyrogram/dispatcher.py | 25 ++++++---- pyrogram/handlers/__init__.py | 12 +++-- pyrogram/handlers/story_handler.py | 49 +++++++++++++++++++ pyrogram/methods/decorators/__init__.py | 15 +++--- pyrogram/methods/decorators/on_story.py | 62 +++++++++++++++++++++++++ 7 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 pyrogram/handlers/story_handler.py create mode 100644 pyrogram/methods/decorators/on_story.py diff --git a/docs/source/api/decorators.rst b/docs/source/api/decorators.rst index 2e7a04f2..1dac109c 100644 --- a/docs/source/api/decorators.rst +++ b/docs/source/api/decorators.rst @@ -44,6 +44,7 @@ Index - :meth:`~Client.on_chat_join_request` - :meth:`~Client.on_deleted_messages` - :meth:`~Client.on_user_status` + - :meth:`~Client.on_story` - :meth:`~Client.on_poll` - :meth:`~Client.on_disconnect` - :meth:`~Client.on_raw_update` @@ -63,6 +64,7 @@ Details .. autodecorator:: pyrogram.Client.on_chat_join_request() .. autodecorator:: pyrogram.Client.on_deleted_messages() .. autodecorator:: pyrogram.Client.on_user_status() +.. autodecorator:: pyrogram.Client.on_story() .. autodecorator:: pyrogram.Client.on_poll() .. autodecorator:: pyrogram.Client.on_disconnect() .. autodecorator:: pyrogram.Client.on_raw_update() diff --git a/docs/source/api/handlers.rst b/docs/source/api/handlers.rst index 7c0ffc11..e1f32507 100644 --- a/docs/source/api/handlers.rst +++ b/docs/source/api/handlers.rst @@ -43,6 +43,7 @@ Index - :class:`ChosenInlineResultHandler` - :class:`ChatMemberUpdatedHandler` - :class:`UserStatusHandler` + - :class:`StoryHandler` - :class:`PollHandler` - :class:`DisconnectHandler` - :class:`RawUpdateHandler` @@ -61,6 +62,7 @@ Details .. autoclass:: ChosenInlineResultHandler() .. autoclass:: ChatMemberUpdatedHandler() .. autoclass:: UserStatusHandler() +.. autoclass:: StoryHandler() .. autoclass:: PollHandler() .. autoclass:: DisconnectHandler() .. autoclass:: RawUpdateHandler() diff --git a/pyrogram/dispatcher.py b/pyrogram/dispatcher.py index 6e503ceb..13e03636 100644 --- a/pyrogram/dispatcher.py +++ b/pyrogram/dispatcher.py @@ -1,20 +1,21 @@ -# Pyrogram - Telegram MTProto API Client Library for Python +# Pyrofork - Telegram MTProto API Client Library for Python # Copyright (C) 2017-present Dan +# Copyright (C) 2022-present Mayuri-Chan # -# This file is part of Pyrogram. +# This file is part of Pyrofork. # -# Pyrogram is free software: you can redistribute it and/or modify +# 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. # -# Pyrogram is distributed in the hope that it will be useful, +# 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 Pyrogram. If not, see . +# along with Pyrofork. If not, see . import asyncio import inspect @@ -26,7 +27,7 @@ from pyrogram import utils from pyrogram.handlers import ( CallbackQueryHandler, MessageHandler, EditedMessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler, - ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler + ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler, StoryHandler ) from pyrogram.raw.types import ( UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage, @@ -35,7 +36,7 @@ from pyrogram.raw.types import ( UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery, UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll, UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant, - UpdateBotChatInviteRequester + UpdateBotChatInviteRequester, UpdateStory ) log = logging.getLogger(__name__) @@ -52,6 +53,7 @@ class Dispatcher: POLL_UPDATES = (UpdateMessagePoll,) CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,) CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,) + NEW_STORY_UPDATES = (UpdateStory,) def __init__(self, client: "pyrogram.Client"): self.client = client @@ -127,6 +129,12 @@ class Dispatcher: ChatJoinRequestHandler ) + async def story_parser(update, _, __): + return ( + await pyrogram.types.Story._parse(self.client, update.story, update.peer), + StoryHandler + ) + self.update_parsers = { Dispatcher.NEW_MESSAGE_UPDATES: message_parser, Dispatcher.EDIT_MESSAGE_UPDATES: edited_message_parser, @@ -137,7 +145,8 @@ class Dispatcher: Dispatcher.POLL_UPDATES: poll_parser, 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.CHAT_JOIN_REQUEST_UPDATES: chat_join_request_parser, + Dispatcher.NEW_STORY_UPDATES: story_parser } self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple} diff --git a/pyrogram/handlers/__init__.py b/pyrogram/handlers/__init__.py index 1c762958..805ca051 100644 --- a/pyrogram/handlers/__init__.py +++ b/pyrogram/handlers/__init__.py @@ -1,20 +1,21 @@ -# Pyrogram - Telegram MTProto API Client Library for Python +# Pyrofork - Telegram MTProto API Client Library for Python # Copyright (C) 2017-present Dan +# Copyright (C) 2022-present Mayuri-Chan # -# This file is part of Pyrogram. +# This file is part of Pyrofork. # -# Pyrogram is free software: you can redistribute it and/or modify +# 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. # -# Pyrogram is distributed in the hope that it will be useful, +# 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 Pyrogram. If not, see . +# along with Pyrofork. If not, see . from .callback_query_handler import CallbackQueryHandler from .chat_join_request_handler import ChatJoinRequestHandler @@ -28,3 +29,4 @@ from .message_handler import MessageHandler from .poll_handler import PollHandler from .raw_update_handler import RawUpdateHandler from .user_status_handler import UserStatusHandler +from .story_handler import StoryHandler diff --git a/pyrogram/handlers/story_handler.py b/pyrogram/handlers/story_handler.py new file mode 100644 index 00000000..f881a89e --- /dev/null +++ b/pyrogram/handlers/story_handler.py @@ -0,0 +1,49 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2022-present 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 . + +from typing import Callable + +from .handler import Handler + + +class StoryHandler(Handler): + """The Story handler class. Used to handle new stories. + 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_story` decorator. + + Parameters: + callback (``Callable``): + Pass a function that will be called when a new Stories arrives. It takes *(client, story)* + 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 stories 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 story handler. + + story (:obj:`~pyrogram.types.Story`): + The received story. + """ + + def __init__(self, callback: Callable, filters=None): + super().__init__(callback, filters) diff --git a/pyrogram/methods/decorators/__init__.py b/pyrogram/methods/decorators/__init__.py index 1fc61185..560a13de 100644 --- a/pyrogram/methods/decorators/__init__.py +++ b/pyrogram/methods/decorators/__init__.py @@ -1,20 +1,21 @@ -# Pyrogram - Telegram MTProto API Client Library for Python +# Pyrofork - Telegram MTProto API Client Library for Python # Copyright (C) 2017-present Dan +# Copyright (C) 2022-present Mayuri-Chan # -# This file is part of Pyrogram. +# This file is part of Pyrofork. # -# Pyrogram is free software: you can redistribute it and/or modify +# 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. # -# Pyrogram is distributed in the hope that it will be useful, +# 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 Pyrogram. If not, see . +# along with Pyrofork. If not, see . from .on_callback_query import OnCallbackQuery from .on_chat_join_request import OnChatJoinRequest @@ -28,6 +29,7 @@ from .on_message import OnMessage from .on_poll import OnPoll from .on_raw_update import OnRawUpdate from .on_user_status import OnUserStatus +from .on_story import OnStory class Decorators( @@ -42,6 +44,7 @@ class Decorators( OnPoll, OnChosenInlineResult, OnChatMemberUpdated, - OnChatJoinRequest + OnChatJoinRequest, + OnStory ): pass diff --git a/pyrogram/methods/decorators/on_story.py b/pyrogram/methods/decorators/on_story.py new file mode 100644 index 00000000..6b11cf55 --- /dev/null +++ b/pyrogram/methods/decorators/on_story.py @@ -0,0 +1,62 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2022-present 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 . + + +from typing import Callable + +import pyrogram +from pyrogram.filters import Filter + + +class OnStory: + def on_story( + self=None, + filters=None, + group: int = 0 + ) -> Callable: + """Decorator for handling new stories. + + This does the same thing as :meth:`~pyrogram.Client.add_handler` using the + :obj:`~pyrogram.handlers.StoryHandler`. + + Parameters: + filters (:obj:`~pyrogram.filters`, *optional*): + Pass one or more filters to allow only a subset of stories 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.StoryHandler(func, filters), group) + elif isinstance(self, Filter) or self is None: + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.StoryHandler(func, self), + group if filters is None else filters + ) + ) + + return func + + return decorator