|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import binascii |
| 20 | +import struct |
| 21 | +from typing import Union |
| 22 | + |
| 23 | +import pyrogram |
| 24 | +from pyrogram.api import functions, types |
| 25 | +from pyrogram.api.errors import FileIdInvalid |
| 26 | +from pyrogram.client.ext import BaseClient, utils |
| 27 | + |
| 28 | + |
| 29 | +class SendCachedMedia(BaseClient): |
| 30 | + def send_cached_media( |
| 31 | + self, |
| 32 | + chat_id: Union[int, str], |
| 33 | + file_id: str, |
| 34 | + caption: str = "", |
| 35 | + parse_mode: str = "", |
| 36 | + disable_notification: bool = None, |
| 37 | + reply_to_message_id: int = None, |
| 38 | + reply_markup: Union[ |
| 39 | + "pyrogram.InlineKeyboardMarkup", |
| 40 | + "pyrogram.ReplyKeyboardMarkup", |
| 41 | + "pyrogram.ReplyKeyboardRemove", |
| 42 | + "pyrogram.ForceReply" |
| 43 | + ] = None |
| 44 | + ) -> Union["pyrogram.Message", None]: |
| 45 | + """Use this method to send any media stored on the Telegram servers using a file_id. |
| 46 | +
|
| 47 | + This convenience method works with any valid file_id only. |
| 48 | + It does the same as calling the relevant method for sending media using a file_id, thus saving you from the |
| 49 | + hassle of using the correct method for the media the file_id is pointing to. |
| 50 | +
|
| 51 | + Args: |
| 52 | + chat_id (``int`` | ``str``): |
| 53 | + Unique identifier (int) or username (str) of the target chat. |
| 54 | + For your personal cloud (Saved Messages) you can simply use "me" or "self". |
| 55 | + For a contact that exists in your Telegram address book you can use his phone number (str). |
| 56 | +
|
| 57 | + file_id (``str``): |
| 58 | + Media to send. |
| 59 | + Pass a file_id as string to send a media that exists on the Telegram servers. |
| 60 | +
|
| 61 | + caption (``bool``, *optional*): |
| 62 | + Media caption, 0-1024 characters. |
| 63 | +
|
| 64 | + parse_mode (``str``, *optional*): |
| 65 | + Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>` |
| 66 | + if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption. |
| 67 | + Defaults to Markdown. |
| 68 | +
|
| 69 | + disable_notification (``bool``, *optional*): |
| 70 | + Sends the message silently. |
| 71 | + Users will receive a notification with no sound. |
| 72 | +
|
| 73 | + reply_to_message_id (``int``, *optional*): |
| 74 | + If the message is a reply, ID of the original message. |
| 75 | +
|
| 76 | + reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*): |
| 77 | + Additional interface options. An object for an inline keyboard, custom reply keyboard, |
| 78 | + instructions to remove reply keyboard or to force a reply from the user. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + On success, the sent :obj:`Message <pyrogram.Message>` is returned. |
| 82 | +
|
| 83 | + Raises: |
| 84 | + :class:`Error <pyrogram.Error>` in case of a Telegram RPC error. |
| 85 | + """ |
| 86 | + style = self.html if parse_mode.lower() == "html" else self.markdown |
| 87 | + |
| 88 | + try: |
| 89 | + decoded = utils.decode(file_id) |
| 90 | + fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" |
| 91 | + unpacked = struct.unpack(fmt, decoded) |
| 92 | + except (AssertionError, binascii.Error, struct.error): |
| 93 | + raise FileIdInvalid from None |
| 94 | + else: |
| 95 | + media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None) |
| 96 | + |
| 97 | + if not media_type: |
| 98 | + raise FileIdInvalid("Unknown media type: {}".format(unpacked[0])) |
| 99 | + |
| 100 | + if media_type == "photo": |
| 101 | + media = types.InputMediaPhoto( |
| 102 | + id=types.InputPhoto( |
| 103 | + id=unpacked[2], |
| 104 | + access_hash=unpacked[3], |
| 105 | + file_reference=b"" |
| 106 | + ) |
| 107 | + ) |
| 108 | + else: |
| 109 | + media = types.InputMediaDocument( |
| 110 | + id=types.InputDocument( |
| 111 | + id=unpacked[2], |
| 112 | + access_hash=unpacked[3], |
| 113 | + file_reference=b"" |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + r = self.send( |
| 118 | + functions.messages.SendMedia( |
| 119 | + peer=self.resolve_peer(chat_id), |
| 120 | + media=media, |
| 121 | + silent=disable_notification or None, |
| 122 | + reply_to_msg_id=reply_to_message_id, |
| 123 | + random_id=self.rnd_id(), |
| 124 | + reply_markup=reply_markup.write() if reply_markup else None, |
| 125 | + **style.parse(caption) |
| 126 | + ) |
| 127 | + ) |
| 128 | + |
| 129 | + for i in r.updates: |
| 130 | + if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): |
| 131 | + return pyrogram.Message._parse( |
| 132 | + self, i.message, |
| 133 | + {i.id: i for i in r.users}, |
| 134 | + {i.id: i for i in r.chats} |
| 135 | + ) |
0 commit comments