2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Support custom callbacks on Client.authorize()

This commit is contained in:
Lonami Exo 2018-01-24 15:40:39 +01:00
parent 3e5421f55f
commit d01d852dc2

View File

@ -170,8 +170,10 @@ class Client:
""" """
return self.session.send(data) return self.session.send(data)
def authorize(self): def authorize(self, phone_number=None, code_callback=None, password=None):
invalid_phone_raises = phone_number is not None
while True: while True:
if phone_number is None:
phone_number = input("Enter phone number: ") phone_number = input("Enter phone number: ")
while True: while True:
@ -208,7 +210,11 @@ class Client:
) )
break break
except PhoneNumberInvalid as e: except PhoneNumberInvalid as e:
if invalid_phone_raises:
raise
else:
print(e.MESSAGE) print(e.MESSAGE)
phone_number = None
except FloodWait as e: except FloodWait as e:
print(e.MESSAGE.format(x=e.x)) print(e.MESSAGE.format(x=e.x))
time.sleep(e.x) time.sleep(e.x)
@ -219,9 +225,12 @@ class Client:
phone_registered = r.phone_registered phone_registered = r.phone_registered
phone_code_hash = r.phone_code_hash phone_code_hash = r.phone_code_hash
if not code_callback:
def code_callback():
return input("Enter phone code: ")
while True: while True:
phone_code = input("Enter phone code: ") phone_code = code_callback()
try: try:
if phone_registered: if phone_registered:
@ -260,11 +269,13 @@ class Client:
print(e.MESSAGE) print(e.MESSAGE)
except SessionPasswordNeeded as e: except SessionPasswordNeeded as e:
print(e.MESSAGE) print(e.MESSAGE)
invalid_password_raises = password is not None
while True: while True:
try: try:
r = self.send(functions.account.GetPassword()) r = self.send(functions.account.GetPassword())
if password is None:
print("Hint: {}".format(r.hint)) print("Hint: {}".format(r.hint))
password = input("Enter password: ") # TODO: Use getpass password = input("Enter password: ") # TODO: Use getpass
@ -273,7 +284,11 @@ class Client:
r = self.send(functions.auth.CheckPassword(password_hash)) r = self.send(functions.auth.CheckPassword(password_hash))
except PasswordHashInvalid as e: except PasswordHashInvalid as e:
if invalid_password_raises:
raise
else:
print(e.MESSAGE) print(e.MESSAGE)
password = None
except FloodWait as e: except FloodWait as e:
print(e.MESSAGE.format(x=e.x)) print(e.MESSAGE.format(x=e.x))
time.sleep(e.x) time.sleep(e.x)