Telethon/telethon/tl/custom/messagebutton.py

104 lines
3.7 KiB
Python
Raw Normal View History

2018-05-31 11:32:32 +03:00
from .. import types, functions
from ...errors import BotTimeout
2018-05-31 11:32:32 +03:00
import webbrowser
class MessageButton:
"""
2018-09-04 12:27:10 +03:00
.. note::
`Message.buttons <telethon.tl.custom.message.Message.buttons>`
are instances of this type. If you want to **define** a reply
markup for e.g. sending messages, refer to `Button
<telethon.tl.custom.button.Button>` instead.
Custom class that encapsulates a message button providing
an abstraction to easily access some commonly needed features
(such as clicking the button itself).
2018-05-31 11:32:32 +03:00
Attributes:
button (:tl:`KeyboardButton`):
The original :tl:`KeyboardButton` object.
"""
def __init__(self, client, original, chat, bot, msg_id):
2018-05-31 11:32:32 +03:00
self.button = original
self._bot = bot
2018-05-31 11:32:32 +03:00
self._chat = chat
self._msg_id = msg_id
self._client = client
@property
def client(self):
2018-06-02 13:52:38 +03:00
"""
2018-10-17 12:47:51 +03:00
Returns the `telethon.client.telegramclient.TelegramClient`
instance that created this instance.
2018-06-02 13:52:38 +03:00
"""
2018-05-31 11:32:32 +03:00
return self._client
@property
def text(self):
"""The text string of the button."""
return self.button.text
@property
def data(self):
"""The ``bytes`` data for :tl:`KeyboardButtonCallback` objects."""
if isinstance(self.button, types.KeyboardButtonCallback):
return self.button.data
@property
def inline_query(self):
"""The query ``str`` for :tl:`KeyboardButtonSwitchInline` objects."""
if isinstance(self.button, types.KeyboardButtonSwitchInline):
return self.button.query
@property
def url(self):
"""The url ``str`` for :tl:`KeyboardButtonUrl` objects."""
if isinstance(self.button, types.KeyboardButtonUrl):
return self.button.url
2018-06-10 21:29:57 +03:00
async def click(self):
2018-05-31 11:32:32 +03:00
"""
Emulates the behaviour of clicking this button.
2018-05-31 11:32:32 +03:00
If it's a normal :tl:`KeyboardButton` with text, a message will be
sent, and the sent `telethon.tl.custom.message.Message` returned.
If it's an inline :tl:`KeyboardButtonCallback` with text and data,
it will be "clicked" and the :tl:`BotCallbackAnswer` returned.
If it's an inline :tl:`KeyboardButtonSwitchInline` button, the
:tl:`StartBotRequest` will be invoked and the resulting updates
returned.
If it's a :tl:`KeyboardButtonUrl`, the URL of the button will
be passed to ``webbrowser.open`` and return ``True`` on success.
2018-05-31 11:32:32 +03:00
"""
if isinstance(self.button, types.KeyboardButton):
2018-06-10 21:29:57 +03:00
return await self._client.send_message(
2018-05-31 11:32:32 +03:00
self._chat, self.button.text, reply_to=self._msg_id)
elif isinstance(self.button, types.KeyboardButtonCallback):
2018-06-10 21:29:57 +03:00
req = functions.messages.GetBotCallbackAnswerRequest(
2018-05-31 11:32:32 +03:00
peer=self._chat, msg_id=self._msg_id, data=self.button.data
2018-06-10 21:29:57 +03:00
)
try:
return await self._client(req)
except BotTimeout:
return None
2018-05-31 11:32:32 +03:00
elif isinstance(self.button, types.KeyboardButtonSwitchInline):
return await self._client(functions.messages.StartBotRequest(
bot=self._bot, peer=self._chat, start_param=self.button.query
))
2018-05-31 11:32:32 +03:00
elif isinstance(self.button, types.KeyboardButtonUrl):
return webbrowser.open(self.button.url)
2018-12-24 19:56:30 +03:00
elif isinstance(self.button, types.KeyboardButtonGame):
req = functions.messages.GetBotCallbackAnswerRequest(
peer=self._chat, msg_id=self._msg_id, game=True
)
try:
return await self._client(req)
except BotTimeout:
return None