Support clicking buttons that require password

Should close #1716.
This commit is contained in:
Lonami Exo 2021-03-07 16:36:26 +01:00
parent 3ee94bdc5e
commit bfa7e4ca37
2 changed files with 23 additions and 15 deletions

View File

@ -832,7 +832,7 @@ class Message(ChatGetter, SenderGetter, TLObject):
async def click(self, i=None, j=None, async def click(self, i=None, j=None,
*, text=None, filter=None, data=None, share_phone=None, *, text=None, filter=None, data=None, share_phone=None,
share_geo=None): share_geo=None, password=None):
""" """
Calls :tl:`SendVote` with the specified poll option Calls :tl:`SendVote` with the specified poll option
or `button.click <telethon.tl.custom.messagebutton.MessageButton.click>` or `button.click <telethon.tl.custom.messagebutton.MessageButton.click>`
@ -911,6 +911,12 @@ class Message(ChatGetter, SenderGetter, TLObject):
If the button is pressed without this, `ValueError` is raised. If the button is pressed without this, `ValueError` is raised.
password (`str`):
When clicking certain buttons (such as BotFather's confirmation
button to transfer ownership), if your account has 2FA enabled,
you need to provide your account's password. Otherwise,
`teltehon.errors.PasswordHashInvalidError` is raised.
Example: Example:
.. code-block:: python .. code-block:: python
@ -934,19 +940,13 @@ class Message(ChatGetter, SenderGetter, TLObject):
return return
if data: if data:
if not await self.get_input_chat(): chat = await self.get_input_chat()
if not chat:
return None return None
try: but = types.KeyboardButtonCallback('', data)
return await self._client( return await MessageButton(self._client, but, chat, None, self.id).click(
functions.messages.GetBotCallbackAnswerRequest( share_phone=share_phone, share_geo=share_geo, password=password)
peer=self._input_chat,
msg_id=self.id,
data=data
)
)
except errors.BotResponseTimeoutError:
return None
if sum(int(x is not None) for x in (i, text, filter)) >= 2: if sum(int(x is not None) for x in (i, text, filter)) >= 2:
raise ValueError('You can only set either of i, text or filter') raise ValueError('You can only set either of i, text or filter')
@ -1018,7 +1018,8 @@ class Message(ChatGetter, SenderGetter, TLObject):
button = find_button() button = find_button()
if button: if button:
return await button.click(share_phone=share_phone, share_geo=share_geo) return await button.click(
share_phone=share_phone, share_geo=share_geo, password=password)
async def mark_read(self): async def mark_read(self):
""" """

View File

@ -1,6 +1,8 @@
from .. import types, functions from .. import types, functions
from ... import password as pwd_mod
from ...errors import BotResponseTimeoutError from ...errors import BotResponseTimeoutError
import webbrowser import webbrowser
import os
class MessageButton: class MessageButton:
@ -59,7 +61,7 @@ class MessageButton:
if isinstance(self.button, types.KeyboardButtonUrl): if isinstance(self.button, types.KeyboardButtonUrl):
return self.button.url return self.button.url
async def click(self, share_phone=None, share_geo=None): async def click(self, share_phone=None, share_geo=None, *, password=None):
""" """
Emulates the behaviour of clicking this button. Emulates the behaviour of clicking this button.
@ -93,8 +95,13 @@ class MessageButton:
return await self._client.send_message( return await self._client.send_message(
self._chat, self.button.text, parse_mode=None) self._chat, self.button.text, parse_mode=None)
elif isinstance(self.button, types.KeyboardButtonCallback): 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)
req = functions.messages.GetBotCallbackAnswerRequest( req = functions.messages.GetBotCallbackAnswerRequest(
peer=self._chat, msg_id=self._msg_id, data=self.button.data peer=self._chat, msg_id=self._msg_id, data=self.button.data,
password=password
) )
try: try:
return await self._client(req) return await self._client(req)