@@ -27,8 +27,10 @@ class ForwardMessages(BaseClient):
2727 def forward_messages (self ,
2828 chat_id : Union [int , str ],
2929 from_chat_id : Union [int , str ],
30- message_ids : Iterable [int ],
31- disable_notification : bool = None ) -> "pyrogram.Messages" :
30+ message_ids : Union [int , Iterable [int ]],
31+ as_copy : bool = False ,
32+ disable_notification : bool = None ,
33+ no_captions : bool = False ) -> "pyrogram.Messages" :
3234 """Use this method to forward messages of any kind.
3335
3436 Args:
@@ -46,10 +48,18 @@ def forward_messages(self,
4648 A list of Message identifiers in the chat specified in *from_chat_id* or a single message id.
4749 Iterators and Generators are also accepted.
4850
51+ as_copy (``bool``, *optional*)
52+ Whether to keep forward headers on messages or resend messages without forward headers.
53+
4954 disable_notification (``bool``, *optional*):
5055 Sends the message silently.
5156 Users will receive a notification with no sound.
5257
58+ no_captions (``bool``, *optional*)
59+ If set to ``True`` and :arg:`as_copy` is enabled as well, media captions are not preserved
60+ when copying the message.
61+ Has no effect if :arg:`as_copy` is not enabled.
62+
5363 Returns:
5464 On success and in case *message_ids* was an iterable, the returned value will be a list of the forwarded
5565 :obj:`Messages <pyrogram.Message>` even if a list contains just one element, otherwise if
@@ -59,35 +69,53 @@ def forward_messages(self,
5969 Raises:
6070 :class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
6171 """
72+
6273 is_iterable = not isinstance (message_ids , int )
6374 message_ids = list (message_ids ) if is_iterable else [message_ids ]
6475
65- r = self .send (
66- functions .messages .ForwardMessages (
67- to_peer = self .resolve_peer (chat_id ),
68- from_peer = self .resolve_peer (from_chat_id ),
69- id = message_ids ,
70- silent = disable_notification or None ,
71- random_id = [self .rnd_id () for _ in message_ids ]
76+ if as_copy :
77+ sent_messages = []
78+ for chunk in [message_ids [i :i + 200 ] for i in range (0 , len (message_ids ), 200 )]:
79+ messages = self .get_messages (chat_id = from_chat_id , message_ids = chunk ) # type: pyrogram.Messages
80+ for message in messages .messages :
81+ sent_messages .append (message .forward (
82+ chat_id ,
83+ as_copy = True ,
84+ disable_notification = disable_notification ,
85+ no_caption = no_captions
86+ ))
87+ return pyrogram .Messages (
88+ client = self ,
89+ total_count = len (sent_messages ),
90+ messages = sent_messages
91+ ) if is_iterable else sent_messages [0 ]
92+ else :
93+ r = self .send (
94+ functions .messages .ForwardMessages (
95+ to_peer = self .resolve_peer (chat_id ),
96+ from_peer = self .resolve_peer (from_chat_id ),
97+ id = message_ids ,
98+ silent = disable_notification or None ,
99+ random_id = [self .rnd_id () for _ in message_ids ]
100+ )
72101 )
73- )
74102
75- messages = []
103+ forwarded_messages = []
76104
77- users = {i .id : i for i in r .users }
78- chats = {i .id : i for i in r .chats }
105+ users = {i .id : i for i in r .users }
106+ chats = {i .id : i for i in r .chats }
79107
80- for i in r .updates :
81- if isinstance (i , (types .UpdateNewMessage , types .UpdateNewChannelMessage )):
82- messages .append (
83- pyrogram .Message ._parse (
84- self , i .message ,
85- users , chats
108+ for i in r .updates :
109+ if isinstance (i , (types .UpdateNewMessage , types .UpdateNewChannelMessage )):
110+ forwarded_messages .append (
111+ pyrogram .Message ._parse (
112+ self , i .message ,
113+ users , chats
114+ )
86115 )
87- )
88116
89- return pyrogram .Messages (
90- client = self ,
91- total_count = len (messages ),
92- messages = messages
93- ) if is_iterable else messages [0 ]
117+ return pyrogram .Messages (
118+ client = self ,
119+ total_count = len (forwarded_messages ),
120+ messages = forwarded_messages
121+ ) if is_iterable else forwarded_messages [0 ]
0 commit comments