Skip to content

Commit

Permalink
pyrofork: Add reply_to_story_id to copy_media_group and send_inline_b…
Browse files Browse the repository at this point in the history
…ot_result method

Signed-off-by: Yasir Aris <git@yasir.id>
  • Loading branch information
yasirarism committed Jan 3, 2025
1 parent 2bb99d9 commit 8dcbff5
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 50 deletions.
81 changes: 49 additions & 32 deletions pyrogram/methods/bots/send_inline_bot_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from typing import List, Union, Optional
from datetime import datetime
from typing import Union, List, Optional

import pyrogram
from pyrogram import enums, raw, types, utils
from pyrogram import raw, enums, types
from pyrogram import utils


class SendInlineBotResult:
Expand All @@ -29,12 +31,16 @@ async def send_inline_bot_result(
chat_id: Union[int, str],
query_id: int,
result_id: str,
disable_notification: bool = None,
message_thread_id: int = None,
reply_to_message_id: int = None,
quote_text: str = None,
quote_entities: List["types.MessageEntity"] = None,
parse_mode: Optional["enums.ParseMode"] = None
disable_notification: Optional[bool] = None,
message_thread_id: Optional[int] = None,
reply_to_message_id: Optional[int] = None,
reply_to_chat_id: Union[int, str] = None,
reply_to_story_id: Optional[int] = None,
quote_text: Optional[str] = None,
parse_mode: Optional["enums.ParseMode"] = None,
quote_entities: Optional[List["types.MessageEntity"]] = None,
quote_offset: Optional[int] = None,
schedule_date: datetime = None,
) -> "types.Message":
"""Send an inline bot result.
Bot results can be retrieved using :meth:`~pyrogram.Client.get_inline_bot_results`
Expand All @@ -46,7 +52,6 @@ async def send_inline_bot_result(
Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
You can also use user profile/chat public link in form of *t.me/<username>* (str).
query_id (``int``):
Unique identifier for the answered query.
Expand All @@ -60,42 +65,42 @@ async def send_inline_bot_result(
message_thread_id (``int``, *optional*):
Unique identifier of a message thread to which the message belongs.
for supergroups only
For supergroups only.
reply_to_message_id (``bool``, *optional*):
If the message is a reply, ID of the original message.
quote_text (``str``, *optional*):
Text to quote.
for reply_to_message only.
reply_to_chat_id (``int``, *optional*):
If the message is a reply, ID of the original chat.
quote_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
List of special entities that appear in quote_text, which can be specified instead of *parse_mode*.
for reply_to_message only.
reply_to_story_id (``int``, *optional*):
If the message is a reply, ID of the target story.
quote_text (``str``, *optional*):
Text of the quote to be sent.
parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
By default, quote_text are parsed using both Markdown and HTML styles.
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
For quote_text.
quote_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
List of special entities that appear in quote text, which can be specified instead of *parse_mode*.
quote_offset (``int``, *optional*):
Offset for quote in original message.
schedule_date (:py:obj:`~datetime.datetime`, *optional*):
Date when the message will be automatically sent.
Returns:
:obj:`~pyrogram.types.Message`: On success, the sent message is returned.
:obj:`~pyrogram.types.Message`: On success, the sent message is returned or False if no message was sent.
Example:
.. code-block:: python
await app.send_inline_bot_result(chat_id, query_id, result_id)
"""

reply_to = await utils.get_reply_to(
client=self,
chat_id=chat_id,
reply_to_message_id=reply_to_message_id,
message_thread_id=message_thread_id,
quote_text=quote_text,
quote_entities=quote_entities,
parse_mode=parse_mode
)
quote_text, quote_entities = (await utils.parse_text_entities(self, quote_text, parse_mode, quote_entities)).values()

r = await self.invoke(
raw.functions.messages.SendInlineBotResult(
Expand All @@ -104,15 +109,27 @@ async def send_inline_bot_result(
id=result_id,
random_id=self.rnd_id(),
silent=disable_notification or None,
reply_to=reply_to
reply_to=utils.get_reply_to(
reply_to_message_id=reply_to_message_id,
reply_to_peer=await self.resolve_peer(reply_to_chat_id) if reply_to_chat_id else None,
reply_to_story_id=reply_to_story_id,
message_thread_id=message_thread_id,
quote_text=quote_text,
quote_entities=quote_entities,
quote_offset=quote_offset,
),
schedule_date=utils.datetime_to_timestamp(schedule_date),
)
)

for i in r.updates:
if isinstance(i, (raw.types.UpdateNewMessage,
raw.types.UpdateNewChannelMessage)):
raw.types.UpdateNewChannelMessage,
raw.types.UpdateNewScheduledMessage)):
return await types.Message._parse(
self, i.message,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
)
is_scheduled=isinstance(i, raw.types.UpdateNewScheduledMessage),
business_connection_id=getattr(i, "connection_id", None)
)
77 changes: 59 additions & 18 deletions pyrogram/methods/messages/copy_media_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from datetime import datetime
from typing import Union, List
from typing import Union, List, Optional

import pyrogram
from pyrogram import types, utils, raw
from pyrogram import types, utils, raw, enums


class CopyMediaGroup:
Expand All @@ -31,11 +31,18 @@ async def copy_media_group(
from_chat_id: Union[int, str],
message_id: int,
captions: Union[List[str], str] = None,
has_spoilers: Union[List[bool], bool] = None,
disable_notification: bool = None,
message_thread_id: int = None,
reply_to_message_id: int = None,
reply_to_chat_id: Union[int, str] = None,
reply_to_story_id: int = None,
quote_text: str = None,
parse_mode: Optional["enums.ParseMode"] = None,
quote_entities: List["types.MessageEntity"] = None,
quote_offset: int = None,
schedule_date: datetime = None,
protect_content: bool = None,
invert_media: bool = None,
) -> List["types.Message"]:
"""Copy a media group by providing one of the message ids.
Expand All @@ -46,13 +53,11 @@ async def copy_media_group(
Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
You can also use chat public link in form of *t.me/<username>* (str).
from_chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the source chat where the original media group was sent.
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
You can also use chat public link in form of *t.me/<username>* (str).
message_id (``int``):
Message identifier in the chat specified in *from_chat_id*.
Expand All @@ -72,16 +77,35 @@ async def copy_media_group(
message_thread_id (``int``, *optional*):
Unique identifier for the target message thread (topic) of the forum.
for forum supergroups only.
For supergroups only.
reply_to_message_id (``int``, *optional*):
If the message is a reply, ID of the original message.
reply_to_chat_id (``int``, *optional*):
If the message is a reply, ID of the original chat.
reply_to_story_id (``int``, *optional*):
If the message is a reply, ID of the target story.
quote_text (``str``, *optional*):
Text of the quote to be sent.
parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
quote_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
List of special entities that appear in quote text, which can be specified instead of *parse_mode*.
quote_offset (``int``, *optional*):
Offset for quote in original message.
schedule_date (:py:obj:`~datetime.datetime`, *optional*):
Date when the message will be automatically sent.
protect_content (``bool``, *optional*):
Protects the contents of the sent message from forwarding and saving
invert_media (``bool``, *optional*):
Pass True, if the caption must be shown above the message media.
Returns:
List of :obj:`~pyrogram.types.Message`: On success, a list of copied messages is returned.
Expand All @@ -93,18 +117,15 @@ async def copy_media_group(
await app.copy_media_group(to_chat, from_chat, 123)
await app.copy_media_group(to_chat, from_chat, 123, captions="single caption")
await app.copy_media_group(to_chat, from_chat, 123,
captions=["caption 1", None, ""])
"""
quote_text, quote_entities = (await utils.parse_text_entities(self, quote_text, parse_mode, quote_entities)).values()

media_group = await self.get_media_group(from_chat_id, message_id)
multi_media = []

reply_to = None
if reply_to_message_id or message_thread_id:
reply_to = types.InputReplyToMessage(reply_to_message_id=reply_to_message_id, message_thread_id=message_thread_id)

for i, message in enumerate(media_group):
if message.photo:
file_id = message.photo.file_id
Expand All @@ -117,7 +138,19 @@ async def copy_media_group(
else:
raise ValueError("Message with this type can't be copied.")

media = utils.get_input_media_from_file_id(file_id=file_id)
media = utils.get_input_media_from_file_id(
file_id=file_id,
has_spoiler=(
has_spoilers[i]
if isinstance(has_spoilers, list)
and i < len(has_spoilers)
else (
has_spoilers
if isinstance(has_spoilers, bool)
else message.has_media_spoiler
)
),
)
multi_media.append(
raw.types.InputSingleMedia(
media=media,
Expand All @@ -135,9 +168,17 @@ async def copy_media_group(
peer=await self.resolve_peer(chat_id),
multi_media=multi_media,
silent=disable_notification or None,
reply_to=reply_to,
noforwards=protect_content,
schedule_date=utils.datetime_to_timestamp(schedule_date)
reply_to=utils.get_reply_to(
reply_to_message_id=reply_to_message_id,
message_thread_id=message_thread_id,
reply_to_peer=await self.resolve_peer(reply_to_chat_id) if reply_to_chat_id else None,
reply_to_story_id=reply_to_story_id,
quote_text=quote_text,
quote_entities=quote_entities,
quote_offset=quote_offset,
),
schedule_date=utils.datetime_to_timestamp(schedule_date),
invert_media=invert_media
),
sleep_threshold=60
)
Expand All @@ -154,4 +195,4 @@ async def copy_media_group(
users=r.users,
chats=r.chats
)
)
)

0 comments on commit 8dcbff5

Please sign in to comment.