Make .start() more friendly by asking phone if not given

Ping #530
This commit is contained in:
Lonami Exo 2018-01-13 11:54:41 +01:00
parent ef3ea11e38
commit 77301378f8
3 changed files with 13 additions and 4 deletions

View File

@ -31,7 +31,6 @@ one is very simple:
# Use your own values here # Use your own values here
api_id = 12345 api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef' api_hash = '0123456789abcdef0123456789abcdef'
phone_number = '+34600000000'
client = TelegramClient('some_name', api_id, api_hash) client = TelegramClient('some_name', api_id, api_hash)
@ -54,6 +53,7 @@ If you're not authorized, you need to ``.sign_in()``:
.. code-block:: python .. code-block:: python
phone_number = '+34600000000'
client.send_code_request(phone_number) client.send_code_request(phone_number)
myself = client.sign_in(phone_number, input('Enter code: ')) myself = client.sign_in(phone_number, input('Enter code: '))
# If .sign_in raises PhoneNumberUnoccupiedError, use .sign_up instead # If .sign_in raises PhoneNumberUnoccupiedError, use .sign_up instead
@ -86,7 +86,9 @@ All of this, however, can be done through a call to ``.start()``:
The code shown is just what ``.start()`` will be doing behind the scenes The code shown is just what ``.start()`` will be doing behind the scenes
(with a few extra checks), so that you know how to sign in case you want (with a few extra checks), so that you know how to sign in case you want
to avoid using ``input()`` (the default) for whatever reason. to avoid using ``input()`` (the default) for whatever reason. If no phone
or bot token is provided, you will be asked one through ``input()``. The
method also accepts a ``phone=`` and ``bot_token`` parameters.
You can use either, as both will work. Determining which You can use either, as both will work. Determining which
is just a matter of taste, and how much control you need. is just a matter of taste, and how much control you need.

View File

@ -27,7 +27,6 @@ Creating a client
# api_hash from https://my.telegram.org, under API Development. # api_hash from https://my.telegram.org, under API Development.
api_id = 12345 api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef' api_hash = '0123456789abcdef0123456789abcdef'
phone = '+34600000000'
client = TelegramClient('session_name', api_id, api_hash) client = TelegramClient('session_name', api_id, api_hash)
client.start() client.start()

View File

@ -231,7 +231,15 @@ class TelegramClient(TelegramBareClient):
'function that returns the code you received by Telegram.' 'function that returns the code you received by Telegram.'
) )
if (phone and bot_token) or (not phone and not bot_token): if not phone and not bot_token:
value = input('Please enter your phone/bot token: ')
phone = utils.parse_phone(phone)
if not phone:
bot_token = value
print("Note: input doesn't look like a phone, "
"using as bot token")
if phone and bot_token:
raise ValueError( raise ValueError(
'You must provide either a phone number or a bot token, ' 'You must provide either a phone number or a bot token, '
'not both (or neither).' 'not both (or neither).'