Skip to content

Commit 61ed44f

Browse files
committed
Add edit_inline_* methods to deal with inline messages only
1 parent 3ed1bb0 commit 61ed44f

11 files changed

Lines changed: 344 additions & 102 deletions

docs/source/api/methods.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ Messages
5555
- :meth:`~Client.send_venue`
5656
- :meth:`~Client.send_contact`
5757
- :meth:`~Client.send_cached_media`
58-
- :meth:`~Client.send_chat_action`
5958
- :meth:`~Client.edit_message_text`
6059
- :meth:`~Client.edit_message_caption`
61-
- :meth:`~Client.edit_message_reply_markup`
6260
- :meth:`~Client.edit_message_media`
61+
- :meth:`~Client.edit_message_reply_markup`
62+
- :meth:`~Client.edit_inline_text`
63+
- :meth:`~Client.edit_inline_caption`
64+
- :meth:`~Client.edit_inline_media`
65+
- :meth:`~Client.edit_inline_reply_markup`
66+
- :meth:`~Client.send_chat_action`
6367
- :meth:`~Client.delete_messages`
6468
- :meth:`~Client.get_messages`
6569
- :meth:`~Client.get_history`
@@ -203,8 +207,12 @@ Details
203207
.. automethod:: Client.send_chat_action()
204208
.. automethod:: Client.edit_message_text()
205209
.. automethod:: Client.edit_message_caption()
206-
.. automethod:: Client.edit_message_reply_markup()
207210
.. automethod:: Client.edit_message_media()
211+
.. automethod:: Client.edit_message_reply_markup()
212+
.. automethod:: Client.edit_inline_text()
213+
.. automethod:: Client.edit_inline_caption()
214+
.. automethod:: Client.edit_inline_media()
215+
.. automethod:: Client.edit_inline_reply_markup()
208216
.. automethod:: Client.delete_messages()
209217
.. automethod:: Client.get_messages()
210218
.. automethod:: Client.get_history()

pyrogram/client/ext/base_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,17 @@ def get_profile_photos(self, *args, **kwargs):
160160
def edit_message_text(self, *args, **kwargs):
161161
pass
162162

163+
def edit_inline_text(self, *args, **kwargs):
164+
pass
165+
163166
def edit_message_media(self, *args, **kwargs):
164167
pass
165168

169+
def edit_inline_media(self, *args, **kwargs):
170+
pass
171+
166172
def edit_message_reply_markup(self, *args, **kwargs):
167173
pass
174+
175+
def edit_inline_reply_markup(self, *args, **kwargs):
176+
pass

pyrogram/client/methods/messages/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
from .delete_messages import DeleteMessages
2020
from .download_media import DownloadMedia
21+
from .edit_inline_caption import EditInlineCaption
22+
from .edit_inline_media import EditInlineMedia
23+
from .edit_inline_reply_markup import EditInlineReplyMarkup
24+
from .edit_inline_text import EditInlineText
2125
from .edit_message_caption import EditMessageCaption
2226
from .edit_message_media import EditMessageMedia
2327
from .edit_message_reply_markup import EditMessageReplyMarkup
@@ -82,6 +86,10 @@ class Messages(
8286
SendCachedMedia,
8387
GetHistoryCount,
8488
SendAnimatedSticker,
85-
ReadHistory
89+
ReadHistory,
90+
EditInlineText,
91+
EditInlineCaption,
92+
EditInlineMedia,
93+
EditInlineReplyMarkup
8694
):
8795
pass
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.client.ext import BaseClient
21+
22+
23+
class EditInlineCaption(BaseClient):
24+
def edit_inline_caption(
25+
self,
26+
inline_message_id: str,
27+
caption: str,
28+
parse_mode: str = "",
29+
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
30+
) -> bool:
31+
"""Edit the caption of **inline** media messages.
32+
33+
Parameters:
34+
inline_message_id (``str``):
35+
Identifier of the inline message.
36+
37+
caption (``str``):
38+
New caption of the media message.
39+
40+
parse_mode (``str``, *optional*):
41+
Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline
42+
URLs in your message. Defaults to "markdown".
43+
44+
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
45+
An InlineKeyboardMarkup object.
46+
47+
Returns:
48+
``bool``: On success, True is returned.
49+
50+
Raises:
51+
RPCError: In case of a Telegram RPC error.
52+
"""
53+
return self.edit_inline_text(
54+
inline_message_id=inline_message_id,
55+
text=caption,
56+
parse_mode=parse_mode,
57+
reply_markup=reply_markup
58+
)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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
21+
from pyrogram.client.ext import BaseClient, utils
22+
23+
24+
class EditInlineReplyMarkup(BaseClient):
25+
def edit_inline_reply_markup(
26+
self,
27+
inline_message_id: str,
28+
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
29+
) -> bool:
30+
"""Edit only the reply markup of **inline** messages sent via the bot (for inline bots).
31+
32+
Parameters:
33+
inline_message_id (``str``):
34+
Identifier of the inline message.
35+
36+
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
37+
An InlineKeyboardMarkup object.
38+
39+
Returns:
40+
``bool``: On success, True is returned.
41+
42+
Raises:
43+
RPCError: In case of a Telegram RPC error.
44+
"""
45+
return self.send(
46+
functions.messages.EditInlineBotMessage(
47+
id=utils.unpack_inline_message_id(inline_message_id),
48+
reply_markup=reply_markup.write() if reply_markup else None,
49+
)
50+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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
21+
from pyrogram.client.ext import BaseClient, utils
22+
23+
24+
class EditInlineText(BaseClient):
25+
def edit_inline_text(
26+
self,
27+
inline_message_id: str,
28+
text: str,
29+
parse_mode: str = "",
30+
disable_web_page_preview: bool = None,
31+
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
32+
) -> bool:
33+
"""Edit the text of **inline** messages.
34+
35+
Parameters:
36+
inline_message_id (``str``):
37+
Identifier of the inline message.
38+
39+
text (``str``):
40+
New text of the message.
41+
42+
parse_mode (``str``, *optional*):
43+
Pass "markdown" or "html" if you want Telegram apps to show bold, italic, fixed-width text or inline
44+
URLs in your message. Defaults to "markdown".
45+
46+
disable_web_page_preview (``bool``, *optional*):
47+
Disables link previews for links in this message.
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 parse_mode.lower() == "html" else self.markdown
59+
60+
return self.send(
61+
functions.messages.EditInlineBotMessage(
62+
id=utils.unpack_inline_message_id(inline_message_id),
63+
no_webpage=disable_web_page_preview or None,
64+
reply_markup=reply_markup.write() if reply_markup else None,
65+
**style.parse(text)
66+
)
67+
)

0 commit comments

Comments
 (0)