From 2d55655ad564f285bced98e8eed93d280a492acf Mon Sep 17 00:00:00 2001 From: wulan17 Date: Wed, 6 Dec 2023 22:33:13 +0700 Subject: [PATCH] Pyrofork: Add forward_story method Signed-off-by: wulan17 --- compiler/docs/compiler.py | 1 + pyrogram/methods/users/__init__.py | 2 + pyrogram/methods/users/forward_story.py | 126 ++++++++++++++++++++++++ pyrogram/methods/users/send_story.py | 20 +++- 4 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 pyrogram/methods/users/forward_story.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 974139c66..13e884e01 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -274,6 +274,7 @@ def get_title_list(s: str) -> list: delete_stories edit_story export_story_link + forward_story get_all_stories get_stories get_stories_history diff --git a/pyrogram/methods/users/__init__.py b/pyrogram/methods/users/__init__.py index 0bcc8a971..be4d6037d 100644 --- a/pyrogram/methods/users/__init__.py +++ b/pyrogram/methods/users/__init__.py @@ -21,6 +21,7 @@ from .delete_stories import DeleteStories from .edit_story import EditStory from .export_story_link import ExportStoryLink +from .forward_story import ForwardStory from .get_chat_photos import GetChatPhotos from .get_chat_photos_count import GetChatPhotosCount from .get_common_chats import GetCommonChats @@ -44,6 +45,7 @@ class Users( DeleteStories, EditStory, ExportStoryLink, + ForwardStory, GetCommonChats, GetChatPhotos, SetProfilePhoto, diff --git a/pyrogram/methods/users/forward_story.py b/pyrogram/methods/users/forward_story.py new file mode 100644 index 000000000..575c101ca --- /dev/null +++ b/pyrogram/methods/users/forward_story.py @@ -0,0 +1,126 @@ +# 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 . + +import os +import re +from typing import List, Union + +import pyrogram +from pyrogram import enums, raw, types, utils +from pyrogram.file_id import FileType + +class ForwardStory: + def _split(self, message, entities, *args, **kwargs): + return message, entities + + async def forward_story( + self: "pyrogram.Client", + from_chat_id: Union[int, str], + from_story_id: int, + channel_id: int = None, + privacy: "enums.StoriesPrivacyRules" = None, + allowed_users: List[int] = None, + denied_users: List[int] = None, + #allowed_chats: List[int] = None, + #denied_chats: List[int] = None, + pinned: bool = None, + protect_content: bool = None, + caption: str = None, + parse_mode: "enums.ParseMode" = None, + caption_entities: List["types.MessageEntity"] = None, + period: int = None + ) -> "types.Story": + """Forward a story. + + .. include:: /_includes/usable-by/users.rst + + Note: You must pass one of following paramater *animation*, *photo*, *video* + + Parameters: + from_chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat/user. + For your personal story you can simply use "me" or "self". + For a contact that exists in your Telegram address book you can use his phone number (str). + + from_story_id (``int``): + Unique identifier of original story. + + channel_id (``int``, *optional*): + Unique identifier (int) of the target channel. + If you want to forward story to a channel. + + privacy (:obj:`~pyrogram.enums.StoriesPrivacyRules`, *optional*): + Story privacy. + Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC` + + allowed_users (List of ``int``, *optional*): + List of user_id whos allowed to view the story. + + denied_users (List of ``int``, *optional*): + List of user_id whos denied to view the story. + + pinned (``bool``, *optional*): + if True, the story will be pinned. + default to False. + + protect_content (``bool``, *optional*): + Protects the contents of the sent story from forwarding and saving. + default to False. + + caption (``str``, *optional*): + Story caption, 0-1024 characters. + + parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): + By default, texts are parsed using both Markdown and HTML styles. + You can combine both syntaxes together. + + caption_entities (List of :obj:`~pyrogram.types.MessageEntity`): + List of special entities that appear in the caption, which can be specified instead of *parse_mode*. + + period (``int``, *optional*): + How long the story will posted, in secs. + only for premium users. + + Returns: + :obj:`~pyrogram.types.Story` a single story is returned. + + Example: + .. code-block:: python + + # forward a story + photo_id = "abcd12345" + await app.send_story(photo=photo_id, caption='Hello guys.') + + Raises: + ValueError: In case of invalid arguments. + """ + + return await self.send_story( + channel_id=channel_id, + privacy=privacy, + allowed_users=allowed_users, + denied_users=denied_users, + pinned=pinned, + protect_content=protect_content, + caption=caption, + caption_entities=caption_entities, + parse_mode=parse_mode, + period=period, + forward_from_chat_id=from_chat_id, + forward_from_story_id=from_story_id + ) diff --git a/pyrogram/methods/users/send_story.py b/pyrogram/methods/users/send_story.py index 531d77076..4b6ea9b1e 100644 --- a/pyrogram/methods/users/send_story.py +++ b/pyrogram/methods/users/send_story.py @@ -18,7 +18,7 @@ import os import re -from typing import List +from typing import List, Union import pyrogram from pyrogram import enums, raw, types, utils @@ -44,7 +44,9 @@ async def send_story( caption: str = None, parse_mode: "enums.ParseMode" = None, caption_entities: List["types.MessageEntity"] = None, - period: int = None + period: int = None, + forward_from_chat_id: Union[int, str] = None, + forward_from_story_id: int = None ) -> "types.Story": """Send new story. @@ -227,7 +229,8 @@ async def send_story( ] ) else: - raise ValueError("You need to pass one of the following parameter animation/photo/video!") + if forward_from_chat_id is None: + raise ValueError("You need to pass one of the following parameter animation/photo/video/forward_from_chat_id!") text, entities = self._split(**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)) @@ -246,6 +249,12 @@ async def send_story( users = [await self.resolve_peer(user_id) for user_id in denied_users] privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) + if forward_from_chat_id is not None: + forward_from_chat = await self.resolve_peer(forward_from_chat_id) + media = raw.types.InputMediaEmpty() + if forward_from_story_id is None: + raise ValueError("You need to pass forward_from_story_id to forward story!") + r = await self.invoke( raw.functions.stories.SendStory( peer=peer, @@ -256,7 +265,10 @@ async def send_story( noforwards=protect_content, caption=text, entities=entities, - period=period + period=period, + fwd_from_id=forward_from_chat, + fwd_from_story=forward_from_story_id if forward_from_chat_id is not None else None, + fwd_modified=True if forward_from_chat_id is not None and caption is not None else False ) ) return await types.Story._parse(self, r.updates[0].story, r.updates[0].peer)