|
| 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 pyrogram |
| 20 | +from pyrogram.api import functions, types |
| 21 | +from pyrogram.client.ext import BaseClient, utils |
| 22 | +from pyrogram.client.types import ( |
| 23 | + InputMediaPhoto, InputMediaVideo, InputMediaAudio, |
| 24 | + InputMediaAnimation, InputMediaDocument |
| 25 | +) |
| 26 | +from pyrogram.client.types.input_media import InputMedia |
| 27 | + |
| 28 | + |
| 29 | +class EditInlineMedia(BaseClient): |
| 30 | + def edit_inline_media( |
| 31 | + self, |
| 32 | + inline_message_id: str, |
| 33 | + media: InputMedia, |
| 34 | + reply_markup: "pyrogram.InlineKeyboardMarkup" = None |
| 35 | + ) -> bool: |
| 36 | + """Edit **inline** animation, audio, document, photo or video messages. |
| 37 | +
|
| 38 | + When the inline message is edited, a new file can't be uploaded. Use a previously uploaded file via its file_id |
| 39 | + or specify a URL. |
| 40 | +
|
| 41 | + Parameters: |
| 42 | + inline_message_id (``str``): |
| 43 | + Required if *chat_id* and *message_id* are not specified. |
| 44 | + Identifier of the inline message. |
| 45 | +
|
| 46 | + media (:obj:`InputMedia`): |
| 47 | + One of the InputMedia objects describing an animation, audio, document, photo or video. |
| 48 | +
|
| 49 | + reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): |
| 50 | + An InlineKeyboardMarkup object. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + ``bool``: On success, True is returned. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + RPCError: In case of a Telegram RPC error. |
| 57 | + """ |
| 58 | + style = self.html if media.parse_mode.lower() == "html" else self.markdown |
| 59 | + caption = media.caption |
| 60 | + |
| 61 | + if isinstance(media, InputMediaPhoto): |
| 62 | + if media.media.startswith("http"): |
| 63 | + media = types.InputMediaPhotoExternal( |
| 64 | + url=media.media |
| 65 | + ) |
| 66 | + else: |
| 67 | + media = utils.get_input_media_from_file_id(media.media, 2) |
| 68 | + elif isinstance(media, InputMediaVideo): |
| 69 | + if media.media.startswith("http"): |
| 70 | + media = types.InputMediaDocumentExternal( |
| 71 | + url=media.media |
| 72 | + ) |
| 73 | + else: |
| 74 | + media = utils.get_input_media_from_file_id(media.media, 4) |
| 75 | + elif isinstance(media, InputMediaAudio): |
| 76 | + if media.media.startswith("http"): |
| 77 | + media = types.InputMediaDocumentExternal( |
| 78 | + url=media.media |
| 79 | + ) |
| 80 | + else: |
| 81 | + media = utils.get_input_media_from_file_id(media.media, 9) |
| 82 | + elif isinstance(media, InputMediaAnimation): |
| 83 | + if media.media.startswith("http"): |
| 84 | + media = types.InputMediaDocumentExternal( |
| 85 | + url=media.media |
| 86 | + ) |
| 87 | + else: |
| 88 | + media = utils.get_input_media_from_file_id(media.media, 10) |
| 89 | + elif isinstance(media, InputMediaDocument): |
| 90 | + if media.media.startswith("http"): |
| 91 | + media = types.InputMediaDocumentExternal( |
| 92 | + url=media.media |
| 93 | + ) |
| 94 | + else: |
| 95 | + media = utils.get_input_media_from_file_id(media.media, 5) |
| 96 | + |
| 97 | + return self.send( |
| 98 | + functions.messages.EditInlineBotMessage( |
| 99 | + id=utils.unpack_inline_message_id(inline_message_id), |
| 100 | + media=media, |
| 101 | + reply_markup=reply_markup.write() if reply_markup else None, |
| 102 | + **style.parse(caption) |
| 103 | + ) |
| 104 | + ) |
0 commit comments