Ask for the phone on start only if required

This commit is contained in:
Lonami Exo 2018-01-15 09:48:37 +01:00
parent 8be7e76b74
commit 00859d52c3

View File

@ -185,7 +185,9 @@ class TelegramClient(TelegramBareClient):
return result 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): force_sms=False, code_callback=None):
""" """
Convenience method to interactively connect and sign in if required, Convenience method to interactively connect and sign in if required,
@ -198,8 +200,9 @@ class TelegramClient(TelegramBareClient):
(You are now logged in) (You are now logged in)
Args: Args:
phone (:obj:`str` | :obj:`int`): phone (:obj:`str` | :obj:`int` | :obj:`callable`):
The phone to which the code will be sent. The phone (or callable without arguments to get it)
to which the code will be sent.
password (:obj:`callable`, optional): password (:obj:`callable`, optional):
The password for 2 Factor Authentication (2FA). The password for 2 Factor Authentication (2FA).
@ -232,14 +235,11 @@ class TelegramClient(TelegramBareClient):
) )
if not phone and not bot_token: if not phone and not bot_token:
while not phone: raise ValueError('No phone number or bot token provided.')
phone = utils.parse_phone(input('Please enter your phone: '))
elif phone and bot_token: if phone and bot_token:
raise ValueError( raise ValueError('Both a phone and a bot token provided, '
'You must provide either a phone number or a bot token, ' 'must only provide one of either')
'not both (or neither).'
)
if not self.is_connected(): if not self.is_connected():
self.connect() self.connect()
@ -251,6 +251,10 @@ class TelegramClient(TelegramBareClient):
self.sign_in(bot_token=bot_token) self.sign_in(bot_token=bot_token)
return self return self
# Turn the callable into a valid phone number
while callable(phone):
phone = utils.parse_phone(phone()) or phone
me = None me = None
attempts = 0 attempts = 0
max_attempts = 3 max_attempts = 3