mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Support custom callbacks on Client.authorize()
This commit is contained in:
parent
3e5421f55f
commit
d01d852dc2
@ -170,8 +170,10 @@ class Client:
|
||||
"""
|
||||
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:
|
||||
if phone_number is None:
|
||||
phone_number = input("Enter phone number: ")
|
||||
|
||||
while True:
|
||||
@ -208,7 +210,11 @@ class Client:
|
||||
)
|
||||
break
|
||||
except PhoneNumberInvalid as e:
|
||||
if invalid_phone_raises:
|
||||
raise
|
||||
else:
|
||||
print(e.MESSAGE)
|
||||
phone_number = None
|
||||
except FloodWait as e:
|
||||
print(e.MESSAGE.format(x=e.x))
|
||||
time.sleep(e.x)
|
||||
@ -219,9 +225,12 @@ class Client:
|
||||
|
||||
phone_registered = r.phone_registered
|
||||
phone_code_hash = r.phone_code_hash
|
||||
if not code_callback:
|
||||
def code_callback():
|
||||
return input("Enter phone code: ")
|
||||
|
||||
while True:
|
||||
phone_code = input("Enter phone code: ")
|
||||
phone_code = code_callback()
|
||||
|
||||
try:
|
||||
if phone_registered:
|
||||
@ -260,11 +269,13 @@ class Client:
|
||||
print(e.MESSAGE)
|
||||
except SessionPasswordNeeded as e:
|
||||
print(e.MESSAGE)
|
||||
invalid_password_raises = password is not None
|
||||
|
||||
while True:
|
||||
try:
|
||||
r = self.send(functions.account.GetPassword())
|
||||
|
||||
if password is None:
|
||||
print("Hint: {}".format(r.hint))
|
||||
password = input("Enter password: ") # TODO: Use getpass
|
||||
|
||||
@ -273,7 +284,11 @@ class Client:
|
||||
|
||||
r = self.send(functions.auth.CheckPassword(password_hash))
|
||||
except PasswordHashInvalid as e:
|
||||
if invalid_password_raises:
|
||||
raise
|
||||
else:
|
||||
print(e.MESSAGE)
|
||||
password = None
|
||||
except FloodWait as e:
|
||||
print(e.MESSAGE.format(x=e.x))
|
||||
time.sleep(e.x)
|
||||
|
Loading…
x
Reference in New Issue
Block a user