Telethon/telethon/tl/custom/messagebutton.py

147 lines
5.9 KiB
Python
Raw Normal View History

2018-05-31 11:32:32 +03:00
from .. import types, functions
from ... import password as pwd_mod
from ...errors import BotResponseTimeoutError
2018-05-31 11:32:32 +03:00
import webbrowser
import os
2018-05-31 11:32:32 +03:00
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."""
2018-05-31 11:32:32 +03:00
if isinstance(self.button, types.KeyboardButtonCallback):
return self.button.data
@property
def inline_query(self):
"""The query `str` for :tl:`KeyboardButtonSwitchInline` objects."""
2018-05-31 11:32:32 +03:00
if isinstance(self.button, types.KeyboardButtonSwitchInline):
return self.button.query
@property
def url(self):
"""The url `str` for :tl:`KeyboardButtonUrl` objects."""
2018-05-31 11:32:32 +03:00
if isinstance(self.button, types.KeyboardButtonUrl):
return self.button.url
async def click(self, share_phone=None, share_geo=None, *, password=None):
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 `Message <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.
If it's a :tl:`KeyboardButtonRequestPhone`, you must indicate that you
want to ``share_phone=True`` in order to share it. Sharing it is not a
default because it is a privacy concern and could happen accidentally.
You may also use ``share_phone=phone`` to share a specific number, in
which case either `str` or :tl:`InputMediaContact` should be used.
If it's a :tl:`KeyboardButtonRequestGeoLocation`, you must pass a
tuple in ``share_geo=(longitude, latitude)``. Note that Telegram seems
to have some heuristics to determine impossible locations, so changing
this value a lot quickly may not work as expected. You may also pass a
:tl:`InputGeoPoint` if you find the order confusing.
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(
self._chat, self.button.text, parse_mode=None)
2018-05-31 11:32:32 +03:00
elif isinstance(self.button, types.KeyboardButtonCallback):
if password is not None:
pwd = await self._client(functions.account.GetPasswordRequest())
password = pwd_mod.compute_check(pwd, password)
2018-06-10 21:29:57 +03:00
req = functions.messages.GetBotCallbackAnswerRequest(
peer=self._chat, msg_id=self._msg_id, data=self.button.data,
password=password
2018-06-10 21:29:57 +03:00
)
try:
return await self._client(req)
except BotResponseTimeoutError:
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 BotResponseTimeoutError:
2018-12-24 19:56:30 +03:00
return None
elif isinstance(self.button, types.KeyboardButtonRequestPhone):
if not share_phone:
raise ValueError('cannot click on phone buttons unless share_phone=True')
if share_phone == True or isinstance(share_phone, str):
me = await self._client.get_me()
share_phone = types.InputMediaContact(
phone_number=me.phone if share_phone == True else share_phone,
first_name=me.first_name or '',
last_name=me.last_name or '',
vcard=''
)
return await self._client.send_file(self._chat, share_phone)
elif isinstance(self.button, types.KeyboardButtonRequestGeoLocation):
if not share_geo:
raise ValueError('cannot click on geo buttons unless share_geo=(longitude, latitude)')
if isinstance(share_geo, (tuple, list)):
long, lat = share_geo
share_geo = types.InputMediaGeoPoint(types.InputGeoPoint(lat=lat, long=long))
return await self._client.send_file(self._chat, share_geo)