Skip to main content

aiotdlib - Python asyncio Telegram client based on TDLib

PyPI version shields.io PyPI pyversions PyPI license

This wrapper is actual for TDLib v1.8.46

This package includes prebuilt TDLib binaries for macOS (arm64) and Debian (amd64). You can use your own binary by passing library_path argument to Client class constructor. Make sure it's built from this commit. Compatibility with other versions of library is not guaranteed.

Features

  • All types and functions are generated automatically from tl schema
  • All types and functions come with validation and good IDE type hinting (thanks to Pydantic)
  • A set of high-level API methods which makes work with tdlib much simpler

Requirements

  • Python 3.9+
  • Get your api_id and api_hash. Read more in Telegram docs

Installation

PyPI

pip install aiotdlib

or if you use Poetry

poetry add aiotdlib

Examples

Base example

import asyncio
import logging

from aiotdlib import Client, ClientSettings

API_ID = 123456
API_HASH = ""
PHONE_NUMBER = ""


async def main():
    client = Client(
        settings=ClientSettings(
            api_id=API_ID,
            api_hash=API_HASH,
            phone_number=PHONE_NUMBER
        )
    )

    async with client:
        me = await client.api.get_me()
        logging.info(f"Successfully logged in as {me.model_dump_json()}")


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

Any parameter of Client class could be also set via environment variables with prefix AIOTDLIB_*.

import asyncio
import logging

from aiotdlib import Client


async def main():
    async with Client() as client:
        me = await client.api.get_me()
        logging.info(f"Successfully logged in as {me.model_dump_json()}")


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

and run it like this:

export AIOTDLIB_API_ID=123456
export AIOTDLIB_API_HASH=<my_api_hash>
export AIOTDLIB_BOT_TOKEN=<my_bot_token>
python main.py

Events handlers

import asyncio
import logging

from aiotdlib import Client, ClientSettings
from aiotdlib.api import API, BaseObject, UpdateNewMessage

API_ID = 123456
API_HASH = ""
PHONE_NUMBER = ""


async def on_update_new_message(client: Client, update: UpdateNewMessage):
    chat_id = update.message.chat_id

    # api field of client instance contains all TDLib functions, for example get_chat
    chat = await client.api.get_chat(chat_id)
    logging.info(f'Message received in chat {chat.title}')


async def any_event_handler(client: Client, update: BaseObject):
    logging.info(f'Event of type {update.ID} received')


async def main():
    client = Client(
        settings=ClientSettings(
            api_id=API_ID,
            api_hash=API_HASH,
            phone_number=PHONE_NUMBER
        )
    )

    # Registering event handler for 'updateNewMessage' event
    # You can register many handlers for certain event type
    client.add_event_handler(on_update_new_message, update_type=API.Types.UPDATE_NEW_MESSAGE)

    # You can register handler for special event type "*". 
    # It will be called for each received event
    client.add_event_handler(any_event_handler, update_type=API.Types.ANY)

    async with client:
        # idle() will run client until it's stopped
        await client.idle()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

Bot command handler

import logging

from aiotdlib import Client, ClientSettings
from aiotdlib.api import UpdateNewMessage

API_ID = 123456
API_HASH = ""
BOT_TOKEN = ""

bot = Client(
    settings=ClientSettings(
        api_id=API_ID,
        api_hash=API_HASH,
        bot_token=BOT_TOKEN
    )
)

# Note: bot_command_handler method is universal and can be used directly or as decorator
# Registering handler for '/help' command
@bot.bot_command_handler(command='help')
async def on_help_command(client: Client, update: UpdateNewMessage):
    # Each command handler registered with this method will update update.EXTRA field
    # with command related data: {'bot_command': 'help', 'bot_command_args': []}
    await client.send_text(update.message.chat_id, "I will help you!")


async def on_start_command(client: Client, update: UpdateNewMessage):
    # So this will print "{'bot_command': 'help', 'bot_command_args': []}"
    print(update.EXTRA)
    await client.send_text(update.message.chat_id, "Have a good day! :)")


async def on_custom_command(client: Client, update: UpdateNewMessage):
    # So when you send a message "/custom 1 2 3 test" 
    # So this will print "{'bot_command': 'custom', 'bot_command_args': ['1', '2', '3', 'test']}"
    print(update.EXTRA)


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    # Registering handler for '/start' command
    bot.bot_command_handler(on_start_command, command='start')
    bot.bot_command_handler(on_custom_command, command='custom')
    bot.run()

Proxy

import asyncio
import logging

from aiotdlib import Client
from aiotdlib import ClientSettings
from aiotdlib import ClientProxySettings
from aiotdlib import ClientProxyType

API_ID = 123456
API_HASH = ""
PHONE_NUMBER = ""


async def main():
    client = Client(
        settings=ClientSettings(
            api_id=API_ID,
            api_hash=API_HASH,
            phone_number=PHONE_NUMBER,
            proxy_settings=ClientProxySettings(
                host="10.0.0.1",
                port=3333,
                type=ClientProxyType.SOCKS5,
                username="aiotdlib",
                password="somepassword",
            )
        )
    )

    async with client:
        await client.idle()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

Middlewares

import asyncio
import logging

from aiotdlib import Client, HandlerCallable, ClientSettings
from aiotdlib.api import API, BaseObject, UpdateNewMessage

API_ID = 12345
API_HASH = ""
PHONE_NUMBER = ""


async def some_pre_updates_work(event: BaseObject):
    logging.info(f"Before call all update handlers for event {event.ID}")


async def some_post_updates_work(event: BaseObject):
    logging.info(f"After call all update handlers for event {event.ID}")


# Note that call_next argument would always be passed as keyword argument,
# so it should be called "call_next" only.
async def my_middleware(client: Client, event: BaseObject, /, *, call_next: HandlerCallable):
    # Middlewares useful for opening database connections for example
    await some_pre_updates_work(event)

    try:
        await call_next(client, event)
    finally:
        await some_post_updates_work(event)


async def on_update_new_message(client: Client, update: UpdateNewMessage):
    logging.info('on_update_new_message handler called')


async def main():
    client = Client(
        settings=ClientSettings(
            api_id=API_ID,
            api_hash=API_HASH,
            phone_number=PHONE_NUMBER
        )
    )

    client.add_event_handler(on_update_new_message, update_type=API.Types.UPDATE_NEW_MESSAGE)

    # Registering middleware.
    # Note that middleware would be called for EVERY EVENT.
    # Don't use them for long-running tasks as it could be heavy performance hit
    # You can add as much middlewares as you want. 
    # They would be called in order you've added them
    client.add_middleware(my_middleware)

    async with client:
        await client.idle()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

LICENSE

This project is licensed under the terms of the MIT license.

Metadata

Release files for aiotdlib 0.27.6

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

Source distribution (sdist)

Source distribution for aiotdlib 0.27.6
File Size Uploaded
aiotdlib-0.27.6.tar.gz 540.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for aiotdlib 0.27.6
File
aiotdlib-0.27.6-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details
aiotdlib-0.27.6-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details
aiotdlib-0.27.6-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details
aiotdlib-0.27.6-py3-none-macosx_10_9_x86_64.whl Python 3 none macOS 10.9+ x86-64 Details

Total release size: 38.9 MB

Release files / aiotdlib-0.27.6.tar.gz

Download URL aiotdlib-0.27.6.tar.gz
Size 540.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c8ee660b5b518acc1c9924ce9565a9e973c56e7de44a0afa4c9a89093097c5aa
BLAKE2b-256 checksum
How to use checksums
610335b5f215658d51150e6a92a2a60a1a11971a3009d0d12622bfc40b43f267
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 8, 2025.

Transparency log

Release files / aiotdlib-0.27.6-py3-none-manylinux_2_28_x86_64.whl

Download URL aiotdlib-0.27.6-py3-none-manylinux_2_28_x86_64.whl
Size 13.1 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
bb1b22c68e0e1eb330a8fe4512d250e49db154c7cbb1cfafbf435716397b6e91
BLAKE2b-256 checksum
How to use checksums
7e4e9640b4a3da6ad14e55ddf4e822dc1fc1b74ddd8bc2619de050a9462934b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 8, 2025.

Transparency log

Release files / aiotdlib-0.27.6-py3-none-manylinux_2_28_aarch64.whl

Download URL aiotdlib-0.27.6-py3-none-manylinux_2_28_aarch64.whl
Size 12.0 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
2db93c4e39fc759eed5af8c0562826d5b960173ea8c510b5c17a7d5205ef9b22
BLAKE2b-256 checksum
How to use checksums
37375264c65db8c8a0d574d88a2dd93a79d6d2edc3958f2f8d1649a5a5719abf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 8, 2025.

Transparency log

Release files / aiotdlib-0.27.6-py3-none-macosx_11_0_arm64.whl

Download URL aiotdlib-0.27.6-py3-none-macosx_11_0_arm64.whl
Size 12.3 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e44629738cdfebb041ac360861f832faeefb9944431c2e233995c7a96ae1f080
BLAKE2b-256 checksum
How to use checksums
52aaf8cdafa72b3aa45d3371c0904eb9a5c816a87533813ba1f9b841b03b573c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 8, 2025.

Transparency log

Release files / aiotdlib-0.27.6-py3-none-macosx_10_9_x86_64.whl

Download URL aiotdlib-0.27.6-py3-none-macosx_10_9_x86_64.whl
Size 942.9 kB
Tags Python 3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d8618aedd62284e2080296b91977f9cb7e23a874ae81e77897c758d0705adedc
BLAKE2b-256 checksum
How to use checksums
39b3ad8809598ac02bc63bbf0bae981d7b316e1781a1faf3c84c6374ca2f4008
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 8, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.27.6 This release

5 release files

0.27.4

5 release files

0.27.3

4 release files

0.27.2

4 release files

0.27.1

4 release files

0.27.0

4 release files

0.26.0

4 release files

0.24.3

2 release files

0.24.2

2 release files

0.24.1

2 release files

0.24.0

2 release files

0.23.4

2 release files

0.23.3

2 release files

0.23.2

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.0

2 release files

0.19.2

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.0

2 release files

0.16.0

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.2

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.2

2 release files

0.9.0

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

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