def delete_user_profile_photos(self, id: Union[str, List[str]]) -> bool: """Use this method to delete your own profile photos. Args: id (``str`` | ``list``): A single :obj:`Photo <pyrogram.Photo>` id as string or multiple ids as list of strings for deleting more than one photos at once. Returns: True on success. Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. """ id = id if isinstance(id, list) else [id] input_photos = [] for i in id: s = unpack("<qq", b64decode(i + "=" * (-len(i) % 4), "-_")) input_photos.append( types.InputPhoto(id=s[0], access_hash=s[1], file_reference=b"")) return bool(self.send(functions.photos.DeletePhotos(id=input_photos)))
def set_chat_photo( self, chat_id: Union[int, str], photo: str ) -> bool: """Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. You must be an administrator in the chat for this to work and must have the appropriate admin rights. Note: In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" setting is off. Args: chat_id (``int`` | ``str``): Unique identifier (int) or username (str) of the target chat. photo (``str``): New chat photo. You can pass a :class:`Photo` id or a file path to upload a new photo. Returns: True on success. Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. ``ValueError`` if a chat_id belongs to user. """ peer = self.resolve_peer(chat_id) if os.path.exists(photo): photo = types.InputChatUploadedPhoto(file=self.save_file(photo)) else: s = unpack("<qq", b64decode(photo + "=" * (-len(photo) % 4), "-_")) photo = types.InputChatPhoto( id=types.InputPhoto( id=s[0], access_hash=s[1], file_reference=b"" ) ) if isinstance(peer, types.InputPeerChat): self.send( functions.messages.EditChatPhoto( chat_id=peer.chat_id, photo=photo ) ) elif isinstance(peer, types.InputPeerChannel): self.send( functions.channels.EditPhoto( channel=peer, photo=photo ) ) else: raise ValueError("The chat_id \"{}\" belongs to a user".format(chat_id)) return True
async def delete_profile_photos(self, photo_ids: Union[str, List[str]]) -> bool: """Delete your own profile photos. Parameters: photo_ids (``str`` | List of ``str``): A single :obj:`Photo` id as string or multiple ids as list of strings for deleting more than one photos at once. Returns: ``bool``: True on success. Raises: RPCError: In case of a Telegram RPC error. """ photo_ids = photo_ids if isinstance(photo_ids, list) else [photo_ids] input_photos = [] for photo_id in photo_ids: unpacked = unpack("<iiqqc", utils.decode(photo_id)) input_photos.append( types.InputPhoto(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) return bool(await self.send(functions.photos.DeletePhotos(id=input_photos)))
def delete_profile_photos(self, photo_ids: Union[str, List[str]]) -> bool: """Delete your own profile photos. Parameters: photo_ids (``str`` | List of ``str``): A single :obj:`Photo` id as string or multiple ids as list of strings for deleting more than one photos at once. Returns: ``bool``: True on success. Example: .. code-block:: python # Get the photos to be deleted photos = app.get_profile_photos("me") # Delete one photo app.delete_profile_photos(photos[0].file_id) # Delete the rest of the photos app.delete_profile_photos([p.file_id for p in photos[1:]]) """ photo_ids = photo_ids if isinstance(photo_ids, list) else [photo_ids] input_photos = [] for photo_id in photo_ids: unpacked = unpack("<iiqqc", utils.decode_file_id(photo_id)) input_photos.append( types.InputPhoto(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) return bool(self.send(functions.photos.DeletePhotos(id=input_photos)))
async def inline_handler(c: bot, q: InlineQuery): text = q.query text = text if text else "Hello" cat_says_photo = await cat().cat_says(text) cat_says_photo_uploaded = await c.send_photo( chat_id=botCommon.dustbin, photo=str(cat_says_photo) ) try: os.remove(cat_says_photo) except Exception as e: logging.error(str(e)) cat_says_file_properties = await CustomFileProperties().get_properties( client=c, m_message_id=cat_says_photo_uploaded.message_id, m_chat_id=cat_says_photo_uploaded.chat.id ) try: await c.send( functions.messages.SetInlineBotResults( query_id=int(q.id), cache_time=0, results=[ types.InputBotInlineResultPhoto( id=str(secrets.token_hex(4)), type="photo", photo=types.InputPhoto( id=cat_says_file_properties.document_id, access_hash=cat_says_file_properties.access_hash, file_reference=utils.decode_file_ref(cat_says_file_properties.file_ref) ), send_message=types.InputBotInlineMessageMediaAuto( message=q.query ) ) ] ) ) except QueryIdInvalid: pass
def send_media_group( self, chat_id: Union[int, str], media: List[Union["pyrogram.InputMediaPhoto", "pyrogram.InputMediaVideo"]], disable_notification: bool = None, reply_to_message_id: int = None) -> List["pyrogram.Message"]: """Send a group of photos or videos as an album. Parameters: chat_id (``int`` | ``str``): 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). media (List of :obj:`InputMediaPhoto` and :obj:`InputMediaVideo`): A list describing photos and videos to be sent, must include 2–10 items. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. Returns: List of :obj:`Message`: On success, a list of the sent messages is returned. Example: .. code-block:: python from pyrogram import InputMediaPhoto, InputMediaVideo app.send_media_group( "me", [ InputMediaPhoto("photo1.jpg"), InputMediaPhoto("photo2.jpg", caption="photo caption"), InputMediaVideo("video.mp4", caption="a video") ] ) """ multi_media = [] for i in media: if isinstance(i, pyrogram.InputMediaPhoto): if os.path.exists(i.media): while True: try: media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=self.save_file(i.media)))) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) else: break media = types.InputMediaPhoto(id=types.InputPhoto( id=media.photo.id, access_hash=media.photo.access_hash, file_reference=media.photo.file_reference)) elif i.media.startswith("http"): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaPhotoExternal(url=i.media))) media = types.InputMediaPhoto(id=types.InputPhoto( id=media.photo.id, access_hash=media.photo.access_hash, file_reference=media.photo.file_reference)) else: media = utils.get_input_media_from_file_id( i.media, i.file_ref, 2) elif isinstance(i, pyrogram.InputMediaVideo): if os.path.exists(i.media): while True: try: media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( file=self.save_file(i.media), thumb=None if i.thumb is None else self.save_file(i.thumb), mime_type=self.guess_mime_type(i.media) or "video/mp4", attributes=[ types.DocumentAttributeVideo( supports_streaming=i. supports_streaming or None, duration=i.duration, w=i.width, h=i.height), types.DocumentAttributeFilename( file_name=os.path.basename( i.media)) ]))) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) else: break media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=media.document.file_reference)) elif i.media.startswith("http"): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaDocumentExternal( url=i.media))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=media.document.file_reference)) else: media = utils.get_input_media_from_file_id( i.media, i.file_ref, 4) multi_media.append( types.InputSingleMedia(media=media, random_id=self.rnd_id(), **self.parser.parse( i.caption, i.parse_mode))) while True: try: r = self.send( functions.messages.SendMultiMedia( peer=self.resolve_peer(chat_id), multi_media=multi_media, silent=disable_notification or None, reply_to_msg_id=reply_to_message_id)) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) else: break return utils.parse_messages( self, types.messages.Messages(messages=[ m.message for m in filter( lambda u: isinstance(u, (types.UpdateNewMessage, types. UpdateNewChannelMessage)), r.updates) ], users=r.users, chats=r.chats))
def send_media_group(self, chat_id: int or str, media: list, disable_notification: bool = None, reply_to_message_id: int = None): """Use this method to send a group of photos or videos as an album. On success, an Update containing the sent Messages is returned. Args: chat_id (``int`` | ``str``): 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). For a private channel/supergroup you can use its *t.me/joinchat/* link. media (``list``): A list containing either :obj:`InputMediaPhoto <pyrogram.InputMediaPhoto>` or :obj:`InputMediaVideo <pyrogram.InputMediaVideo>` objects describing photos and videos to be sent, must include 2–10 items. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. """ multi_media = [] for i in media: style = self.html if i.parse_mode.lower() == "html" else self.markdown if isinstance(i, pyrogram_types.InputMediaPhoto): if os.path.exists(i.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=self.save_file(i.media) ) ) ) media = types.InputMediaPhoto( id=types.InputPhoto( id=media.photo.id, access_hash=media.photo.access_hash ) ) else: try: decoded = utils.decode(i.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 2: media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None) if media_type: raise FileIdInvalid("The file_id belongs to a {}".format(media_type)) else: raise FileIdInvalid("Unknown media type: {}".format(unpacked[0])) media = types.InputMediaPhoto( id=types.InputPhoto( id=unpacked[2], access_hash=unpacked[3] ) ) elif isinstance(i, pyrogram_types.InputMediaVideo): if os.path.exists(i.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( file=self.save_file(i.media), mime_type=mimetypes.types_map[".mp4"], attributes=[ types.DocumentAttributeVideo( supports_streaming=i.supports_streaming or None, duration=i.duration, w=i.width, h=i.height ), types.DocumentAttributeFilename(os.path.basename(i.media)) ] ) ) ) media = types.InputMediaDocument( id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash ) ) else: try: decoded = utils.decode(i.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 4: media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None) if media_type: raise FileIdInvalid("The file_id belongs to a {}".format(media_type)) else: raise FileIdInvalid("Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument( id=types.InputDocument( id=unpacked[2], access_hash=unpacked[3] ) ) multi_media.append( types.InputSingleMedia( media=media, random_id=self.rnd_id(), **style.parse(i.caption) ) ) return self.send( functions.messages.SendMultiMedia( peer=self.resolve_peer(chat_id), multi_media=multi_media, silent=disable_notification or None, reply_to_msg_id=reply_to_message_id ) )
def send_photo( self, chat_id: Union[int, str], photo: str, caption: str = "", parse_mode: str = "", ttl_seconds: int = None, disable_notification: bool = None, reply_to_message_id: int = None, reply_markup: Union["pyrogram.InlineKeyboardMarkup", "pyrogram.ReplyKeyboardMarkup", "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None, progress: callable = None, progress_args: tuple = () ) -> Union["pyrogram.Message", None]: """Use this method to send photos. Args: chat_id (``int`` | ``str``): 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). photo (``str``): Photo to send. Pass a file_id as string to send a photo that exists on the Telegram servers, pass an HTTP URL as a string for Telegram to get a photo from the Internet, or pass a file path as string to upload a new photo that exists on your local machine. caption (``bool``, *optional*): Photo caption, 0-1024 characters. parse_mode (``str``, *optional*): Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>` if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption. Defaults to Markdown. ttl_seconds (``int``, *optional*): Self-Destruct Timer. If you set a timer, the photo will self-destruct in *ttl_seconds* seconds after it was viewed. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*): Additional interface options. An object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. progress (``callable``, *optional*): Pass a callback function to view the upload progress. The function must take *(client, current, total, \*args)* as positional arguments (look at the section below for a detailed description). progress_args (``tuple``, *optional*): Extra custom arguments for the progress callback function. Useful, for example, if you want to pass a chat_id and a message_id in order to edit a message with the updated progress. Other Parameters: client (:obj:`Client <pyrogram.Client>`): The Client itself, useful when you want to call other API methods inside the callback function. current (``int``): The amount of bytes uploaded so far. total (``int``): The size of the file. *args (``tuple``, *optional*): Extra custom arguments as defined in the *progress_args* parameter. You can either keep *\*args* or add every single extra argument in your function signature. Returns: On success, the sent :obj:`Message <pyrogram.Message>` is returned. In case the upload is deliberately stopped with :meth:`stop_transmission`, None is returned instead. Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. """ file = None style = self.html if parse_mode.lower() == "html" else self.markdown try: if os.path.exists(photo): file = self.save_file(photo, progress=progress, progress_args=progress_args) media = types.InputMediaUploadedPhoto(file=file, ttl_seconds=ttl_seconds) elif photo.startswith("http"): media = types.InputMediaPhotoExternal(url=photo, ttl_seconds=ttl_seconds) else: try: decoded = utils.decode(photo) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 2: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaPhoto(id=types.InputPhoto( id=unpacked[2], access_hash=unpacked[3], file_reference=b""), ttl_seconds=ttl_seconds) while True: try: r = self.send( functions.messages.SendMedia( peer=self.resolve_peer(chat_id), media=media, silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), reply_markup=reply_markup.write() if reply_markup else None, **style.parse(caption))) except FilePartMissing as e: self.save_file(photo, file_id=file.id, file_part=e.x) else: for i in r.updates: if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): return pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats}) except BaseClient.StopTransmission: return None
def edit_message_media( self, chat_id: Union[int, str], message_id: int, media: InputMedia, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> "pyrogram.Message": """Edit audio, document, photo, or video messages. If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise, message type can be changed arbitrarily. When inline message is edited, new file can't be uploaded. Use previously uploaded file via its file_id or specify a URL. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned. Parameters: chat_id (``int`` | ``str``): 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). message_id (``int``): Message identifier in the chat specified in chat_id. media (:obj:`InputMedia`) One of the InputMedia objects describing an animation, audio, document, photo or video. reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): An InlineKeyboardMarkup object. Returns: :obj:`Message`: On success, the edited message is returned. Raises: RPCError: In case of a Telegram RPC error. """ style = self.html if media.parse_mode.lower( ) == "html" else self.markdown caption = media.caption if isinstance(media, InputMediaPhoto): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=self.save_file(media.media)))) media = types.InputMediaPhoto( id=types.InputPhoto(id=media.photo.id, access_hash=media.photo.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaPhotoExternal(url=media.media) else: media = utils.get_input_media_from_file_id(media.media, 2) if isinstance(media, InputMediaVideo): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "video/mp4", thumb=None if media.thumb is None else self.save_file(media.thumb), file=self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=media.supports_streaming or None, duration=media.duration, w=media.width, h=media.height), types.DocumentAttributeFilename( file_name=os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: media = utils.get_input_media_from_file_id(media.media, 4) if isinstance(media, InputMediaAudio): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "audio/mpeg", thumb=None if media.thumb is None else self.save_file(media.thumb), file=self.save_file(media.media), attributes=[ types.DocumentAttributeAudio( duration=media.duration, performer=media.performer, title=media.title), types.DocumentAttributeFilename( file_name=os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: media = utils.get_input_media_from_file_id(media.media, 9) if isinstance(media, InputMediaAnimation): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "video/mp4", thumb=None if media.thumb is None else self.save_file(media.thumb), file=self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=True, duration=media.duration, w=media.width, h=media.height), types.DocumentAttributeFilename( file_name=os.path.basename(media.media)), types.DocumentAttributeAnimated() ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: media = utils.get_input_media_from_file_id(media.media, 10) if isinstance(media, InputMediaDocument): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "application/zip", thumb=None if media.thumb is None else self.save_file(media.thumb), file=self.save_file(media.media), attributes=[ types.DocumentAttributeFilename( file_name=os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: media = utils.get_input_media_from_file_id(media.media, 5) r = self.send( functions.messages.EditMessage( peer=self.resolve_peer(chat_id), id=message_id, reply_markup=reply_markup.write() if reply_markup else None, media=media, **style.parse(caption))) for i in r.updates: if isinstance( i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): return pyrogram.Message._parse(self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats})
async def edit_message_media( self, chat_id: Union[int, str], message_id: int, media: InputMedia, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> "pyrogram.Message": """Edit animation, audio, document, photo or video messages. If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise, the message type can be changed arbitrarily. Parameters: chat_id (``int`` | ``str``): 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). message_id (``int``): Message identifier in the chat specified in chat_id. media (:obj:`InputMedia`): One of the InputMedia objects describing an animation, audio, document, photo or video. reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): An InlineKeyboardMarkup object. Returns: :obj:`Message`: On success, the edited message is returned. Example: .. code-block:: python from pyrogram import InputMediaPhoto, InputMediaVideo, InputMediaAudio # Replace the current media with a local photo app.edit_message_media(chat_id, message_id, InputMediaPhoto("new_photo.jpg")) # Replace the current media with a local video app.edit_message_media(chat_id, message_id, InputMediaVideo("new_video.mp4")) # Replace the current media with a local audio app.edit_message_media(chat_id, message_id, InputMediaAudio("new_audio.mp3")) """ caption = media.caption parse_mode = media.parse_mode if isinstance(media, InputMediaPhoto): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=await self.save_file(media.media) ) ) ) media = types.InputMediaPhoto( id=types.InputPhoto( id=media.photo.id, access_hash=media.photo.access_hash, file_reference=media.photo.file_reference ) ) elif media.media.startswith("http"): media = types.InputMediaPhotoExternal( url=media.media ) else: media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2) elif isinstance(media, InputMediaVideo): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "video/mp4", thumb=None if media.thumb is None else await self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=media.supports_streaming or None, duration=media.duration, w=media.width, h=media.height ), types.DocumentAttributeFilename( file_name=os.path.basename(media.media) ) ] ) ) ) media = types.InputMediaDocument( id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=media.document.file_reference ) ) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4) elif isinstance(media, InputMediaAudio): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "audio/mpeg", thumb=None if media.thumb is None else await self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeAudio( duration=media.duration, performer=media.performer, title=media.title ), types.DocumentAttributeFilename( file_name=os.path.basename(media.media) ) ] ) ) ) media = types.InputMediaDocument( id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=media.document.file_reference ) ) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9) elif isinstance(media, InputMediaAnimation): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "video/mp4", thumb=None if media.thumb is None else self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=True, duration=media.duration, w=media.width, h=media.height ), types.DocumentAttributeFilename( file_name=os.path.basename(media.media) ), types.DocumentAttributeAnimated() ] ) ) ) media = types.InputMediaDocument( id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=media.document.file_reference ) ) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10) elif isinstance(media, InputMediaDocument): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=self.guess_mime_type(media.media) or "application/zip", thumb=None if media.thumb is None else await self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeFilename( file_name=os.path.basename(media.media) ) ] ) ) ) media = types.InputMediaDocument( id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=media.document.file_reference ) ) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal( url=media.media ) else: media = utils.get_input_media_from_file_id(media.media, media.file_ref, 5) r = await self.send( functions.messages.EditMessage( peer=await self.resolve_peer(chat_id), id=message_id, media=media, reply_markup=reply_markup.write() if reply_markup else None, **await self.parser.parse(caption, parse_mode) ) ) for i in r.updates: if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): return await pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats} )
def send_media_group(self, chat_id: Union[int, str], media: List[Union["pyrogram.InputMediaPhoto", "pyrogram.InputMediaVideo"]], disable_notification: bool = None, reply_to_message_id: int = None): """Use this method to send a group of photos or videos as an album. Args: chat_id (``int`` | ``str``): 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). media (List of :obj:`InputMediaPhoto` and :obj:`InputMediaVideo`): A list describing photos and videos to be sent, must include 2–10 items. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. Returns: On success, a :obj:`Messages <pyrogram.Messages>` object is returned containing all the single messages sent. Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. """ multi_media = [] for i in media: style = self.html if i.parse_mode.lower( ) == "html" else self.markdown if isinstance(i, pyrogram.InputMediaPhoto): if os.path.exists(i.media): while True: try: media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=self.save_file(i.media)))) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) else: break media = types.InputMediaPhoto(id=types.InputPhoto( id=media.photo.id, access_hash=media.photo.access_hash, file_reference=b"")) else: try: decoded = utils.decode(i.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 2: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format( unpacked[0])) media = types.InputMediaPhoto( id=types.InputPhoto(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) elif isinstance(i, pyrogram.InputMediaVideo): if os.path.exists(i.media): while True: try: media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( file=self.save_file(i.media), thumb=None if i.thumb is None else self.save_file(i.thumb), mime_type="video/mp4", attributes=[ types.DocumentAttributeVideo( supports_streaming=i. supports_streaming or None, duration=i.duration, w=i.width, h=i.height), types.DocumentAttributeFilename( file_name=os.path.basename( i.media)) ]))) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) else: break media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) else: try: decoded = utils.decode(i.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 4: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format( unpacked[0])) media = types.InputMediaDocument( id=types.InputDocument(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) multi_media.append( types.InputSingleMedia(media=media, random_id=self.rnd_id(), **style.parse(i.caption))) while True: try: r = self.send( functions.messages.SendMultiMedia( peer=self.resolve_peer(chat_id), multi_media=multi_media, silent=disable_notification or None, reply_to_msg_id=reply_to_message_id)) except FloodWait as e: log.warning("Sleeping for {}s".format(e.x)) time.sleep(e.x) else: break return pyrogram.Messages._parse( self, types.messages.Messages(messages=[ m.message for m in filter( lambda u: isinstance(u, (types.UpdateNewMessage, types. UpdateNewChannelMessage)), r.updates) ], users=r.users, chats=r.chats))
async def send_cached_media( self, chat_id: Union[int, str], file_id: str, caption: str = "", parse_mode: str = "", disable_notification: bool = None, reply_to_message_id: int = None, reply_markup: Union["pyrogram.InlineKeyboardMarkup", "pyrogram.ReplyKeyboardMarkup", "pyrogram.ReplyKeyboardRemove", "pyrogram.ForceReply"] = None ) -> Union["pyrogram.Message", None]: """Use this method to send any media stored on the Telegram servers using a file_id. This convenience method works with any valid file_id only. It does the same as calling the relevant method for sending media using a file_id, thus saving you from the hassle of using the correct method for the media the file_id is pointing to. Args: chat_id (``int`` | ``str``): 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). file_id (``str``): Media to send. Pass a file_id as string to send a media that exists on the Telegram servers. caption (``bool``, *optional*): Media caption, 0-1024 characters. parse_mode (``str``, *optional*): Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>` if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption. Defaults to Markdown. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*): Additional interface options. An object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. Returns: On success, the sent :obj:`Message <pyrogram.Message>` is returned. Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. """ style = self.html if parse_mode.lower() == "html" else self.markdown try: decoded = utils.decode(file_id) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None) if not media_type: raise FileIdInvalid("Unknown media type: {}".format( unpacked[0])) if media_type == "photo": media = types.InputMediaPhoto( id=types.InputPhoto(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) else: media = types.InputMediaDocument( id=types.InputDocument(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) r = await self.send( functions.messages.SendMedia( peer=await self.resolve_peer(chat_id), media=media, silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), reply_markup=reply_markup.write() if reply_markup else None, **await style.parse(caption))) for i in r.updates: if isinstance( i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): return await pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats})
async def edit_message_media( self, chat_id: Union[int, str], message_id: int, media: InputMedia, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> "pyrogram.Message": """Use this method to edit audio, document, photo, or video messages. If a message is a part of a message album, then it can be edited only to a photo or a video. Otherwise, message type can be changed arbitrarily. When inline message is edited, new file can't be uploaded. Use previously uploaded file via its file_id or specify a URL. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned. Args: chat_id (``int`` | ``str``): 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). message_id (``int``): Message identifier in the chat specified in chat_id. media (:obj:`InputMedia`) One of the InputMedia objects describing an animation, audio, document, photo or video. reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): An InlineKeyboardMarkup object. Returns: On success, the edited :obj:`Message <pyrogram.Message>` is returned. Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. """ style = self.html if media.parse_mode.lower( ) == "html" else self.markdown caption = media.caption if isinstance(media, InputMediaPhoto): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=await self.save_file(media.media)))) media = types.InputMediaPhoto( id=types.InputPhoto(id=media.photo.id, access_hash=media.photo.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaPhotoExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 2: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaPhoto( id=types.InputPhoto(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) if isinstance(media, InputMediaVideo): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type="video/mp4", thumb=None if media.thumb is None else self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=media.supports_streaming or None, duration=media.duration, w=media.width, h=media.height), types.DocumentAttributeFilename( file_name=os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 4: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument( id=types.InputDocument(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) if isinstance(media, InputMediaAudio): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type="audio/mpeg", thumb=None if media.thumb is None else self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeAudio( duration=media.duration, performer=media.performer, title=media.title), types.DocumentAttributeFilename( file_name=os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 9: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument( id=types.InputDocument(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) if isinstance(media, InputMediaAnimation): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type="video/mp4", thumb=None if media.thumb is None else self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=True, duration=media.duration, w=media.width, h=media.height), types.DocumentAttributeFilename( file_name=os.path.basename(media.media)), types.DocumentAttributeAnimated() ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 10: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument( id=types.InputDocument(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) if isinstance(media, InputMediaDocument): if os.path.exists(media.media): media = await self.send( functions.messages.UploadMedia( peer=await self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type="application/zip", thumb=None if media.thumb is None else self.save_file(media.thumb), file=await self.save_file(media.media), attributes=[ types.DocumentAttributeFilename( file_name=os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash, file_reference=b"")) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] not in (5, 10): media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument( id=types.InputDocument(id=unpacked[2], access_hash=unpacked[3], file_reference=b"")) r = await self.send( functions.messages.EditMessage( peer=await self.resolve_peer(chat_id), id=message_id, reply_markup=reply_markup.write() if reply_markup else None, media=media, **await style.parse(caption))) for i in r.updates: if isinstance( i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): return await pyrogram.Message._parse( self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats})
def edit_message_media(self, chat_id: int or str, message_id: int, media, reply_markup=None): style = self.html if media.parse_mode.lower( ) == "html" else self.markdown caption = media.caption if isinstance(media, InputMediaPhoto): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedPhoto( file=self.save_file(media.media)))) media = types.InputMediaPhoto(id=types.InputPhoto( id=media.photo.id, access_hash=media.photo.access_hash)) elif media.media.startswith("http"): media = types.InputMediaPhotoExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 2: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaPhoto(id=types.InputPhoto( id=unpacked[2], access_hash=unpacked[3])) if isinstance(media, InputMediaVideo): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=mimetypes.types_map[".mp4"], file=self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=media.supports_streaming or None, duration=media.duration, w=media.width, h=media.height), types.DocumentAttributeFilename( os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash)) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 4: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument(id=types.InputDocument( id=unpacked[2], access_hash=unpacked[3])) if isinstance(media, InputMediaAudio): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=mimetypes.types_map.get( "." + media.media.split(".")[-1], "audio/mpeg"), file=self.save_file(media.media), attributes=[ types.DocumentAttributeAudio( duration=media.duration, performer=media.performer, title=media.title), types.DocumentAttributeFilename( os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash)) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 9: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument(id=types.InputDocument( id=unpacked[2], access_hash=unpacked[3])) if isinstance(media, InputMediaAnimation): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=mimetypes.types_map[".mp4"], file=self.save_file(media.media), attributes=[ types.DocumentAttributeVideo( supports_streaming=True, duration=media.duration, w=media.width, h=media.height), types.DocumentAttributeFilename( os.path.basename(media.media)), types.DocumentAttributeAnimated() ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash)) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] != 10: media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument(id=types.InputDocument( id=unpacked[2], access_hash=unpacked[3])) if isinstance(media, InputMediaDocument): if os.path.exists(media.media): media = self.send( functions.messages.UploadMedia( peer=self.resolve_peer(chat_id), media=types.InputMediaUploadedDocument( mime_type=mimetypes.types_map.get( "." + media.media.split(".")[-1], "text/plain"), file=self.save_file(media.media), attributes=[ types.DocumentAttributeFilename( os.path.basename(media.media)) ]))) media = types.InputMediaDocument(id=types.InputDocument( id=media.document.id, access_hash=media.document.access_hash)) elif media.media.startswith("http"): media = types.InputMediaDocumentExternal(url=media.media) else: try: decoded = utils.decode(media.media) fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" unpacked = struct.unpack(fmt, decoded) except (AssertionError, binascii.Error, struct.error): raise FileIdInvalid from None else: if unpacked[0] not in (5, 10): media_type = BaseClient.MEDIA_TYPE_ID.get( unpacked[0], None) if media_type: raise FileIdInvalid( "The file_id belongs to a {}".format( media_type)) else: raise FileIdInvalid( "Unknown media type: {}".format(unpacked[0])) media = types.InputMediaDocument(id=types.InputDocument( id=unpacked[2], access_hash=unpacked[3])) r = self.send( functions.messages.EditMessage( peer=self.resolve_peer(chat_id), id=message_id, reply_markup=reply_markup.write() if reply_markup else None, media=media, **style.parse(caption))) for i in r.updates: if isinstance( i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): return utils.parse_messages(self, i.message, {i.id: i for i in r.users}, {i.id: i for i in r.chats})