From 178643d3a1951688ee16a32ee33d89ed53d4f14a Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 15 Feb 2018 11:41:32 +0100 Subject: [PATCH] Periodically send getState even without disconnect (341fb38) After some more tests, even if the server doesn't drop the connection, it might also just stop sending updates at all. --- telethon/telegram_bare_client.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index db07f321..31a3f7d9 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -158,6 +158,14 @@ class TelegramBareClient: self._last_ping = datetime.now() self._ping_delay = timedelta(minutes=1) + # Also have another delay for GetStateRequest. + # + # If the connection is kept alive for long without invoking any + # high level request the server simply stops sending updates. + # TODO maybe we can have ._last_request instead if any req works? + self._last_state = datetime.now() + self._state_delay = timedelta(hours=1) + # Some errors are known but there's nothing we can do from the # background thread. If any of these happens, call .disconnect(), # and raise them next time .invoke() is tried to be called. @@ -579,6 +587,7 @@ class TelegramBareClient: otherwise it should be called manually after enabling updates. """ self.updates.process(self(GetStateRequest())) + self._last_state = datetime.now() def add_update_handler(self, handler): """Adds an update handler (a function which takes a TLObject, @@ -650,6 +659,10 @@ class TelegramBareClient: )) self._last_ping = datetime.now() + if datetime.now() > self._last_state + self._state_delay: + self._sender.send(GetStateRequest()) + self._last_state = datetime.now() + __log__.debug('Receiving items from the network...') self._sender.receive(update_state=self.updates) except TimeoutError: