telethon.extensions package

telethon.extensions.binary_reader module

This module contains the BinaryReader utility class.

class telethon.extensions.binary_reader.BinaryReader(data=None, stream=None)

Bases: object

Small utility class to read binary data. Also creates a “Memory Stream” if necessary

close()

Closes the reader, freeing the BytesIO stream.

get_bytes()

Gets the byte array representing the current buffer as a whole.

read(length=None)

Read the given amount of bytes.

read_byte()

Reads a single byte value.

read_double()

Reads a real floating point (8 bytes) value.

read_float()

Reads a real floating point (4 bytes) value.

read_int(signed=True)

Reads an integer (4 bytes) value.

read_large_int(bits, signed=True)

Reads a n-bits long integer value.

read_long(signed=True)

Reads a long integer (8 bytes) value.

seek(offset)

Seeks the stream position given an offset from the current position. The offset may be negative.

set_position(position)

Sets the current position on the stream.

tell_position()

Tells the current position on the stream.

tgread_bool()

Reads a Telegram boolean value.

tgread_bytes()

Reads a Telegram-encoded byte array, without the need of specifying its length.

tgread_date()

Reads and converts Unix time (used by Telegram) into a Python datetime object.

tgread_object()

Reads a Telegram object.

tgread_string()

Reads a Telegram-encoded string.

tgread_vector()

Reads a vector (a list) of Telegram objects.

telethon.extensions.markdown module

Simple markdown parser which does not support nesting. Intended primarily for use within the library, which attempts to handle emojies correctly, since they seem to count as two characters and it’s a bit strange.

telethon.extensions.markdown.get_inner_text(text, entity)

Gets the inner text that’s surrounded by the given entity or entities. For instance: text = ‘hey!’, entity = MessageEntityBold(2, 2) -> ‘y!’.

Parameters:
  • text – the original text.
  • entity – the entity or entities that must be matched.
Returns:

a single result or a list of the text surrounded by the entities.

telethon.extensions.markdown.parse(message, delimiters=None, url_re=None)

Parses the given markdown message and returns its stripped representation plus a list of the MessageEntity’s that were found.

Parameters:
  • message – the message with markdown-like syntax to be parsed.
  • delimiters – the delimiters to be used, {delimiter: type}.
  • url_re – the URL bytes regex to be used. Must have two groups.
Returns:

a tuple consisting of (clean message, [message entities]).

telethon.extensions.markdown.unparse(text, entities, delimiters=None, url_fmt=None)

Performs the reverse operation to .parse(), effectively returning markdown-like syntax given a normal text and its MessageEntity’s.

Parameters:
  • text – the text to be reconverted into markdown.
  • entities – the MessageEntity’s applied to the text.
Returns:

a markdown-like text representing the combination of both inputs.

telethon.extensions.tcp_client module

This module holds a rough implementation of the C# TCP client.

class telethon.extensions.tcp_client.TcpClient(proxy=None, timeout=datetime.timedelta(0, 5))

Bases: object

A simple TCP client to ease the work with sockets and proxies.

close()

Closes the connection.

connect(ip, port)

Tries connecting forever to IP:port unless an OSError is raised.

Parameters:
  • ip – the IP to connect to.
  • port – the port to connect to.
connected

Determines whether the client is connected or not.

read(size)

Reads (receives) a whole block of size bytes from the connected peer.

Parameters:size – the size of the block to be read.
Returns:the read data with len(data) == size.
write(data)

Writes (sends) the specified bytes to the connected peer.

Parameters:data – the data to send.