mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 10:53:44 +03:00
Greatly improve the handling of working with different DC
This commit is contained in:
parent
6b1dc30045
commit
628b7391e3
|
@ -328,7 +328,7 @@ class MtProtoSender:
|
||||||
error.additional_data))
|
error.additional_data))
|
||||||
sleep(error.additional_data)
|
sleep(error.additional_data)
|
||||||
|
|
||||||
elif error.message.startswith('PHONE_MIGRATE_'):
|
elif '_MIGRATE_' in error.message:
|
||||||
raise InvalidDCError(error.additional_data)
|
raise InvalidDCError(error.additional_data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -152,18 +152,29 @@ class TelegramClient:
|
||||||
|
|
||||||
# region Telegram requests functions
|
# region Telegram requests functions
|
||||||
|
|
||||||
def invoke(self, request, timeout=timedelta(seconds=5)):
|
def invoke(self, request, timeout=timedelta(seconds=5), throw_invalid_dc=False):
|
||||||
"""Invokes a MTProtoRequest (sends and receives it) and returns its result.
|
"""Invokes a MTProtoRequest (sends and receives it) and returns its result.
|
||||||
An optional timeout can be given to cancel the operation after the time delta.
|
An optional timeout can be given to cancel the operation after the time delta.
|
||||||
Timeout can be set to None for no timeout"""
|
Timeout can be set to None for no timeout.
|
||||||
|
|
||||||
|
If throw_invalid_dc is True, these errors won't be caught (useful to
|
||||||
|
avoid infinite recursion). This should not be set to True manually."""
|
||||||
if not issubclass(type(request), MTProtoRequest):
|
if not issubclass(type(request), MTProtoRequest):
|
||||||
raise ValueError('You can only invoke MtProtoRequests')
|
raise ValueError('You can only invoke MtProtoRequests')
|
||||||
|
|
||||||
|
try:
|
||||||
self.sender.send(request)
|
self.sender.send(request)
|
||||||
self.sender.receive(request, timeout)
|
self.sender.receive(request, timeout)
|
||||||
|
|
||||||
return request.result
|
return request.result
|
||||||
|
|
||||||
|
except InvalidDCError as error:
|
||||||
|
if throw_invalid_dc:
|
||||||
|
raise error
|
||||||
|
|
||||||
|
self.reconnect_to_dc(error.new_dc)
|
||||||
|
return self.invoke(request, timeout=timeout, throw_invalid_dc=True)
|
||||||
|
|
||||||
# region Authorization requests
|
# region Authorization requests
|
||||||
|
|
||||||
def is_user_authorized(self):
|
def is_user_authorized(self):
|
||||||
|
@ -173,16 +184,8 @@ class TelegramClient:
|
||||||
|
|
||||||
def send_code_request(self, phone_number):
|
def send_code_request(self, phone_number):
|
||||||
"""Sends a code request to the specified phone number"""
|
"""Sends a code request to the specified phone number"""
|
||||||
request = SendCodeRequest(phone_number, self.api_id, self.api_hash)
|
result = self.invoke(SendCodeRequest(phone_number, self.api_id, self.api_hash))
|
||||||
completed = False
|
|
||||||
while not completed:
|
|
||||||
try:
|
|
||||||
result = self.invoke(request)
|
|
||||||
self.phone_code_hashes[phone_number] = result.phone_code_hash
|
self.phone_code_hashes[phone_number] = result.phone_code_hash
|
||||||
completed = True
|
|
||||||
|
|
||||||
except InvalidDCError as error:
|
|
||||||
self.reconnect_to_dc(error.new_dc)
|
|
||||||
|
|
||||||
def sign_in(self, phone_number=None, code=None, password=None):
|
def sign_in(self, phone_number=None, code=None, password=None):
|
||||||
"""Completes the authorization of a phone number by providing the received code.
|
"""Completes the authorization of a phone number by providing the received code.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user