Let .sign_in() call .send_code_request()

This commit is contained in:
Lonami 2017-08-31 10:34:09 +02:00 committed by GitHub
parent 8bff10d956
commit 5f636fdf31
3 changed files with 22 additions and 17 deletions

View File

@ -92,8 +92,8 @@ If you've installed Telethon via pip, launch an interactive python3 session and
True True
>>> >>>
>>> if not client.is_user_authorized(): >>> if not client.is_user_authorized():
>>> client.send_code_request('+34600000000') >>> client.sign_in(code='+34600000000')
>>> client.sign_in('+34600000000', input('Enter code: ')) >>> client.sign_in(code=input('Enter code: '))
... ...
>>> # Now you can use the connected client as you wish >>> # Now you can use the connected client as you wish
>>> dialogs, entities = client.get_dialogs(10) >>> dialogs, entities = client.get_dialogs(10)

View File

@ -32,8 +32,8 @@ Creating a client
client.connect() client.connect()
# If you already have a previous 'session_name.session' file, skip this. # If you already have a previous 'session_name.session' file, skip this.
client.send_code_request(phone) client.sign_in(phone=phone)
me = client.sign_in(phone, 77777) # Put whatever code you received here. me = client.sign_in(code=77777) # Put whatever code you received here.
Doing stuff Doing stuff

View File

@ -113,7 +113,8 @@ class TelegramClient(TelegramBareClient):
setattr(self.session, name, value) setattr(self.session, name, value)
self._updates_thread = None self._updates_thread = None
self._phone_code_hashes = {} self._phone_code_hash = None
self._phone = None
# Uploaded files cache so subsequent calls are instant # Uploaded files cache so subsequent calls are instant
self._upload_cache = {} self._upload_cache = {}
@ -242,14 +243,15 @@ class TelegramClient(TelegramBareClient):
(code request sent and confirmed)?""" (code request sent and confirmed)?"""
return self.session and self.get_me() is not None return self.session and self.get_me() is not None
def send_code_request(self, phone_number): def send_code_request(self, phone):
"""Sends a code request to the specified phone number""" """Sends a code request to the specified phone number"""
result = self( result = self(
SendCodeRequest(phone_number, self.api_id, self.api_hash)) SendCodeRequest(phone, self.api_id, self.api_hash))
self._phone = phone
self._phone_code_hash = result.phone_code_hash
return result
self._phone_code_hashes[phone_number] = result.phone_code_hash def sign_in(self, phone=None, code=None,
def sign_in(self, phone_number=None, code=None,
password=None, bot_token=None): password=None, bot_token=None):
"""Completes the sign in process with the phone number + code pair. """Completes the sign in process with the phone number + code pair.
@ -264,8 +266,11 @@ class TelegramClient(TelegramBareClient):
If the login succeeds, the logged in user is returned. If the login succeeds, the logged in user is returned.
""" """
if phone_number and code:
if phone_number not in self._phone_code_hashes: if phone:
return self.send_code_request(phone)
elif code:
if self._phone == None:
raise ValueError( raise ValueError(
'Please make sure to call send_code_request first.') 'Please make sure to call send_code_request first.')
@ -273,7 +278,7 @@ class TelegramClient(TelegramBareClient):
if isinstance(code, int): if isinstance(code, int):
code = str(code) code = str(code)
result = self(SignInRequest( result = self(SignInRequest(
phone_number, self._phone_code_hashes[phone_number], code self._phone, self._phone_code_hash, code
)) ))
except (PhoneCodeEmptyError, PhoneCodeExpiredError, except (PhoneCodeEmptyError, PhoneCodeExpiredError,
@ -292,17 +297,17 @@ class TelegramClient(TelegramBareClient):
else: else:
raise ValueError( raise ValueError(
'You must provide a phone_number and a code the first time, ' 'You must provide a phone and a code the first time, '
'and a password only if an RPCError was raised before.') 'and a password only if an RPCError was raised before.')
return result.user return result.user
def sign_up(self, phone_number, code, first_name, last_name=''): def sign_up(self, code, first_name, last_name=''):
"""Signs up to Telegram. Make sure you sent a code request first!""" """Signs up to Telegram. Make sure you sent a code request first!"""
result = self( result = self(
SignUpRequest( SignUpRequest(
phone_number=phone_number, phone=self._phone,
phone_code_hash=self._phone_code_hashes[phone_number], phone_code_hash=self._phone_code_hash,
phone_code=code, phone_code=code,
first_name=first_name, first_name=first_name,
last_name=last_name)) last_name=last_name))