python-telegram
Python API for the tdlib library. It helps you build your own Telegram clients.
tdlib connects to Telegram over MTProto, the same protocol the official apps use.
This library signs in as a full Telegram account with a phone number, and it can do what a regular client can do.
It is not a wrapper around the HTTP Bot API.
If you only need a bot, python-telegram-bot is a better fit.
You can still sign in as a bot here by passing bot_token instead of phone.
Installation
This library requires Python 3.10+. Windows is not supported.
pip install python-telegram
See documentation for more details.
tdlib
Four wheels bundle a tdlib binary. Each one works on Python 3.10 through 3.14:
| Platform | Wheel | Needs at least |
|---|---|---|
| Linux x86_64 | manylinux_2_28_x86_64 |
glibc 2.28 |
| Linux aarch64 | manylinux_2_28_aarch64 |
glibc 2.28 |
| macOS arm64 | macosx_11_0_arm64 |
macOS 11 |
| macOS x86_64 | macosx_10_15_x86_64 |
macOS 10.15 |
Those binaries link OpenSSL and zlib statically, so they need nothing from the system.
Anywhere else, pip installs the source distribution, which carries no binary. On musl, on
32-bit ARM, or with an older glibc, compile tdlib
yourself.
python-telegram looks for the library in this order, and logs which one it used:
- the
library_pathargument - the
PYTHON_TELEGRAM_TDLIB_PATHenvironment variable - a system-wide
tdjson - the bundled binary
A system-wide tdlib therefore wins over the bundled one. To point at a specific build,
pass its path. The file is called libtdjson.so on Linux and libtdjson.dylib on macOS:
tg = Telegram(
# ...
library_path="/usr/local/lib/libtdjson.so",
)
If nothing can be found, TDJson() raises TDLibNotFoundError, an OSError subclass whose
message names the platform, the paths tried and the overrides.
Docker
This library has a docker image:
docker run -i -t --rm \
-v /tmp/docker-python-telegram/:/tmp/ \
akhmetov/python-telegram \
python3 /app/examples/send_message.py $API_ID $API_HASH $PHONE $CHAT_ID $TEXT
How to use the library
First, register a new Telegram application to get your api_id and api_hash.
Check out the tutorial for more details.
Basic example:
from telegram.client import Telegram
from telegram.text import Spoiler
tg = Telegram(
api_id=123456,
api_hash="api_hash",
phone="+31611111111", # you can pass 'bot_token' instead
database_encryption_key="changekey123",
files_directory="/tmp/.tdlib_files/",
)
tg.login()
# The chat must be in the tdlib database before you can send a message to it.
# `get_chats` loads up to `limit` chats from the main chat list.
result = tg.get_chats(limit=100)
result.wait()
chat_id = 123456789
result = tg.send_message(chat_id, Spoiler("Hello world!"))
# `tdlib` is asynchronous, so `python-telegram` always returns an `AsyncResult` object.
# You can receive a result with the `wait` method of this object.
result.wait()
print(result.update)
tg.stop() # You must call `stop` at the end of the script.
You can also use call_method to call any tdlib method:
tg.call_method("getUser", params={"user_id": user_id})
More examples can be found in the /examples/ directory.
More information is available in the documentation.
Development
See CONTRIBUTING.md.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_telegram-2.0.0.tar.gz.
File metadata
- Download URL: python_telegram-2.0.0.tar.gz
- Upload date:
- Size: 26.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e024306fdb1c633ed2ab02981f3d36bca73d759cf776e3940f36babbc999002
|
|
| MD5 |
4778a65c8f95ef3e913d96fbb588ddde
|
|
| BLAKE2b-256 |
6d61310fe14c1e6700940d2bb4c79d7cb91e256fa4c02c148b4646924b70c42a
|
Provenance
The following attestation bundles were made for python_telegram-2.0.0.tar.gz:
Publisher:
release.yml on alexander-akhmetov/python-telegram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_telegram-2.0.0.tar.gz -
Subject digest:
5e024306fdb1c633ed2ab02981f3d36bca73d759cf776e3940f36babbc999002 - Sigstore transparency entry: 2569979753
- Sigstore integration time:
-
Permalink:
alexander-akhmetov/python-telegram@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/alexander-akhmetov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_telegram-2.0.0-py3-none-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: python_telegram-2.0.0-py3-none-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 15.8 MB
- Tags: Python 3, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36f7264be8eb6a9ec727d6ea78ca4b9828aaad491995eb5cb91aa24b76d1ef19
|
|
| MD5 |
ec42fc01072d0e8dcb0e9a6ec038cdf2
|
|
| BLAKE2b-256 |
21ac0963c95583dfee246e5364b50980d799c59d0d148f27d4d5587aa1df7021
|
Provenance
The following attestation bundles were made for python_telegram-2.0.0-py3-none-manylinux_2_28_x86_64.whl:
Publisher:
release.yml on alexander-akhmetov/python-telegram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_telegram-2.0.0-py3-none-manylinux_2_28_x86_64.whl -
Subject digest:
36f7264be8eb6a9ec727d6ea78ca4b9828aaad491995eb5cb91aa24b76d1ef19 - Sigstore transparency entry: 2569980170
- Sigstore integration time:
-
Permalink:
alexander-akhmetov/python-telegram@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/alexander-akhmetov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_telegram-2.0.0-py3-none-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: python_telegram-2.0.0-py3-none-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 15.6 MB
- Tags: Python 3, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7edd4367fa1a597a654e38c86c964494629fb6d03f4cb2d7ba4a7193b6847e30
|
|
| MD5 |
05faaec8ae9d8b66f14065fc73ceea72
|
|
| BLAKE2b-256 |
4032c294e26bc0d48d818fe192af3ae415a86a79924d67cdeee62c0d229da77e
|
Provenance
The following attestation bundles were made for python_telegram-2.0.0-py3-none-manylinux_2_28_aarch64.whl:
Publisher:
release.yml on alexander-akhmetov/python-telegram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_telegram-2.0.0-py3-none-manylinux_2_28_aarch64.whl -
Subject digest:
7edd4367fa1a597a654e38c86c964494629fb6d03f4cb2d7ba4a7193b6847e30 - Sigstore transparency entry: 2569979868
- Sigstore integration time:
-
Permalink:
alexander-akhmetov/python-telegram@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/alexander-akhmetov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_telegram-2.0.0-py3-none-macosx_11_0_arm64.whl.
File metadata
- Download URL: python_telegram-2.0.0-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 10.6 MB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67491bc97235cc93165068aa2f37b5eeb53206bdeb64cd0020c1f21fb951caed
|
|
| MD5 |
15d35946ec18a1ba310978c6b9c6b436
|
|
| BLAKE2b-256 |
525e91858b231e4383c6a5d207b9458ff1e2ebdffd4a84f09b09ff9d3dd7d10d
|
Provenance
The following attestation bundles were made for python_telegram-2.0.0-py3-none-macosx_11_0_arm64.whl:
Publisher:
release.yml on alexander-akhmetov/python-telegram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_telegram-2.0.0-py3-none-macosx_11_0_arm64.whl -
Subject digest:
67491bc97235cc93165068aa2f37b5eeb53206bdeb64cd0020c1f21fb951caed - Sigstore transparency entry: 2569980049
- Sigstore integration time:
-
Permalink:
alexander-akhmetov/python-telegram@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/alexander-akhmetov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_telegram-2.0.0-py3-none-macosx_10_15_x86_64.whl.
File metadata
- Download URL: python_telegram-2.0.0-py3-none-macosx_10_15_x86_64.whl
- Upload date:
- Size: 11.5 MB
- Tags: Python 3, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0abaea7a48e494260c384490d1fa5938ab838dfa285a97098440a8f3dc469e55
|
|
| MD5 |
c64224d3c8f75737f35044affc7328a1
|
|
| BLAKE2b-256 |
960ae1dd574af0ec1cb0f29a4aaa1e7de0ab7eb10d9befddc5076aa6ff2d1333
|
Provenance
The following attestation bundles were made for python_telegram-2.0.0-py3-none-macosx_10_15_x86_64.whl:
Publisher:
release.yml on alexander-akhmetov/python-telegram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_telegram-2.0.0-py3-none-macosx_10_15_x86_64.whl -
Subject digest:
0abaea7a48e494260c384490d1fa5938ab838dfa285a97098440a8f3dc469e55 - Sigstore transparency entry: 2569979973
- Sigstore integration time:
-
Permalink:
alexander-akhmetov/python-telegram@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Branch / Tag:
refs/tags/2.0.0 - Owner: https://github.com/alexander-akhmetov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a6bf6a82c60ab9f7a70e8ca5ffbf2b820294c680 -
Trigger Event:
push
-
Statement type: