Minor improvement to updates handling

Now the updates thread won't start unless you add,
at least, one updates handler. Also, if the TcpClient
was receiving (i.e., from an update), it will let the
update to be received first instead of crashing
This commit is contained in:
Lonami
2016-09-11 11:50:38 +02:00
parent c11795f294
commit cdb1674a27
4 changed files with 31 additions and 34 deletions

View File

@@ -18,9 +18,6 @@ from tl.functions.help import GetConfigRequest
from tl.functions.auth import SendCodeRequest, SignInRequest
from tl.functions.messages import GetDialogsRequest, GetHistoryRequest, SendMessageRequest
# For working with updates
from tl.types import UpdateShortMessage
class TelegramClient:
@@ -59,7 +56,6 @@ class TelegramClient:
self.session.save()
self.sender = MtProtoSender(self.transport, self.session)
self.sender.add_update_handler(self.on_update)
# Now it's time to send an InitConnectionRequest
# This must always be invoked with the layer we'll be using
@@ -72,10 +68,6 @@ class TelegramClient:
result = self.invoke(InvokeWithLayerRequest(layer=self.layer, query=query))
# Only listen for updates if we're authorized
if self.is_user_authorized():
self.sender.set_listen_for_updates(True)
# We're only interested in the DC options,
# although many other options are available!
self.dc_options = result.dc_options
@@ -260,21 +252,18 @@ class TelegramClient:
return InputPeerChannel(channel.id, channel.access_hash)
except StopIteration:
pass
return None
return None
# endregion
# region Updates handling
def on_update(self, tlobject):
"""This method is fired when there are updates from Telegram.
Add your own implementation below, or simply override it!"""
def add_update_handler(self, handler):
"""Adds an update handler (a function which takes a TLObject,
an update, as its parameter) and listens for updates"""
self.sender.add_update_handler(handler)
# Only show incoming messages
if type(tlobject) is UpdateShortMessage:
if not tlobject.out:
print('> User with ID {} said "{}"'.format(tlobject.user_id, tlobject.message))
def remove_update_handler(self, handler):
self.sender.remove_update_handler(handler)
# endregion