Skip to content

Commit

Permalink
Support clicking buttons that require password
Browse files Browse the repository at this point in the history
Should close #1716.
  • Loading branch information
Lonami committed Mar 7, 2021
1 parent 3ee94bd commit bfa7e4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
27 changes: 14 additions & 13 deletions telethon/tl/custom/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ async def download_media(self, *args, **kwargs):

async def click(self, i=None, j=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
or `button.click <telethon.tl.custom.messagebutton.MessageButton.click>`
Expand Down Expand Up @@ -911,6 +911,12 @@ async def click(self, i=None, j=None,
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:
.. code-block:: python
Expand All @@ -934,19 +940,13 @@ async def click(self, i=None, j=None,
return

if data:
if not await self.get_input_chat():
chat = await self.get_input_chat()
if not chat:
return None

try:
return await self._client(
functions.messages.GetBotCallbackAnswerRequest(
peer=self._input_chat,
msg_id=self.id,
data=data
)
)
except errors.BotResponseTimeoutError:
return None
but = types.KeyboardButtonCallback('', data)
return await MessageButton(self._client, but, chat, None, self.id).click(
share_phone=share_phone, share_geo=share_geo, password=password)

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')
Expand Down Expand Up @@ -1018,7 +1018,8 @@ def find_button():

button = find_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):
"""
Expand Down
11 changes: 9 additions & 2 deletions telethon/tl/custom/messagebutton.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .. import types, functions
from ... import password as pwd_mod
from ...errors import BotResponseTimeoutError
import webbrowser
import os


class MessageButton:
Expand Down Expand Up @@ -59,7 +61,7 @@ def url(self):
if isinstance(self.button, types.KeyboardButtonUrl):
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.
Expand Down Expand Up @@ -93,8 +95,13 @@ async def click(self, share_phone=None, share_geo=None):
return await self._client.send_message(
self._chat, self.button.text, parse_mode=None)
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(
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:
return await self._client(req)
Expand Down

0 comments on commit bfa7e4c

Please sign in to comment.