From a50d013ee6e1b474ab639d7143a64a8b696fbee8 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 10 Jul 2018 10:21:15 +0200 Subject: [PATCH] Support interactively signing in as a bot --- telethon/client/auth.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/telethon/client/auth.py b/telethon/client/auth.py index 90d3da05..8ed62b5a 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -16,7 +16,7 @@ class AuthMethods(MessageParseMethods, UserMethods): def start( self, - phone=lambda: input('Please enter your phone: '), + phone=lambda: input('Please enter your phone (or bot token): '), password=lambda: getpass.getpass('Please enter your password: '), *, bot_token=None, force_sms=False, code_callback=None, @@ -45,7 +45,8 @@ class AuthMethods(MessageParseMethods, UserMethods): Args: phone (`str` | `int` | `callable`): The phone (or callable without arguments to get it) - to which the code will be sent. + to which the code will be sent. If a bot-token-like + string is given, it will be used as such instead. password (`callable`, optional): The password for 2 Factor Authentication (2FA). @@ -119,14 +120,21 @@ class AuthMethods(MessageParseMethods, UserMethods): if await self.is_user_authorized(): return self + if not bot_token: + # Turn the callable into a valid phone number (or bot token) + while callable(phone): + value = phone() + if ':' in value: + # Bot tokens have 'user_id:access_hash' format + bot_token = value + break + + phone = utils.parse_phone(value) or phone + if bot_token: await 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 two_step_detected = False