From 77301378f85ba4976c02e7e5704cdde88cd501a0 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 13 Jan 2018 11:54:41 +0100 Subject: [PATCH] Make .start() more friendly by asking phone if not given Ping #530 --- readthedocs/extra/basic/creating-a-client.rst | 6 ++++-- readthedocs/extra/basic/getting-started.rst | 1 - telethon/telegram_client.py | 10 +++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/readthedocs/extra/basic/creating-a-client.rst b/readthedocs/extra/basic/creating-a-client.rst index dd468abc..10ae5f60 100644 --- a/readthedocs/extra/basic/creating-a-client.rst +++ b/readthedocs/extra/basic/creating-a-client.rst @@ -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) @@ -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 @@ -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. diff --git a/readthedocs/extra/basic/getting-started.rst b/readthedocs/extra/basic/getting-started.rst index 912ea768..e69cc3ef 100644 --- a/readthedocs/extra/basic/getting-started.rst +++ b/readthedocs/extra/basic/getting-started.rst @@ -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() diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 9134feef..98b22940 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -231,7 +231,15 @@ class TelegramClient(TelegramBareClient): '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).'