From fb9813ae61bd6657293c867bf17af102cb8c37c2 Mon Sep 17 00:00:00 2001 From: "Dmitry D. Chernov" Date: Sun, 24 Dec 2017 21:21:14 +1000 Subject: [PATCH] TelegramClient.send_code_request(): Change logic of methods invocation Before: First call, force_sms=False: SendCodeRequest Next call, force_sms=False: SendCodeRequest First call, force_sms=True: raise ValueError Next call, force_sms=True: ResendCodeRequest That's inconvenient because the user must remember whether the code requested at all and whether the request was successful. In addition, the repeated invocation of SendCodeRequest does nothing. This commit changes logic to this: First call, force_sms=False: SendCodeRequest Next call, force_sms=False: ResendCodeRequest First call, force_sms=True: SendCodeRequest, ResendCodeRequest Next call, force_sms=True: ResendCodeRequest --- telethon/telegram_client.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index fc4b4342..8546c377 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -138,23 +138,24 @@ class TelegramClient(TelegramBareClient): :param str | int phone: The phone to which the code will be sent. :param bool force_sms: - Whether to force sending as SMS. You should call it at least - once before without this set to True first. + Whether to force sending as SMS. :return auth.SentCode: Information about the result of the request. """ phone = EntityDatabase.parse_phone(phone) or self._phone - if force_sms: - if not self._phone_code_hash: - raise ValueError( - 'You must call this method without force_sms at least once.' - ) - result = self(ResendCodeRequest(phone, self._phone_code_hash)) - else: + + if not self._phone_code_hash: result = self(SendCodeRequest(phone, self.api_id, self.api_hash)) self._phone_code_hash = result.phone_code_hash + else: + force_sms = True self._phone = phone + + if force_sms: + result = self(ResendCodeRequest(phone, self._phone_code_hash)) + self._phone_code_hash = result.phone_code_hash + return result def sign_in(self, phone=None, code=None,