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:
parent
3e5421f55f
commit
d01d852dc2
@ -170,17 +170,19 @@ 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:
|
||||||
phone_number = input("Enter phone number: ")
|
if phone_number is None:
|
||||||
|
phone_number = input("Enter phone number: ")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
confirm = input("Is \"{}\" correct? (y/n): ".format(phone_number))
|
confirm = input("Is \"{}\" correct? (y/n): ".format(phone_number))
|
||||||
|
|
||||||
if confirm in ("y", "1"):
|
if confirm in ("y", "1"):
|
||||||
break
|
break
|
||||||
elif confirm in ("n", "2"):
|
elif confirm in ("n", "2"):
|
||||||
phone_number = input("Enter phone number: ")
|
phone_number = input("Enter phone number: ")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = self.send(
|
r = self.send(
|
||||||
@ -208,7 +210,11 @@ class Client:
|
|||||||
)
|
)
|
||||||
break
|
break
|
||||||
except PhoneNumberInvalid as e:
|
except PhoneNumberInvalid as e:
|
||||||
print(e.MESSAGE)
|
if invalid_phone_raises:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
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,20 +269,26 @@ 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())
|
||||||
|
|
||||||
print("Hint: {}".format(r.hint))
|
if password is None:
|
||||||
password = input("Enter password: ") # TODO: Use getpass
|
print("Hint: {}".format(r.hint))
|
||||||
|
password = input("Enter password: ") # TODO: Use getpass
|
||||||
|
|
||||||
password = r.current_salt + password.encode() + r.current_salt
|
password = r.current_salt + password.encode() + r.current_salt
|
||||||
password_hash = sha256(password).digest()
|
password_hash = sha256(password).digest()
|
||||||
|
|
||||||
r = self.send(functions.auth.CheckPassword(password_hash))
|
r = self.send(functions.auth.CheckPassword(password_hash))
|
||||||
except PasswordHashInvalid as e:
|
except PasswordHashInvalid as e:
|
||||||
print(e.MESSAGE)
|
if invalid_password_raises:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user