From 1029c38d7e5b8b90128c0cef31a011626c9b78bb Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 16 Feb 2022 12:59:52 +0100 Subject: [PATCH] Remove phone and hash from sign_up --- readthedocs/misc/v2-migration-guide.rst | 1 + telethon/_client/auth.py | 22 ++++++++-------------- telethon/_client/telegramclient.py | 18 ++++-------------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/readthedocs/misc/v2-migration-guide.rst b/readthedocs/misc/v2-migration-guide.rst index 85eb60a4..3380ac92 100644 --- a/readthedocs/misc/v2-migration-guide.rst +++ b/readthedocs/misc/v2-migration-guide.rst @@ -982,3 +982,4 @@ is_connected is now a property (consistent with the rest of ``is_`` properties) send_code_request now returns a custom type (reducing raw api). sign_in no longer has phone or phone_hash (these are impl details, and now it's less error prone). also mandatory code=. also no longer is a no-op if already logged in. different error for sign up required. send code / sign in now only expect a single phone. resend code with new phone is send code, not resend. +sign_up code is also now a kwarg. and no longer noop if already loggedin. diff --git a/telethon/_client/auth.py b/telethon/_client/auth.py index 6301b9b1..795a7f51 100644 --- a/telethon/_client/auth.py +++ b/telethon/_client/auth.py @@ -233,17 +233,17 @@ async def sign_in( return await _update_session_state(self, result.user) + async def sign_up( self: 'TelegramClient', - code: typing.Union[str, int], first_name: str, last_name: str = '', *, - phone: str = None, - phone_code_hash: str = None) -> '_tl.User': - me = await self.get_me() - if me: - return me + code: typing.Union[str, int]) -> '_tl.User': + if not self._phone_code_hash: + raise ValueError('Must call client.send_code_request before sign up') + + phone, phone_code_hash = self._phone_code_hash # To prevent abuse, one has to try to sign in before signing up. This # is the current way in which Telegram validates the code to sign up. @@ -259,18 +259,13 @@ async def sign_up( code=code, phone_code_hash=phone_code_hash, ) - except errors.PhoneNumberUnoccupiedError: + except errors.SignUpRequired: pass # code is correct and was used, now need to sign in if self._tos and self._tos.text: sys.stderr.write("{}\n".format(self._tos.text)) sys.stderr.flush() - if not self._phone_code_hash: - raise ValueError('Must call client.send_code_request before sign up') - - phone, phone_code_hash = self._phone_code_hash - result = await self(_tl.fn.auth.SignUp( phone_number=phone, phone_code_hash=phone_code_hash, @@ -279,8 +274,7 @@ async def sign_up( )) if self._tos: - await self( - _tl.fn.help.AcceptTermsOfService(self._tos.id)) + await self(_tl.fn.help.AcceptTermsOfService(self._tos.id)) return await _update_session_state(self, result.user) diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index e801b26d..df58a287 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -444,12 +444,10 @@ class TelegramClient: @forward_call(auth.sign_up) async def sign_up( self: 'TelegramClient', - code: typing.Union[str, int], first_name: str, last_name: str = '', *, - phone: str = None, - phone_code_hash: str = None) -> '_tl.User': + code: typing.Union[str, int]) -> '_tl.User': """ Signs up to Telegram as a new user account. @@ -463,22 +461,14 @@ class TelegramClient: and https://core.telegram.org/api/terms. Arguments - code (`str` | `int`): - The code sent by Telegram - first_name (`str`): The first name to be used by the new account. last_name (`str`, optional) Optional last name. - phone (`str` | `int`, optional): - The phone to sign up. This will be the last phone used by - default (you normally don't need to set this). - - phone_code_hash (`str`, optional): - The hash returned by `send_code_request`. This can be left as - `None` to use the last hash known for the phone to be used. + code (`str` | `int`): + The code sent by Telegram Returns The new created :tl:`User`. @@ -490,7 +480,7 @@ class TelegramClient: await client.send_code_request(phone) code = input('enter code: ') - await client.sign_up(code, first_name='Anna', last_name='Banana') + await client.sign_up('Anna', 'Banana', code=code) """ @forward_call(auth.send_code_request)