From f381b267903cad2b7e208a5006a7cd10a02b6eb1 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 4 Nov 2017 20:46:02 +0100 Subject: [PATCH] Add optional force_sms parameter to .send_code_request() --- telethon/telegram_client.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index c638c2b1..59bf4a9a 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -23,7 +23,7 @@ from .tl.functions.account import ( ) from .tl.functions.auth import ( CheckPasswordRequest, LogOutRequest, SendCodeRequest, SignInRequest, - SignUpRequest, ImportBotAuthorizationRequest + SignUpRequest, ResendCodeRequest, ImportBotAuthorizationRequest ) from .tl.functions.contacts import ( GetContactsRequest, ResolveUsernameRequest @@ -129,16 +129,29 @@ class TelegramClient(TelegramBareClient): # region Authorization requests - def send_code_request(self, phone): + def send_code_request(self, phone, force_sms=False): """Sends a code request to the specified phone number. - :param str | int phone: The phone to which the code will be sent. - :return auth.SentCode: Information about the result of the request. + :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. + :return auth.SentCode: + Information about the result of the request. """ phone = EntityDatabase.parse_phone(phone) or self._phone - result = self(SendCodeRequest(phone, self.api_id, self.api_hash)) + 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: + result = self(SendCodeRequest(phone, self.api_id, self.api_hash)) + self._phone_code_hash = result.phone_code_hash + self._phone = phone - self._phone_code_hash = result.phone_code_hash return result def sign_in(self, phone=None, code=None,