Skip to content

Commit 82c3ea8

Browse files
committed
refactor media sending methods to use send_media and handle deprecated reply parameters
1 parent 1368540 commit 82c3ea8

10 files changed

Lines changed: 462 additions & 779 deletions

File tree

pyrogram/methods/messages/send_animation.py

Lines changed: 45 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import logging
2019
import os
2120
import re
2221
from datetime import datetime
2322
from typing import BinaryIO, Callable, List, Optional, Union
2423

2524
import pyrogram
2625
from pyrogram import StopTransmission, enums, raw, types, utils
27-
from pyrogram.errors import FilePartMissing
2826
from pyrogram.file_id import FileType
27+
from pyrogram.utils import handle_deprecated_reply_parameters, send_media
2928

30-
log = logging.getLogger(__name__)
3129

3230
class SendAnimation:
3331
async def send_animation(
@@ -217,55 +215,16 @@ async def progress(current, total):
217215
218216
await app.send_animation("me", "animation.gif", progress=progress)
219217
"""
220-
if any(
221-
(
222-
reply_to_message_id is not None,
223-
reply_to_chat_id is not None,
224-
reply_to_story_id is not None,
225-
quote_text is not None,
226-
quote_entities is not None,
227-
quote_offset is not None,
228-
)
229-
):
230-
if reply_to_message_id is not None:
231-
log.warning(
232-
"`reply_to_message_id` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
233-
)
234-
235-
if reply_to_chat_id is not None:
236-
log.warning(
237-
"`reply_to_chat_id` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
238-
)
239-
240-
if reply_to_story_id is not None:
241-
log.warning(
242-
"`reply_to_story_id` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
243-
)
244-
245-
if quote_text is not None:
246-
log.warning(
247-
"`quote_text` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
248-
)
249-
250-
if quote_entities is not None:
251-
log.warning(
252-
"`quote_entities` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
253-
)
254-
255-
if quote_offset is not None:
256-
log.warning(
257-
"`quote_offset` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
258-
)
259-
260-
reply_parameters = types.ReplyParameters(
261-
message_id=reply_to_message_id,
262-
chat_id=reply_to_chat_id,
263-
story_id=reply_to_story_id,
264-
quote=quote_text,
265-
quote_parse_mode=parse_mode,
266-
quote_entities=quote_entities,
267-
quote_position=quote_offset
268-
)
218+
reply_parameters = handle_deprecated_reply_parameters(
219+
reply_to_message_id=reply_to_message_id,
220+
reply_to_chat_id=reply_to_chat_id,
221+
reply_to_story_id=reply_to_story_id,
222+
quote_text=quote_text,
223+
quote_entities=quote_entities,
224+
quote_offset=quote_offset,
225+
parse_mode=parse_mode,
226+
reply_parameters=reply_parameters
227+
)
269228

270229
file = None
271230

@@ -317,58 +276,39 @@ async def progress(current, total):
317276
]
318277
)
319278

320-
while True:
321-
try:
322-
peer = await self.resolve_peer(chat_id)
323-
r = await self.invoke(
324-
raw.functions.messages.SendMedia(
325-
peer=peer,
326-
media=media,
327-
silent=disable_notification or None,
328-
invert_media=show_caption_above_media,
329-
reply_to=await utils.get_reply_to(
330-
self,
331-
reply_parameters,
332-
message_thread_id,
333-
direct_messages_topic_id
334-
),
335-
random_id=self.rnd_id(),
336-
schedule_date=utils.datetime_to_timestamp(schedule_date),
337-
noforwards=protect_content,
338-
allow_paid_floodskip=allow_paid_broadcast,
339-
reply_markup=await reply_markup.write(self) if reply_markup else None,
340-
effect=effect_id,
341-
allow_paid_stars=paid_message_star_count,
342-
suggested_post=suggested_post_parameters.write() if suggested_post_parameters else None,
343-
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
344-
),
345-
business_connection_id=business_connection_id
346-
)
347-
except FilePartMissing as e:
348-
await self.save_file(animation, file_id=file.id, file_part=e.value)
349-
else:
350-
for i in r.updates:
351-
if isinstance(i, (raw.types.UpdateNewMessage,
352-
raw.types.UpdateNewChannelMessage,
353-
raw.types.UpdateNewScheduledMessage,
354-
raw.types.UpdateBotNewBusinessMessage)):
355-
message = await types.Message._parse(
356-
self, i.message,
357-
{i.id: i for i in r.users},
358-
{i.id: i for i in r.chats},
359-
is_scheduled=isinstance(i, raw.types.UpdateNewScheduledMessage),
360-
business_connection_id=getattr(i, "connection_id", None),
361-
)
362-
363-
if unsave and message.animation:
364-
document_id = utils.get_input_media_from_file_id(
365-
message.animation.file_id,
366-
FileType.ANIMATION,
367-
).id
368-
369-
await self.invoke(raw.functions.messages.SaveGif(id=document_id, unsave=True)) # type: ignore[arg-type]
370-
371-
return message
372-
279+
# Функция для обработки unsave после отправки
280+
async def post_process(client, message):
281+
if unsave and message.animation:
282+
document_id = utils.get_input_media_from_file_id(
283+
message.animation.file_id,
284+
FileType.ANIMATION,
285+
).id
286+
await client.invoke(raw.functions.messages.SaveGif(id=document_id, unsave=True))
287+
288+
return await send_media(
289+
client=self,
290+
chat_id=chat_id,
291+
media=media,
292+
caption=caption,
293+
parse_mode=parse_mode,
294+
caption_entities=caption_entities,
295+
disable_notification=disable_notification,
296+
message_thread_id=message_thread_id,
297+
direct_messages_topic_id=direct_messages_topic_id,
298+
effect_id=effect_id,
299+
show_caption_above_media=show_caption_above_media,
300+
reply_parameters=reply_parameters,
301+
schedule_date=schedule_date,
302+
protect_content=protect_content,
303+
business_connection_id=business_connection_id,
304+
allow_paid_broadcast=allow_paid_broadcast,
305+
paid_message_star_count=paid_message_star_count,
306+
suggested_post_parameters=suggested_post_parameters,
307+
reply_markup=reply_markup,
308+
file_reference=animation,
309+
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part),
310+
use_parse_messages=False, # Используем ручной парсинг для совместимости с unsave
311+
post_process_callback=post_process if unsave else None,
312+
)
373313
except StopTransmission:
374314
return None

pyrogram/methods/messages/send_audio.py

Lines changed: 33 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import logging
2019
import os
2120
import re
2221
from datetime import datetime
2322
from typing import BinaryIO, Callable, List, Optional, Union
2423

2524
import pyrogram
2625
from pyrogram import StopTransmission, enums, raw, types, utils
27-
from pyrogram.errors import FilePartMissing
2826
from pyrogram.file_id import FileType
27+
from pyrogram.utils import handle_deprecated_reply_parameters, send_media
2928

30-
log = logging.getLogger(__name__)
3129

3230
class SendAudio:
3331
async def send_audio(
@@ -207,55 +205,16 @@ async def progress(current, total):
207205
208206
await app.send_audio("me", "audio.mp3", progress=progress)
209207
"""
210-
if any(
211-
(
212-
reply_to_message_id is not None,
213-
reply_to_chat_id is not None,
214-
reply_to_story_id is not None,
215-
quote_text is not None,
216-
quote_entities is not None,
217-
quote_offset is not None,
218-
)
219-
):
220-
if reply_to_message_id is not None:
221-
log.warning(
222-
"`reply_to_message_id` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
223-
)
224-
225-
if reply_to_chat_id is not None:
226-
log.warning(
227-
"`reply_to_chat_id` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
228-
)
229-
230-
if reply_to_story_id is not None:
231-
log.warning(
232-
"`reply_to_story_id` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
233-
)
234-
235-
if quote_text is not None:
236-
log.warning(
237-
"`quote_text` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
238-
)
239-
240-
if quote_entities is not None:
241-
log.warning(
242-
"`quote_entities` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
243-
)
244-
245-
if quote_offset is not None:
246-
log.warning(
247-
"`quote_offset` is deprecated and will be removed in future updates. Use `reply_parameters` instead."
248-
)
249-
250-
reply_parameters = types.ReplyParameters(
251-
message_id=reply_to_message_id,
252-
chat_id=reply_to_chat_id,
253-
story_id=reply_to_story_id,
254-
quote=quote_text,
255-
quote_parse_mode=parse_mode,
256-
quote_entities=quote_entities,
257-
quote_position=quote_offset
258-
)
208+
reply_parameters = handle_deprecated_reply_parameters(
209+
reply_to_message_id=reply_to_message_id,
210+
reply_to_chat_id=reply_to_chat_id,
211+
reply_to_story_id=reply_to_story_id,
212+
quote_text=quote_text,
213+
quote_entities=quote_entities,
214+
quote_offset=quote_offset,
215+
parse_mode=parse_mode,
216+
reply_parameters=reply_parameters
217+
)
259218

260219
file = None
261220

@@ -306,37 +265,27 @@ async def progress(current, total):
306265
]
307266
)
308267

309-
while True:
310-
try:
311-
peer = await self.resolve_peer(chat_id)
312-
r = await self.invoke(
313-
raw.functions.messages.SendMedia(
314-
peer=peer,
315-
media=media,
316-
silent=disable_notification or None,
317-
reply_to=await utils.get_reply_to(
318-
self,
319-
reply_parameters,
320-
message_thread_id,
321-
direct_messages_topic_id
322-
),
323-
random_id=self.rnd_id(),
324-
schedule_date=utils.datetime_to_timestamp(schedule_date),
325-
noforwards=protect_content,
326-
allow_paid_floodskip=allow_paid_broadcast,
327-
reply_markup=await reply_markup.write(self) if reply_markup else None,
328-
effect=effect_id,
329-
allow_paid_stars=paid_message_star_count,
330-
suggested_post=suggested_post_parameters.write() if suggested_post_parameters else None,
331-
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
332-
),
333-
business_connection_id=business_connection_id
334-
)
335-
except FilePartMissing as e:
336-
await self.save_file(audio, file_id=file.id, file_part=e.value)
337-
else:
338-
messages = await utils.parse_messages(client=self, messages=r)
339-
340-
return messages[0] if messages else None
268+
return await send_media(
269+
client=self,
270+
chat_id=chat_id,
271+
media=media,
272+
caption=caption,
273+
parse_mode=parse_mode,
274+
caption_entities=caption_entities,
275+
disable_notification=disable_notification,
276+
message_thread_id=message_thread_id,
277+
direct_messages_topic_id=direct_messages_topic_id,
278+
effect_id=effect_id,
279+
reply_parameters=reply_parameters,
280+
schedule_date=schedule_date,
281+
protect_content=protect_content,
282+
business_connection_id=business_connection_id,
283+
allow_paid_broadcast=allow_paid_broadcast,
284+
paid_message_star_count=paid_message_star_count,
285+
suggested_post_parameters=suggested_post_parameters,
286+
reply_markup=reply_markup,
287+
file_reference=audio,
288+
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part),
289+
)
341290
except StopTransmission:
342291
return None

0 commit comments

Comments
 (0)