2018-06-07 11:30:20 +03:00
|
|
|
import asyncio
|
2017-09-27 22:01:20 +03:00
|
|
|
import struct
|
|
|
|
|
2018-06-09 14:11:49 +03:00
|
|
|
from .gzippacked import GzipPacked
|
|
|
|
from .. import TLObject
|
|
|
|
from ..functions import InvokeAfterMsgRequest
|
2017-09-27 22:01:20 +03:00
|
|
|
|
|
|
|
|
2017-09-28 12:59:24 +03:00
|
|
|
class TLMessage(TLObject):
|
2018-06-07 11:30:20 +03:00
|
|
|
"""
|
|
|
|
https://core.telegram.org/mtproto/service_messages#simple-container.
|
|
|
|
|
|
|
|
Messages are what's ultimately sent to Telegram:
|
|
|
|
message msg_id:long seqno:int bytes:int body:bytes = Message;
|
|
|
|
|
|
|
|
Each message has its own unique identifier, and the body is simply
|
|
|
|
the serialized request that should be executed on the server. Then
|
|
|
|
Telegram will, at some point, respond with the result for this msg.
|
|
|
|
|
|
|
|
Thus it makes sense that requests and their result are bound to a
|
|
|
|
sent `TLMessage`, and this result can be represented as a `Future`
|
|
|
|
that will eventually be set with either a result, error or cancelled.
|
|
|
|
"""
|
2018-06-09 14:48:27 +03:00
|
|
|
def __init__(self, msg_id, seq_no, obj=None, after_id=0):
|
2017-09-27 22:01:20 +03:00
|
|
|
super().__init__()
|
2018-06-09 12:34:01 +03:00
|
|
|
self.msg_id = msg_id
|
|
|
|
self.seq_no = seq_no
|
2018-06-09 14:48:27 +03:00
|
|
|
self.obj = obj
|
2017-10-22 14:13:49 +03:00
|
|
|
self.container_msg_id = None
|
2018-06-07 11:30:20 +03:00
|
|
|
self.future = asyncio.Future()
|
2017-09-27 22:01:20 +03:00
|
|
|
|
2018-05-09 17:18:42 +03:00
|
|
|
# After which message ID this one should run. We do this so
|
|
|
|
# InvokeAfterMsgRequest is transparent to the user and we can
|
|
|
|
# easily invoke after while confirming the original request.
|
|
|
|
self.after_id = after_id
|
|
|
|
|
2017-10-29 22:13:00 +03:00
|
|
|
def to_dict(self, recursive=True):
|
|
|
|
return {
|
|
|
|
'msg_id': self.msg_id,
|
|
|
|
'seq_no': self.seq_no,
|
2018-06-09 14:48:27 +03:00
|
|
|
'obj': self.obj,
|
2017-10-29 22:13:00 +03:00
|
|
|
'container_msg_id': self.container_msg_id,
|
2018-05-09 17:18:42 +03:00
|
|
|
'after_id': self.after_id
|
2017-10-29 22:13:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-17 20:54:24 +03:00
|
|
|
def __bytes__(self):
|
2018-05-09 17:18:42 +03:00
|
|
|
if self.after_id is None:
|
2018-06-09 14:48:27 +03:00
|
|
|
body = GzipPacked.gzip_if_smaller(self.obj)
|
2018-05-09 17:18:42 +03:00
|
|
|
else:
|
|
|
|
body = GzipPacked.gzip_if_smaller(
|
2018-06-09 14:48:27 +03:00
|
|
|
InvokeAfterMsgRequest(self.after_id, self.obj))
|
2018-05-09 17:18:42 +03:00
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
return struct.pack('<qii', self.msg_id, self.seq_no, len(body)) + body
|
2017-10-21 21:23:53 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-29 22:13:00 +03:00
|
|
|
return TLObject.pretty_format(self)
|
|
|
|
|
|
|
|
def stringify(self):
|
|
|
|
return TLObject.pretty_format(self, indent=0)
|