Skip to main content

Shirdal

A transport-agnostic, context-first Python framework for Telegram — featuring a built-in Bot API 10.5 engine and a native MTProto 2.0 core.

English | فارسی

Python 3.11+ PyPI CI License: LGPL v3


Most Telegram frameworks force a choice: either use a Bot API library that cannot touch MTProto, or an MTProto client with heavy, tightly-coupled internals. Shirdal decouples your application logic (Dispatcher, Context, filters, FSM, AntiFlood, hot-reload plugins, and storage) behind a single TransportAdapter protocol—and ships both a zero-HTTP-dependency Bot API 10.5 engine and a clean 5-layer native MTProto 2.0 core.

Architecture

  Your Handlers & Plugins (Context, Filters, FSM, Scheduler, MultiBot)
                                │
                        TransportAdapter
         ┌──────────────────────┼──────────────────────┐
         ▼                      ▼                      ▼
   BotAPIAdapter      NativeMTProtoAdapter    Telethon / Hydrogram / Mock
 (HTTP Keep-Alive +   (TL Codec, AES-256-IGE,   (Legacy MTProto wrappers
  WebhookServer)       TCP Abridged, PeerCache)  & offline test adapter)

Why Shirdal?

Capability Shirdal Pyrogram (Archived) Telethon aiogram
Transport independence Bot API + Native MTProto + Wrappers + Mock MTProto only MTProto only Bot API only
Bot API 9.4–10.5 (Colored Buttons, Custom Emoji, Rich Messages, Ephemeral, Draft Streaming) Native & typed Partial / raw N/A Requires external HTTP stack
Built-in Webhook Server asyncio HTTP/1.1 (WebhookServer, zero deps) N/A N/A Requires aiohttp / FastAPI
Native MTProto 2.0 Core 5-layer modular (shirdal.mtproto) + tlgen Monolithic Monolithic N/A
Production Primitives AntiFlood, SQLite/Redis/MySQL, SecretBox, Hot-Reload, MultiBot Manual Manual FSM + Middleware

Key Features

  • Built-in Bot API Engine (BotAPIAdapter): Keep-Alive HTTP/HTTPS client with optional msgspec JSON acceleration and a built-in asyncio WebhookServer (with X-Telegram-Bot-Api-Secret-Token verification and GET /health).
  • Bot API 10.5 Ready:
    • Colored & Icon Buttons: ButtonStyle (primary, success, danger), icon / icon_custom_emoji_id, disabled, copy_text, web_app_url, reply_button(), and reply_keyboard(..., force_reply=True).
    • Premium Emoji & Localized Time: custom_emoji(), custom_emoji_entity(), date_time(), and date_time_entity().
    • Ephemeral Group Replies & AI Streaming: ctx.reply_ephemeral(), async with ctx.stream() (sendMessageDraft), and async with ctx.stream_rich() (sendRichMessageDraft).
    • Rich Messages: ctx.reply_rich() and typed block builders (rich_heading, rich_table, rich_details, rich_blockquote, rich_code, rich_math, rich_buttons, rich_thinking).
    • Group Tags, Topics & Settings: ctx.set_member_tag(), create_forum_topic(), and set_my_settings().
  • Native MTProto 2.0 Core (shirdal.mtproto & NativeMTProtoAdapter):
    • Zero-copy TLReader/TLWriter + .tl schema codegen (generate_python_module) producing @dataclass(slots=True) models.
    • AES-256-IGE, MTProto 2.0 KDF, AuthKey, Pollard's rho factorization, and 3-step DH HandshakeSession.
    • TCPAbridgedTransport, MTProtoSessionState (SQLite persistence), MTProtoWire (msg_container, msgs_ack, bad_server_salt, gzip_packed, rpc_result).
    • PeerCache (access_hash persistence), UpdateStateTracker (pts/qts/seq gap detection), and parallel chunked FileTransferManager.
  • Resilience & Tooling: AntiFloodAdapter with FloodWaitError recovery, live hot-reload plugins/, StatesGroup FSM, EncryptedStorage (Encrypt-then-MAC), @bot.every() scheduler, MultiBot, and shirdal CLI (new, doctor, run).

Installation

pip install shirdal              # Includes Bot API 10.5 + Native MTProto 2.0 (cryptography)

# Optional extras
pip install "shirdal[fast]"      # uvloop + msgspec JSON speedups
pip install "shirdal[telethon]"  # Telethon adapter

Quickstart: Modern Bot API

import os
from shirdal import (
    BotAPIAdapter,
    ButtonStyle,
    Context,
    Shirdal,
    button,
    callback,
    command,
    custom_emoji,
    incoming,
    inline_keyboard,
)

bot = Shirdal(BotAPIAdapter(os.environ["BOT_TOKEN"]))

@bot.on(incoming & command("start", prefix="/"))
async def start(ctx: Context) -> None:
    badge = custom_emoji("5368324170671202286", "✨")
    kb = inline_keyboard([
        [
            button("Approve", "act:ok", style=ButtonStyle.SUCCESS, icon="5368324170671202286"),
            button("Reject", "act:no", style=ButtonStyle.DANGER),
        ]
    ])
    await ctx.reply(f"{badge} <b>Select an action:</b>", reply_markup=kb)

@bot.on_callback(callback(prefix="act:"))
async def on_action(ctx: Context) -> None:
    await ctx.answer("Recorded")
    await ctx.reply_ephemeral(f"Triggered: <code>{ctx.callback_data}</code>")

if __name__ == "__main__":
    bot.run()

Quickstart: Native MTProto 2.0

import os
from shirdal import Context, DeviceProfile, NativeMTProtoAdapter, Shirdal, command, outgoing

adapter = NativeMTProtoAdapter(
    int(os.environ["API_ID"]),
    os.environ["API_HASH"],
    session_path="account.sqlite",
    device=DeviceProfile.desktop(),
)

bot = Shirdal(adapter, plugins_dir="plugins", hot_reload=True)

@bot.on(command("ping") & outgoing)
async def ping(ctx: Context) -> None:
    await ctx.edit("Pong from Native MTProto 2.0", auto_delete=10)

if __name__ == "__main__":
    bot.run()

Documentation

Running Tests

PYTHONPATH=src python3 -m unittest discover -s tests -v

Security, Contributing & License

Metadata

Release files for shirdal 1.0.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 shirdal 1.0.6
File Size Uploaded Provenance Actions
shirdal-1.0.6.tar.gz 97.7 kB With provenance Details for shirdal-1.0.6.tar.gz

Built distribution (wheel)

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

Total release size: 181.4 kB

Release files / shirdal-1.0.6.tar.gz

Download URL shirdal-1.0.6.tar.gz
Size 97.7 kB
Tags Source
SHA-256 checksum
How to use checksums
85e9ff5d75515ecc263ccf2fabe960cd5141890b392917ee8cd9935517b22484
BLAKE2b-256 checksum
How to use checksums
e0a715c4ab05e6eed554b97a58027836659ffdd76ebacecca429d70e9b872994
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 26, 2026.

Transparency log

Release files / shirdal-1.0.6-py3-none-any.whl

Download URL shirdal-1.0.6-py3-none-any.whl
Size 83.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e63e198428284d029d1140ffee0c2bd7bb795ba677281ee6ee0bfab4f520141
BLAKE2b-256 checksum
How to use checksums
56cd2b792107566dd070f924ce4609b66c554c1a58bbc8bbc018820a67665f52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 26, 2026.

Transparency log

Security for shirdal 1.0.6

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for shirdal-1.0.6.tar.gz

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 Nov 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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