Skip to content

Commit

Permalink
Implement missing .to_dict() and .stringify() on message/container
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 29, 2017
1 parent 6567f4b commit 05626c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 15 additions & 4 deletions telethon/tl/message_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ def __init__(self, messages):
self.content_related = False
self.messages = messages

def to_dict(self, recursive=True):
return {
'content_related': self.content_related,
'messages':
([] if self.messages is None else [
None if x is None else x.to_dict() for x in self.messages
]) if recursive else self.messages,
}

def __bytes__(self):
return struct.pack(
'<Ii', MessageContainer.CONSTRUCTOR_ID, len(self.messages)
) + b''.join(bytes(m) for m in self.messages)

def __str__(self):
return 'MessageContainer(messages=[{}])'\
.format(', '.join(str(m) for m in self.messages))

@staticmethod
def iter_read(reader):
reader.read_int(signed=False) # code
Expand All @@ -29,3 +34,9 @@ def iter_read(reader):
inner_sequence = reader.read_int()
inner_length = reader.read_int()
yield inner_msg_id, inner_sequence, inner_length

def __str__(self):
return TLObject.pretty_format(self)

def stringify(self):
return TLObject.pretty_format(self, indent=0)
14 changes: 12 additions & 2 deletions telethon/tl/tl_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ def __init__(self, session, request):
self.request = request
self.container_msg_id = None

def to_dict(self, recursive=True):
return {
'msg_id': self.msg_id,
'seq_no': self.seq_no,
'request': self.request,
'container_msg_id': self.container_msg_id,
}

def __bytes__(self):
body = GzipPacked.gzip_if_smaller(self.request)
return struct.pack('<qii', self.msg_id, self.seq_no, len(body)) + body

def __str__(self):
return 'TLMessage(msg_id={}, seq_no={}, body={})'\
.format(self.msg_id, self.seq_no, self.request)
return TLObject.pretty_format(self)

def stringify(self):
return TLObject.pretty_format(self, indent=0)

0 comments on commit 05626c8

Please sign in to comment.