diff --git a/pyrogram/dispatcher.py b/pyrogram/dispatcher.py index 6e503cebb..13e03636f 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.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 @@ 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 @@ async def chat_join_request_parser(update, users, chats): 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 @@ async def chat_join_request_parser(update, users, chats): 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 1c762958a..6b5853f46 100644 --- a/pyrogram/handlers/__init__.py +++ b/pyrogram/handlers/__init__.py @@ -28,3 +28,4 @@ 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 000000000..f881a89eb --- /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 1fc61185c..80c5f9c4b 100644 --- a/pyrogram/methods/decorators/__init__.py +++ b/pyrogram/methods/decorators/__init__.py @@ -28,6 +28,7 @@ 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 +43,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 000000000..6b11cf557 --- /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