Skip to main content

Async MTProto client library for Telegram user accounts and bots

Project description

sent

PyPI version Python versions License: MIT CI

sent is an async MTProto client library for Telegram. It speaks the raw Telegram protocol and supports both user accounts and bots with an event-driven, developer-friendly API.

Features

  • Async MTProto 2.0 — encryption, sessions, RPC with RpcResult / MsgContainer handling
  • User accounts and bots — phone + code login, SRP 2FA, bot token, QR login
  • Event systemNewMessage, MessageDeleted, MessageRead, CallbackQuery, ChatAction, UserUpdate, Album
  • Friendly APIsend_message, send_file, get_dialogs, iter_messages, conversation()
  • TL layer 225 — auto-generated from the latest Telegram schema
  • Session storage — SQLite (with entity cache), StringSession, or in-memory
  • Transport — TCP abridged / intermediate / full, SOCKS5 proxy support

Installation

pip install sent

Optional extras for faster cryptography:

pip install sent[fast]    # cryptg (recommended for production)
pip install sent[crypto]  # pycryptodome fallback

For local development:

git clone https://github.com/kalanakt/sent.git
cd sent
uv sync --all-extras

Quick start

Get your api_id and api_hash from my.telegram.org.

Bot

import asyncio
import os

from sent import TelegramClient, events

API_ID = int(os.environ["TELEGRAM_API_ID"])
API_HASH = os.environ["TELEGRAM_API_HASH"]
BOT_TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]


async def main():
    client = TelegramClient("echo_bot", API_ID, API_HASH)

    @client.on(events.NewMessage)
    async def handler(event):
        await event.reply(event.text)

    await client.start(bot_token=BOT_TOKEN)
    await client.run_until_disconnected()


asyncio.run(main())

See examples/echo_bot.py for a complete echo bot.

User account

import asyncio
import os

from sent import TelegramClient, events

API_ID = int(os.environ["TELEGRAM_API_ID"])
API_HASH = os.environ["TELEGRAM_API_HASH"]


async def main():
    client = TelegramClient("my_account", API_ID, API_HASH)

    @client.on(events.NewMessage(incoming=True))
    async def handler(event):
        await event.reply("Hello!")

    await client.start()  # prompts for phone, code, and 2FA when needed
    me = await client.get_me()
    print(f"Logged in as {me.first_name}")
    await client.run_until_disconnected()


asyncio.run(main())

See examples/user_bot.py for a fuller user-account example.

Conversation helper

async with client.conversation("username") as conv:
    await conv.send_message("What's your name?")
    response = await conv.get_response()
    print(response.text)

Development

uv sync --group test
uv run pytest test/ -v

Run benchmarks:

uv run python bench/run.py

Regenerate TL types after updating the schema:

python -m sent._codegen.generate

See CONTRIBUTING.md for full contributor and release guidelines.

Project links

License

MIT — see 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

sent-1.0.0.tar.gz (365.8 kB view details)

Uploaded Source

Built Distribution

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

sent-1.0.0-py3-none-any.whl (409.8 kB view details)

Uploaded Python 3

File details

Details for the file sent-1.0.0.tar.gz.

File metadata

  • Download URL: sent-1.0.0.tar.gz
  • Upload date:
  • Size: 365.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sent-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b1a61ccc7fc4c8daf9ba40415f5ebf1ed5faeebc9cfa077dcb7605ef14d0396f
MD5 d33eb82d9db67350750627d00da06032
BLAKE2b-256 965277c1106aa7a8c35b54502efde1ea62348283aff47ffa890ed3e7b2294964

See more details on using hashes here.

Provenance

The following attestation bundles were made for sent-1.0.0.tar.gz:

Publisher: publish.yml on kalanakt/sent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sent-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sent-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 409.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sent-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 614872f1c94c8f63ab42a84e3217d52e52758a06e47dd27bdd9b2436296a1319
MD5 04189ff393c94fd49a464ae6e1d8fda5
BLAKE2b-256 b720b54ec206b05bbc9493a9ccc3b63810d33c8f24d73450de8c8a5744152ea8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sent-1.0.0-py3-none-any.whl:

Publisher: publish.yml on kalanakt/sent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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