Skip to content

Commit 6bdf57a

Browse files
Added support for setting email during authorization
KurimuzonAkuma/kurigram@830edcf Co-authored-by: KurimuzonAkuma <KurimuzonAkuma@users.noreply.github.com>
1 parent d1528d0 commit 6bdf57a

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

pyrogram/client.py

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,24 +442,70 @@ async def authorize(self) -> User:
442442
else:
443443
break
444444

445-
sent_code_descriptions = {
446-
enums.SentCodeType.APP: "Telegram app",
447-
enums.SentCodeType.CALL: "phone call",
448-
enums.SentCodeType.FLASH_CALL: "phone flash call",
449-
enums.SentCodeType.MISSED_CALL: "",
450-
enums.SentCodeType.SMS: "SMS",
451-
enums.SentCodeType.FRAGMENT_SMS: "Fragment SMS",
452-
enums.SentCodeType.FIREBASE_SMS: "SMS after Firebase attestation",
453-
enums.SentCodeType.EMAIL_CODE: "email",
454-
enums.SentCodeType.SETUP_EMAIL_REQUIRED: "add and verify email required",
455-
}
456-
457-
print(f"The confirmation code has been sent via {sent_code_descriptions[sent_code.type]}")
445+
if sent_code.type == enums.SentCodeType.SETUP_EMAIL_REQUIRED:
446+
print("Setup email required for authorization")
447+
448+
while True:
449+
try:
450+
while True:
451+
email = await ainput("Enter setup email: ", loop=self.loop)
452+
453+
if not email:
454+
continue
455+
456+
confirm = await ainput(f'Is "{email}" correct? (y/N): ', loop=self.loop)
457+
458+
if confirm.lower() == "y":
459+
break
460+
461+
await self.invoke(
462+
raw.functions.account.SendVerifyEmailCode(
463+
purpose=raw.types.EmailVerifyPurposeLoginSetup(
464+
phone_number=self.phone_number,
465+
phone_code_hash=sent_code.phone_code_hash,
466+
),
467+
email=email,
468+
)
469+
)
470+
471+
email_code = await ainput("Enter confirmation code received in setup email: ", loop=self.loop)
472+
473+
email_sent_code = await self.invoke(
474+
raw.functions.account.VerifyEmail(
475+
purpose=raw.types.EmailVerifyPurposeLoginSetup(
476+
phone_number=self.phone_number,
477+
phone_code_hash=sent_code.phone_code_hash,
478+
),
479+
verification=raw.types.EmailVerificationCode(code=email_code),
480+
)
481+
)
482+
483+
if isinstance(email_sent_code, raw.types.account.EmailVerifiedLogin):
484+
sent_code = types.SentCode._parse(email_sent_code.sent_code)
485+
except BadRequest as e:
486+
print(e.MESSAGE)
487+
self.phone_number = None
488+
self.bot_token = None
489+
else:
490+
break
491+
else:
492+
sent_code_descriptions = {
493+
enums.SentCodeType.APP: "Telegram app",
494+
enums.SentCodeType.CALL: "phone call",
495+
enums.SentCodeType.FLASH_CALL: "phone flash call",
496+
enums.SentCodeType.MISSED_CALL: "",
497+
enums.SentCodeType.SMS: "SMS",
498+
enums.SentCodeType.FRAGMENT_SMS: "Fragment SMS",
499+
enums.SentCodeType.FIREBASE_SMS: "SMS after Firebase attestation",
500+
enums.SentCodeType.EMAIL_CODE: "email",
501+
enums.SentCodeType.SETUP_EMAIL_REQUIRED: "add and verify email required",
502+
}
503+
504+
print(f"The confirmation code has been sent via {sent_code_descriptions[sent_code.type]}")
458505

459506
while True:
460507
if not self.phone_code:
461508
self.phone_code = await ainput("Enter confirmation code: ")
462-
463509
try:
464510
signed_in = await self.sign_in(self.phone_number, sent_code.phone_code_hash, self.phone_code)
465511
except BadRequest as e:

pyrogram/types/authorization/sent_code.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from pyrogram import raw, enums
20+
from pyrogram.errors import Unauthorized
2021
from ..object import Object
2122

2223

@@ -53,10 +54,17 @@ def __init__(
5354
self.timeout = timeout
5455

5556
@staticmethod
56-
def _parse(sent_code: raw.types.auth.SentCode) -> "SentCode":
57-
return SentCode(
58-
type=enums.SentCodeType(type(sent_code.type)),
59-
phone_code_hash=sent_code.phone_code_hash,
60-
next_type=enums.NextCodeType(type(sent_code.next_type)) if sent_code.next_type else None,
61-
timeout=sent_code.timeout
62-
)
57+
def _parse(sent_code: raw.base.auth.SentCode) -> "SentCode":
58+
if isinstance(sent_code, raw.types.auth.SentCodePaymentRequired):
59+
# TODO: raw.functions.auth.CheckPaidAuth
60+
raise Unauthorized(
61+
f"You need to pay {sent_code.amount}{sent_code.currency} or contact {sent_code.support_email_subject} ({sent_code.support_email_address}) which is currently not supported by Pyrogram."
62+
)
63+
64+
if isinstance(sent_code, raw.types.auth.SentCode):
65+
return SentCode(
66+
type=enums.SentCodeType(type(sent_code.type)),
67+
phone_code_hash=sent_code.phone_code_hash,
68+
next_type=enums.NextCodeType(type(sent_code.next_type)) if sent_code.next_type else None,
69+
timeout=sent_code.timeout
70+
)

0 commit comments

Comments
 (0)