Skip to content

Commit

Permalink
Ask for the phone on start only if required
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jan 15, 2018
1 parent 8be7e76 commit 00859d5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions telethon/telegram_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def send_code_request(self, phone, force_sms=False):

return result

def start(self, phone=None, password=None, bot_token=None,
def start(self,
phone=lambda: input('Please enter your phone: '),
password=None, bot_token=None,
force_sms=False, code_callback=None):
"""
Convenience method to interactively connect and sign in if required,
Expand All @@ -198,8 +200,9 @@ def start(self, phone=None, password=None, bot_token=None,
(You are now logged in)
Args:
phone (:obj:`str` | :obj:`int`):
The phone to which the code will be sent.
phone (:obj:`str` | :obj:`int` | :obj:`callable`):
The phone (or callable without arguments to get it)
to which the code will be sent.
password (:obj:`callable`, optional):
The password for 2 Factor Authentication (2FA).
Expand Down Expand Up @@ -232,14 +235,11 @@ def code_callback():
)

if not phone and not bot_token:
while not phone:
phone = utils.parse_phone(input('Please enter your phone: '))
raise ValueError('No phone number or bot token provided.')

elif phone and bot_token:
raise ValueError(
'You must provide either a phone number or a bot token, '
'not both (or neither).'
)
if phone and bot_token:
raise ValueError('Both a phone and a bot token provided, '
'must only provide one of either')

if not self.is_connected():
self.connect()
Expand All @@ -251,6 +251,10 @@ def code_callback():
self.sign_in(bot_token=bot_token)
return self

# Turn the callable into a valid phone number
while callable(phone):
phone = utils.parse_phone(phone()) or phone

me = None
attempts = 0
max_attempts = 3
Expand Down

0 comments on commit 00859d5

Please sign in to comment.