88from io import BytesIO
99import logging
1010import re
11- from typing import TYPE_CHECKING , Callable
12- from typing_extensions import Final
11+ from typing import TYPE_CHECKING , Callable , Final
1312
1413import aiofiles
1514import anyio
@@ -489,9 +488,16 @@ def _parse_path(ws_message: str) -> list[str]:
489488 path = ["" ]
490489 return path
491490
492- async def _send_image (self , paths : list [str ], message : str ) -> None :
491+ async def _send_media (
492+ self ,
493+ paths : list [str ],
494+ message : str ,
495+ media_class : type [InputMediaPhoto | InputMediaVideo | InputMediaDocument ],
496+ media_name : str ,
497+ error_prefix : str ,
498+ ) -> None :
493499 try :
494- photos_list : list [InputMediaAudio | InputMediaDocument | InputMediaPhoto | InputMediaVideo ] = []
500+ media_list : list [InputMediaAudio | InputMediaDocument | InputMediaPhoto | InputMediaVideo ] = []
495501 for path in paths :
496502 path_obj = anyio .Path (path )
497503 if not await path_obj .is_file ():
@@ -505,96 +511,39 @@ async def _send_image(self, paths: list[str], message: str) -> None:
505511 bio .write (await fh .read ())
506512 bio .seek (0 )
507513 if bio .getbuffer ().nbytes > self ._max_upload_file_size * 1024 * 1024 :
508- await self ._bot .send_message (self ._chat_id , text = f"Telegram bots have a { self ._max_upload_file_size } mb filesize restriction, image couldn't be uploaded: `{ path } `" )
509- elif not photos_list :
510- photos_list .append (InputMediaPhoto (bio , filename = bio .name , caption = message ))
514+ await self ._bot .send_message (self ._chat_id , text = f"Telegram bots have a { self ._max_upload_file_size } mb filesize restriction, { media_name } couldn't be uploaded: `{ path } `" )
515+ elif not media_list :
516+ media_list .append (media_class (bio , filename = bio .name , caption = message ))
511517 else :
512- photos_list .append (InputMediaPhoto (bio , filename = bio .name ))
518+ media_list .append (media_class (bio , filename = bio .name ))
513519 bio .close ()
514520
515521 await self ._bot .send_media_group (
516522 self ._chat_id ,
517- media = photos_list ,
523+ media = media_list ,
518524 disable_notification = self ._silent_commands ,
525+ write_timeout = 120 ,
519526 )
520527
521528 except Exception as ex :
522529 logger .warning (ex )
523- await self ._bot .send_message (self ._chat_id , text = f"Error sending image : { ex } " , disable_notification = self ._silent_commands )
530+ await self ._bot .send_message (self ._chat_id , text = f"Error sending { error_prefix } : { ex } " , disable_notification = self ._silent_commands )
524531
525- def send_image (self , ws_message : str ) -> None :
526- self ._schedule_job ( self . _send_image , { "paths" : self . _parse_path ( ws_message ) , "message" : self . _parse_message ( ws_message )} )
532+ async def _send_image (self , paths : list [ str ], message : str ) -> None :
533+ await self ._send_media ( paths , message , InputMediaPhoto , "image" , "image" )
527534
528535 async def _send_video (self , paths : list [str ], message : str ) -> None :
529- try :
530- photos_list : list [InputMediaAudio | InputMediaDocument | InputMediaPhoto | InputMediaVideo ] = []
531- for path in paths :
532- path_obj = anyio .Path (path )
533- if not await path_obj .is_file ():
534- await self ._bot .send_message (self ._chat_id , text = "Provided path is not a file" , disable_notification = self ._silent_commands )
535- return
536-
537- bio = BytesIO ()
538- bio .name = path_obj .name
539-
540- async with aiofiles .open (path_obj , "rb" ) as fh :
541- bio .write (await fh .read ())
542- bio .seek (0 )
543- if bio .getbuffer ().nbytes > self ._max_upload_file_size * 1024 * 1024 :
544- await self ._bot .send_message (self ._chat_id , text = f"Telegram bots have a { self ._max_upload_file_size } mb filesize restriction, video couldn't be uploaded: `{ path } `" )
545- elif not photos_list :
546- photos_list .append (InputMediaVideo (bio , filename = bio .name , caption = message ))
547- else :
548- photos_list .append (InputMediaVideo (bio , filename = bio .name ))
549- bio .close ()
536+ await self ._send_media (paths , message , InputMediaVideo , "video" , "video" )
550537
551- await self ._bot .send_media_group (
552- self ._chat_id ,
553- media = photos_list ,
554- disable_notification = self ._silent_commands ,
555- write_timeout = 120 ,
556- )
538+ async def _send_document (self , paths : list [str ], message : str ) -> None :
539+ await self ._send_media (paths , message , InputMediaDocument , "document" , "document" )
557540
558- except Exception as ex :
559- logger .warning (ex )
560- await self ._bot .send_message (self ._chat_id , text = f"Error sending video: { ex } " , disable_notification = self ._silent_commands )
541+ def send_image (self , ws_message : str ) -> None :
542+ self ._schedule_job (self ._send_image , {"paths" : self ._parse_path (ws_message ), "message" : self ._parse_message (ws_message )})
561543
562544 def send_video (self , ws_message : str ) -> None :
563545 self ._schedule_job (self ._send_video , {"paths" : self ._parse_path (ws_message ), "message" : self ._parse_message (ws_message )})
564546
565- async def _send_document (self , paths : list [str ], message : str ) -> None :
566- try :
567- photos_list : list [InputMediaAudio | InputMediaDocument | InputMediaPhoto | InputMediaVideo ] = []
568- for path in paths :
569- path_obj = anyio .Path (path )
570- if not await path_obj .is_file ():
571- await self ._bot .send_message (self ._chat_id , text = "Provided path is not a file" , disable_notification = self ._silent_commands )
572- return
573-
574- bio = BytesIO ()
575- bio .name = path_obj .name
576-
577- async with aiofiles .open (path_obj , "rb" ) as fh :
578- bio .write (await fh .read ())
579- bio .seek (0 )
580- if bio .getbuffer ().nbytes > self ._max_upload_file_size * 1024 * 1024 :
581- await self ._bot .send_message (self ._chat_id , text = f"Telegram bots have a { self ._max_upload_file_size } mb filesize restriction, document couldn't be uploaded: `{ path } `" )
582- elif not photos_list :
583- photos_list .append (InputMediaDocument (bio , filename = bio .name , caption = message ))
584- else :
585- photos_list .append (InputMediaDocument (bio , filename = bio .name ))
586- bio .close ()
587-
588- await self ._bot .send_media_group (
589- self ._chat_id ,
590- media = photos_list ,
591- disable_notification = self ._silent_commands ,
592- )
593-
594- except Exception as ex :
595- logger .warning (ex )
596- await self ._bot .send_message (self ._chat_id , text = f"Error sending document: { ex } " , disable_notification = self ._silent_commands )
597-
598547 def send_document (self , ws_message : str ) -> None :
599548 self ._schedule_job (self ._send_document , {"paths" : self ._parse_path (ws_message ), "message" : self ._parse_message (ws_message )})
600549
0 commit comments