Skip to content

Commit

Permalink
Make .start() more friendly by asking phone if not given
Browse files Browse the repository at this point in the history
Ping #530
  • Loading branch information
Lonami committed Jan 13, 2018
1 parent ef3ea11 commit 7730137
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions readthedocs/extra/basic/creating-a-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ one is very simple:
# Use your own values here
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
phone_number = '+34600000000'
client = TelegramClient('some_name', api_id, api_hash)
Expand All @@ -54,6 +53,7 @@ If you're not authorized, you need to ``.sign_in()``:

.. code-block:: python
phone_number = '+34600000000'
client.send_code_request(phone_number)
myself = client.sign_in(phone_number, input('Enter code: '))
# If .sign_in raises PhoneNumberUnoccupiedError, use .sign_up instead
Expand Down Expand Up @@ -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
(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
is just a matter of taste, and how much control you need.
Expand Down
1 change: 0 additions & 1 deletion readthedocs/extra/basic/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Creating a client
# api_hash from https://my.telegram.org, under API Development.
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
phone = '+34600000000'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
Expand Down
10 changes: 9 additions & 1 deletion telethon/telegram_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ def code_callback():
'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(
'You must provide either a phone number or a bot token, '
'not both (or neither).'
Expand Down

0 comments on commit 7730137

Please sign in to comment.