2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Add edit_inline_* methods to deal with inline messages only

This commit is contained in:
Dan 2019-06-14 04:52:05 +02:00
parent 3ed1bb0d86
commit 61ed44ff5f
11 changed files with 344 additions and 102 deletions

View File

@ -55,11 +55,15 @@ Messages
- :meth:`~Client.send_venue` - :meth:`~Client.send_venue`
- :meth:`~Client.send_contact` - :meth:`~Client.send_contact`
- :meth:`~Client.send_cached_media` - :meth:`~Client.send_cached_media`
- :meth:`~Client.send_chat_action`
- :meth:`~Client.edit_message_text` - :meth:`~Client.edit_message_text`
- :meth:`~Client.edit_message_caption` - :meth:`~Client.edit_message_caption`
- :meth:`~Client.edit_message_reply_markup`
- :meth:`~Client.edit_message_media` - :meth:`~Client.edit_message_media`
- :meth:`~Client.edit_message_reply_markup`
- :meth:`~Client.edit_inline_text`
- :meth:`~Client.edit_inline_caption`
- :meth:`~Client.edit_inline_media`
- :meth:`~Client.edit_inline_reply_markup`
- :meth:`~Client.send_chat_action`
- :meth:`~Client.delete_messages` - :meth:`~Client.delete_messages`
- :meth:`~Client.get_messages` - :meth:`~Client.get_messages`
- :meth:`~Client.get_history` - :meth:`~Client.get_history`
@ -203,8 +207,12 @@ Details
.. automethod:: Client.send_chat_action() .. automethod:: Client.send_chat_action()
.. automethod:: Client.edit_message_text() .. automethod:: Client.edit_message_text()
.. automethod:: Client.edit_message_caption() .. automethod:: Client.edit_message_caption()
.. automethod:: Client.edit_message_reply_markup()
.. automethod:: Client.edit_message_media() .. automethod:: Client.edit_message_media()
.. automethod:: Client.edit_message_reply_markup()
.. automethod:: Client.edit_inline_text()
.. automethod:: Client.edit_inline_caption()
.. automethod:: Client.edit_inline_media()
.. automethod:: Client.edit_inline_reply_markup()
.. automethod:: Client.delete_messages() .. automethod:: Client.delete_messages()
.. automethod:: Client.get_messages() .. automethod:: Client.get_messages()
.. automethod:: Client.get_history() .. automethod:: Client.get_history()

View File

@ -160,8 +160,17 @@ class BaseClient:
def edit_message_text(self, *args, **kwargs): def edit_message_text(self, *args, **kwargs):
pass pass
def edit_inline_text(self, *args, **kwargs):
pass
def edit_message_media(self, *args, **kwargs): def edit_message_media(self, *args, **kwargs):
pass pass
def edit_inline_media(self, *args, **kwargs):
pass
def edit_message_reply_markup(self, *args, **kwargs): def edit_message_reply_markup(self, *args, **kwargs):
pass pass
def edit_inline_reply_markup(self, *args, **kwargs):
pass

View File

@ -18,6 +18,10 @@
from .delete_messages import DeleteMessages from .delete_messages import DeleteMessages
from .download_media import DownloadMedia from .download_media import DownloadMedia
from .edit_inline_caption import EditInlineCaption
from .edit_inline_media import EditInlineMedia
from .edit_inline_reply_markup import EditInlineReplyMarkup
from .edit_inline_text import EditInlineText
from .edit_message_caption import EditMessageCaption from .edit_message_caption import EditMessageCaption
from .edit_message_media import EditMessageMedia from .edit_message_media import EditMessageMedia
from .edit_message_reply_markup import EditMessageReplyMarkup from .edit_message_reply_markup import EditMessageReplyMarkup
@ -82,6 +86,10 @@ class Messages(
SendCachedMedia, SendCachedMedia,
GetHistoryCount, GetHistoryCount,
SendAnimatedSticker, SendAnimatedSticker,
ReadHistory ReadHistory,
EditInlineText,
EditInlineCaption,
EditInlineMedia,
EditInlineReplyMarkup
): ):
pass pass

View File

@ -0,0 +1,58 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram.client.ext import BaseClient
class EditInlineCaption(BaseClient):
def edit_inline_caption(
self,
inline_message_id: str,
caption: str,
parse_mode: str = "",
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> bool:
"""Edit the caption of **inline** media messages.
Parameters:
inline_message_id (``str``):
Identifier of the inline message.
caption (``str``):
New caption of the media message.
parse_mode (``str``, *optional*):
Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline
URLs in your message. Defaults to "markdown".
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object.
Returns:
``bool``: On success, True is returned.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self.edit_inline_text(
inline_message_id=inline_message_id,
text=caption,
parse_mode=parse_mode,
reply_markup=reply_markup
)

View File

@ -0,0 +1,104 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils
from pyrogram.client.types import (
InputMediaPhoto, InputMediaVideo, InputMediaAudio,
InputMediaAnimation, InputMediaDocument
)
from pyrogram.client.types.input_media import InputMedia
class EditInlineMedia(BaseClient):
def edit_inline_media(
self,
inline_message_id: str,
media: InputMedia,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> bool:
"""Edit **inline** animation, audio, document, photo or video messages.
When the inline message is edited, a new file can't be uploaded. Use a previously uploaded file via its file_id
or specify a URL.
Parameters:
inline_message_id (``str``):
Required if *chat_id* and *message_id* are not specified.
Identifier of the inline message.
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:
``bool``: On success, True 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 media.media.startswith("http"):
media = types.InputMediaPhotoExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, 2)
elif isinstance(media, InputMediaVideo):
if media.media.startswith("http"):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, 4)
elif isinstance(media, InputMediaAudio):
if media.media.startswith("http"):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, 9)
elif isinstance(media, InputMediaAnimation):
if media.media.startswith("http"):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, 10)
elif isinstance(media, InputMediaDocument):
if media.media.startswith("http"):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, 5)
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
media=media,
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
)
)

View File

@ -0,0 +1,50 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram.api import functions
from pyrogram.client.ext import BaseClient, utils
class EditInlineReplyMarkup(BaseClient):
def edit_inline_reply_markup(
self,
inline_message_id: str,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> bool:
"""Edit only the reply markup of **inline** messages sent via the bot (for inline bots).
Parameters:
inline_message_id (``str``):
Identifier of the inline message.
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object.
Returns:
``bool``: On success, True is returned.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
reply_markup=reply_markup.write() if reply_markup else None,
)
)

View File

@ -0,0 +1,67 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram.api import functions
from pyrogram.client.ext import BaseClient, utils
class EditInlineText(BaseClient):
def edit_inline_text(
self,
inline_message_id: str,
text: str,
parse_mode: str = "",
disable_web_page_preview: bool = None,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> bool:
"""Edit the text of **inline** messages.
Parameters:
inline_message_id (``str``):
Identifier of the inline message.
text (``str``):
New text of the message.
parse_mode (``str``, *optional*):
Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline
URLs in your message. Defaults to "markdown".
disable_web_page_preview (``bool``, *optional*):
Disables link previews for links in this message.
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object.
Returns:
``bool``: On success, True is returned.
Raises:
RPCError: In case of a Telegram RPC error.
"""
style = self.html if parse_mode.lower() == "html" else self.markdown
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(text)
)
)

View File

@ -25,32 +25,25 @@ from pyrogram.client.ext import BaseClient
class EditMessageCaption(BaseClient): class EditMessageCaption(BaseClient):
def edit_message_caption( def edit_message_caption(
self, self,
chat_id: Union[int, str],
message_id: int,
caption: str, caption: str,
chat_id: Union[int, str] = None,
message_id: int = None,
inline_message_id: str = None,
parse_mode: str = "", parse_mode: str = "",
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> "pyrogram.Message": ) -> "pyrogram.Message":
"""Edit caption of media messages. """Edit the caption of media messages.
Parameters: Parameters:
caption (``str``): chat_id (``int`` | ``str``):
New caption of the media message.
chat_id (``int`` | ``str``, *optional*):
Required if *inline_message_id* is not specified.
Unique identifier (int) or username (str) of the target chat. Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self". 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 contact that exists in your Telegram address book you can use his phone number (str).
message_id (``int``, *optional*): message_id (``int``):
Required if *inline_message_id* is not specified.
Message identifier in the chat specified in chat_id. Message identifier in the chat specified in chat_id.
inline_message_id (``str``, *optional*): caption (``str``):
Required if *chat_id* and *message_id* are not specified. New caption of the media message.
Identifier of the inline message.
parse_mode (``str``, *optional*): parse_mode (``str``, *optional*):
Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline
@ -60,17 +53,15 @@ class EditMessageCaption(BaseClient):
An InlineKeyboardMarkup object. An InlineKeyboardMarkup object.
Returns: Returns:
:obj:`Message` | ``bool``: On success, if the edited message was sent by the bot, the edited message is :obj:`Message`: On success, the edited message is returned.
returned, otherwise True is returned (message sent via the bot, as inline query result).
Raises: Raises:
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
""" """
return self.edit_message_text( return self.edit_message_text(
text=caption,
chat_id=chat_id, chat_id=chat_id,
message_id=message_id, message_id=message_id,
inline_message_id=inline_message_id, text=caption,
parse_mode=parse_mode, parse_mode=parse_mode,
reply_markup=reply_markup reply_markup=reply_markup
) )

View File

@ -32,42 +32,33 @@ from pyrogram.client.types.input_media import InputMedia
class EditMessageMedia(BaseClient): class EditMessageMedia(BaseClient):
def edit_message_media( def edit_message_media(
self, self,
chat_id: Union[int, str],
message_id: int,
media: InputMedia, media: InputMedia,
chat_id: Union[int, str] = None,
message_id: int = None,
inline_message_id: str = None,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> "pyrogram.Message": ) -> "pyrogram.Message":
"""Edit animation, audio, document, photo or video messages. """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, 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. When inline message is edited, new file can't be uploaded. message type can be changed arbitrarily.
Use previously uploaded file via its file_id or specify a URL.
Parameters: Parameters:
media (:obj:`InputMedia`): chat_id (``int`` | ``str``):
One of the InputMedia objects describing an animation, audio, document, photo or video.
chat_id (``int`` | ``str``, *optional*):
Required if *inline_message_id* is not specified.
Unique identifier (int) or username (str) of the target chat. Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self". 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 contact that exists in your Telegram address book you can use his phone number (str).
message_id (``int``, *optional*): message_id (``int``):
Required if *inline_message_id* is not specified.
Message identifier in the chat specified in chat_id. Message identifier in the chat specified in chat_id.
inline_message_id (``str``, *optional*): media (:obj:`InputMedia`):
Required if *chat_id* and *message_id* are not specified. One of the InputMedia objects describing an animation, audio, document, photo or video.
Identifier of the inline message.
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object. An InlineKeyboardMarkup object.
Returns: Returns:
:obj:`Message` | ``bool``: On success, if the edited message was sent by the bot, the edited message is :obj:`Message`: On success, the edited message is returned.
returned, otherwise True is returned (message sent via the bot, as inline query result).
Raises: Raises:
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
@ -242,16 +233,6 @@ class EditMessageMedia(BaseClient):
else: else:
media = utils.get_input_media_from_file_id(media.media, 5) media = utils.get_input_media_from_file_id(media.media, 5)
if inline_message_id is not None:
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
media=media,
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
)
)
r = self.send( r = self.send(
functions.messages.EditMessage( functions.messages.EditMessage(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),

View File

@ -20,52 +20,36 @@ from typing import Union
import pyrogram import pyrogram
from pyrogram.api import functions, types from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils from pyrogram.client.ext import BaseClient
class EditMessageReplyMarkup(BaseClient): class EditMessageReplyMarkup(BaseClient):
def edit_message_reply_markup( def edit_message_reply_markup(
self, self,
chat_id: Union[int, str],
message_id: int,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None, reply_markup: "pyrogram.InlineKeyboardMarkup" = None,
chat_id: Union[int, str] = None,
message_id: int = None,
inline_message_id: str = None
) -> "pyrogram.Message": ) -> "pyrogram.Message":
"""Edit only the reply markup of messages sent by the bot or via the bot (for inline bots). """Edit only the reply markup of messages sent by the bot.
Parameters: Parameters:
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): chat_id (``int`` | ``str``):
An InlineKeyboardMarkup object.
chat_id (``int`` | ``str``, *optional*):
Required if *inline_message_id* is not specified.
Unique identifier (int) or username (str) of the target chat. Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self". 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 contact that exists in your Telegram address book you can use his phone number (str).
message_id (``int``, *optional*): message_id (``int``):
Required if *inline_message_id* is not specified.
Message identifier in the chat specified in chat_id. Message identifier in the chat specified in chat_id.
inline_message_id (``str``, *optional*): reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
Required if *chat_id* and *message_id* are not specified. An InlineKeyboardMarkup object.
Identifier of the inline message.
Returns: Returns:
:obj:`Message` | ``bool``: On success, if the edited message was sent by the bot, the edited message is :obj:`Message`: On success, the edited message is returned.
returned, otherwise True is returned (message sent via the bot, as inline query result).
Raises: Raises:
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
""" """
if inline_message_id is not None:
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
reply_markup=reply_markup.write() if reply_markup else None,
)
)
r = self.send( r = self.send(
functions.messages.EditMessage( functions.messages.EditMessage(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),

View File

@ -20,39 +20,32 @@ from typing import Union
import pyrogram import pyrogram
from pyrogram.api import functions, types from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils from pyrogram.client.ext import BaseClient
class EditMessageText(BaseClient): class EditMessageText(BaseClient):
def edit_message_text( def edit_message_text(
self, self,
chat_id: Union[int, str],
message_id: int,
text: str, text: str,
chat_id: Union[int, str] = None,
message_id: int = None,
inline_message_id: str = None,
parse_mode: str = "", parse_mode: str = "",
disable_web_page_preview: bool = None, disable_web_page_preview: bool = None,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> "pyrogram.Message": ) -> "pyrogram.Message":
"""Edit text messages. """Edit the text of messages.
Parameters: Parameters:
text (``str``): chat_id (``int`` | ``str``):
New text of the message.
chat_id (``int`` | ``str``, *optional*):
Required if *inline_message_id* is not specified.
Unique identifier (int) or username (str) of the target chat. Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self". 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 contact that exists in your Telegram address book you can use his phone number (str).
message_id (``int``, *optional*): message_id (``int``):
Required if *inline_message_id* is not specified.
Message identifier in the chat specified in chat_id. Message identifier in the chat specified in chat_id.
inline_message_id (``str``, *optional*): text (``str``):
Required if *chat_id* and *message_id* are not specified. New text of the message.
Identifier of the inline message.
parse_mode (``str``, *optional*): parse_mode (``str``, *optional*):
Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline
@ -65,24 +58,13 @@ class EditMessageText(BaseClient):
An InlineKeyboardMarkup object. An InlineKeyboardMarkup object.
Returns: Returns:
:obj:`Message` | ``bool``: On success, if the edited message was sent by the bot, the edited message is :obj:`Message`: On success, the edited message is returned.
returned, otherwise True is returned (message sent via the bot, as inline query result).
Raises: Raises:
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
""" """
style = self.html if parse_mode.lower() == "html" else self.markdown style = self.html if parse_mode.lower() == "html" else self.markdown
if inline_message_id is not None:
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(text)
)
)
r = self.send( r = self.send(
functions.messages.EditMessage( functions.messages.EditMessage(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),