Added an updates thread and construtor_id to TLObjects

A new thread is now created when MtProtoSender is created.
This thread constantly listens for Telegram updates (such as
incoming messages), so the updates can be received any time
rather than only when sending another request.
This commit is contained in:
Lonami
2016-09-09 11:47:37 +02:00
parent e9e44795ec
commit fd14a5a49a
9 changed files with 174 additions and 56 deletions

View File

@@ -1,9 +1,4 @@
try:
from .all_tlobjects import tlobjects
from .session import Session
from .mtproto_request import MTProtoRequest
from .telegram_client import TelegramClient
except ImportError:
import errors
raise errors.TLGeneratorNotRan()
from .all_tlobjects import tlobjects
from .session import Session
from .mtproto_request import MTProtoRequest
from .telegram_client import TelegramClient

View File

@@ -15,6 +15,7 @@ class MTProtoRequest:
self.confirm_received = False
# These should be overrode
self.constructor_id = 0
self.confirmed = False
self.responded = False

View File

@@ -9,6 +9,7 @@ from errors import *
from network import MtProtoSender, TcpTransport
from parser.markdown_parser import parse_message_entities
# For sending and receiving requests
from tl import Session
from tl.types import PeerUser, PeerChat, PeerChannel, InputPeerUser, InputPeerChat, InputPeerChannel, InputPeerEmpty
from tl.functions import InvokeWithLayerRequest, InitConnectionRequest
@@ -16,6 +17,9 @@ 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:
@@ -90,6 +94,11 @@ class TelegramClient:
self.connect(reconnect=True)
def disconnect(self):
"""Disconnects from the Telegram server **and pauses all the spawned threads**"""
if self.sender:
self.sender.disconnect()
# endregion
# region Telegram requests functions
@@ -261,6 +270,10 @@ class TelegramClient:
def on_update(self, tlobject):
"""This method is fired when there are updates from Telegram.
Add your own implementation below, or simply override it!"""
print('We have an update: {}'.format(str(tlobject)))
# Only show incoming messages
if type(tlobject) is UpdateShortMessage:
if not tlobject.out:
print('> User with ID {} said "{}"'.format(tlobject.user_id, tlobject.message))
# endregion