Skip to main content

pyMTProto

This is a Telegram MTProto protocol library inspired by h11.

This library implements the following MTProto transports:

  • Abridged
  • Intermediate
  • Padded Intermediate
  • Obfuscated versions of all above
  • Full
  • HTTP
  • Websocket

Also, this library has WIP "bring-your-own-I/O" implementation of the session layer or MTProto protocol.

Installation

pip install mtproto

Note that in order to use obfuscated transports or encrypt/decrypt mtproto messages, you MUST specify at least one (if you install both, only tgcrypto will be used) crypto library in square brackets (currently tgcrypto and pyaes are supported):

pip install mtproto[tgcrypto]
# or
pip install mtproto[pyaes]

To use http or websocket transport, you'd need to install http or ws extra:

pip install mtproto[http]
# or
pip install mtproto[ws]

Usage

from os import urandom

from mtproto import ConnectionRole
from mtproto.transport import Connection
from mtproto.transport import IntermediateTransport
from mtproto.transport.packets import UnencryptedMessagePacket

conn = Connection(
    ConnectionRole.CLIENT,
    # Transport class to use, supported: 
    #  AbridgedTransport, IntermediateTransport, PaddedIntermediateTransport, FullTransport, HttpTransport, WsTransport
    # Default is AbridgedTransport. You need to specify transport class only if you are using ConnectionRole.CLIENT role.
    #  When role is ConnectionRole.SERVER, transport is ignored
    transport=IntermediateTransport,
    # Whether to use transport obfuscation or not. Default is False. Obfuscation for FullTransport is not supported now. 
    obfuscated=True,
)

to_send = conn.send(UnencryptedMessagePacket(
    message_id=123456789,
    message_data=b"\xbe\x7e\x8e\xf1"[::-1] + urandom(16)  # req_pq_multi#be7e8ef1 nonce:int128
))

# Send data to telegram server
...
# Receive data from telegram server
received = ...
conn.data_received(received)
packet = conn.next_event()

print(packet)
# UnencryptedMessagePacket(message_id=..., message_data=b"...")

Metadata

Release files for mtproto 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mtproto 0.3.1
File Size Uploaded
mtproto-0.3.1.tar.gz 27.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mtproto 0.3.1
File Interpreter ABI Platform
mtproto-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 64.8 kB

Release files / mtproto-0.3.1.tar.gz

Download URL mtproto-0.3.1.tar.gz
Size 27.4 kB
Tags Source
SHA-256 checksum
How to use checksums
642bdc5396429b78e13632dfa25f8839ab9062ae1fb7572cd4c992481cbd3c55
BLAKE2b-256 checksum
How to use checksums
573228b1a1780eac05aec67ef9616973f6f2cc34f6e643e358227b61d46ea6c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / mtproto-0.3.1-py3-none-any.whl

Download URL mtproto-0.3.1-py3-none-any.whl
Size 37.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
596216e9a82ef5e2fb30e359edc601d0e82565bdf197d495f3e51bbc9a2c8365
BLAKE2b-256 checksum
How to use checksums
0e463f77aa5fd628904ff5bf2c45be8f43f8ea3746d47503d01407eca4597388
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page