diff --git a/telethon/interactive_telegram_client.py b/telethon/interactive_telegram_client.py index e9d32dae..b24c6b0f 100644 --- a/telethon/interactive_telegram_client.py +++ b/telethon/interactive_telegram_client.py @@ -51,7 +51,11 @@ class InteractiveTelegramClient(TelegramClient): self.found_media = set() print('Connecting to Telegram servers...') - self.connect() + if not self.connect(): + print('Initial connection failed. Retrying...') + if not self.connect(): + print('Could not connect to Telegram servers.') + return # Then, ensure we're authorized and have access if not self.is_user_authorized(): diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 81ee707e..70c5d790 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -147,7 +147,8 @@ class TelegramClient: self._setup_ping_thread() return True - except RPCError as error: + except (RPCError, ConnectionError) as error: + # Probably errors from the previous session, ignore them self._logger.warning('Could not stabilise initial connection: {}' .format(error)) return False @@ -163,7 +164,7 @@ class TelegramClient: self.transport = None # Also disconnect all the cached senders - for sender in self._cached_senders: + for sender in self._cached_senders.values(): sender.disconnect() self._cached_senders.clear() @@ -196,7 +197,7 @@ class TelegramClient: if not self.dc_options: raise ConnectionError( 'Cannot determine the required data center IP address. ' - 'Stabilise an initial connection first.') + 'Stabilise a successful initial connection first.') return next(dc for dc in self.dc_options if dc.id == dc_id) @@ -218,8 +219,8 @@ class TelegramClient: if sender and session: if init_connection: - # TODO reconnect - pass + sender.disconnect() + sender.connect() return sender else: