From 74e82c78ccdac8d66a3c99a340b02b36e6359efc Mon Sep 17 00:00:00 2001 From: tangyoha Date: Sun, 29 Oct 2023 09:18:28 +0800 Subject: [PATCH] feat: support multi media forward --- module/bot.py | 7 ++--- module/pyrogram_extension.py | 61 ++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/module/bot.py b/module/bot.py index 4eeaf5ad..2d11e18b 100644 --- a/module/bot.py +++ b/module/bot.py @@ -875,10 +875,9 @@ async def forward_normal_content( await report_bot_forward_status(client, node, forward_ret) return - if message.media_group_id: - await upload_telegram_chat_message( - _bot.client, node.upload_user, _bot.app, node, message - ) + await upload_telegram_chat_message( + _bot.client, node.upload_user, _bot.app, node, message + ) async def forward_msg(node: TaskNode, message_id: int): diff --git a/module/pyrogram_extension.py b/module/pyrogram_extension.py index 6d7923d3..bd1e6c49 100644 --- a/module/pyrogram_extension.py +++ b/module/pyrogram_extension.py @@ -5,10 +5,11 @@ import secrets import struct import time +from datetime import datetime from functools import wraps from io import BytesIO, StringIO from mimetypes import MimeTypes -from typing import Callable, List, Optional, Union +from typing import Callable, Iterable, List, Optional, Union import pyrogram from loguru import logger @@ -427,8 +428,12 @@ async def _upload_telegram_chat_message( if not message.media_group_id: if not file_name: - await client.forward_messages( - node.upload_telegram_chat_id, node.chat_id, message.id + await forward_messages( + client, + node.upload_telegram_chat_id, # type: ignore + node.chat_id, + message.id, + drop_author=True, ) else: await _upload_signal_message( @@ -1091,3 +1096,53 @@ async def start(self): await self.initialize() return self + + +async def forward_messages( + client: pyrogram.Client, + chat_id: Union[int, str], + from_chat_id: Union[int, str], + message_ids: Union[int, Iterable[int]], + disable_notification: bool = None, + schedule_date: datetime = None, + protect_content: bool = None, + drop_author: bool = None, +) -> Union["types.Message", List["types.Message"]]: + """Forward messages of any kind.""" + + is_iterable = not isinstance(message_ids, int) + message_ids = list(message_ids) if is_iterable else [message_ids] # type: ignore + + r = await client.invoke( + pyrogram.raw.functions.messages.ForwardMessages( + to_peer=await client.resolve_peer(chat_id), + from_peer=await client.resolve_peer(from_chat_id), + id=message_ids, + silent=disable_notification or None, + random_id=[client.rnd_id() for _ in message_ids], + schedule_date=pyrogram.utils.datetime_to_timestamp(schedule_date), + noforwards=protect_content, + drop_author=drop_author, + ) + ) + + forwarded_messages = [] + + users = {i.id: i for i in r.users} + chats = {i.id: i for i in r.chats} + + for i in r.updates: + if isinstance( + i, + ( + pyrogram.raw.types.UpdateNewMessage, + pyrogram.raw.types.UpdateNewChannelMessage, + pyrogram.raw.types.UpdateNewScheduledMessage, + ), + ): + forwarded_messages.append( + # pylint: disable=W0212 + await types.Message._parse(client, i.message, users, chats) + ) + + return types.List(forwarded_messages) if is_iterable else forwarded_messages[0]