Skip to main content

Python asyncio wrapper for TDLib

Project description

aiotdlib - Python asyncio Telegram client based on TDLib

PyPI version shields.io PyPI pyversions PyPI license

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

Compatible with TDLib v1.7.4. Support for other versions of TDLib is not guaranteed

Prebuilt binary (v1.7.4) only for macOS included. You can use your own binary by passing library_path argument to Client class constructor

Requirements

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

Installation

pip install aiotdlib

Example

import asyncio
import logging

from aiotdlib import Client

API_ID = 5172829
API_HASH = "7ae1bef25a2194cf31d6321fd228cac2"
PHONE_NUMBER = ""


async def main():
    client = Client(
        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.json()}")


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

Events handlers

import asyncio
import logging

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

API_ID = 5172829
API_HASH = "7ae1bef25a2194cf31d6321fd228cac2"
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(
        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 asyncio
import logging

from aiotdlib import Client
from aiotdlib.api import UpdateNewMessage

API_ID = 5172829
API_HASH = "7ae1bef25a2194cf31d6321fd228cac2"
PHONE_NUMBER = ""

client = Client(
    api_id=API_ID,
    api_hash=API_HASH,
    phone_number=PHONE_NUMBER
)


async def on_start_command(client: Client, update: UpdateNewMessage):
    await client.send_text(update.message.chat_id, "Have a good day! :)")


# Note: bot_command_handler method is universal and can be used directly or as decorator
# Registering handler for '/help' command
@client.bot_command_handler(command='help')
async def on_help_command(client: Client, update: UpdateNewMessage):
    await client.send_text(update.message.chat_id, "I will help you!")


async def main():
    # Note: bot_command_handler method is universal and can be used directly or as decorator
    # Registering handler for '/start' command
    client.bot_command_handler(on_start_command, command='start')

    async with client:
        await client.idle()


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

Proxy

import asyncio
import logging

from aiotdlib import Client, ClientProxySettings, ClientProxyType

API_ID = 5172829
API_HASH = "7ae1bef25a2194cf31d6321fd228cac2"
PHONE_NUMBER = ""


async def main():
  client = Client(
    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())

LICENSE

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiotdlib-0.4.3.tar.gz (7.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiotdlib-0.4.3-py3-none-any.whl (8.0 MB view details)

Uploaded Python 3

File details

Details for the file aiotdlib-0.4.3.tar.gz.

File metadata

  • Download URL: aiotdlib-0.4.3.tar.gz
  • Upload date:
  • Size: 7.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for aiotdlib-0.4.3.tar.gz
Algorithm Hash digest
SHA256 9b0c44bb3a3685b004020b5c3af7dce6072b981a83bc754b5eae8650e96bc960
MD5 b54267d2e74191dc6fc4dc1c08a57c37
BLAKE2b-256 c38f19dc2b6c779d8db97832ef4df5da43c30df5cc320bc08ec678b78a6e0f02

See more details on using hashes here.

File details

Details for the file aiotdlib-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: aiotdlib-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 8.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for aiotdlib-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a53fa88ac7ad09724bb5965c3e979c9a9d8f20c48a6b0f5e377d2ff07cc25b91
MD5 29df629817e397dfa1a836bebf48e83a
BLAKE2b-256 5c100bb021c0d40ebb42d4250d6860779ae8026bb5b69b3bf25550bfbd166472

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page