ferogram
Async Rust client for the Telegram MTProto API.
Built by Ankit Chaubey
This is the main client crate. It talks to Telegram directly over MTProto, no Bot API proxy in between, and handles auth for both bots and user accounts from the same client builder. You get a dispatcher with composable filters, FSM for multi-step conversations, CDN downloads, middleware, and a raw invoke() escape hatch for anything not wrapped yet.
If you're starting fresh, this is the only crate you need. Everything else in the workspace exists to support it and can be pulled in separately if you need a specific layer on its own. Want the Bot API instead? Take a look at ferobot.
[!NOTE] ferogram is still in active development. It covers major use cases and runs in production, but the API may still shift.
Installation
[]
= "0.6.4"
= { = "1", = ["full"] }
Get api_id and api_hash from my.telegram.org.
Optional feature flags:
= { = "0.6.4", = [
"sqlite-session", # SqliteBackend via rusqlite
"libsql-session", # LibSqlBackend via libsql-client (Turso)
"html", # parse_html / generate_html (built-in parser)
"html5ever", # parse_html via spec-compliant html5ever
"derive", # #[derive(FsmState)]
"serde", # serde support on session types
] }
Quick start: bot
use ;
const API_ID: i32 = 0;
const API_HASH: &str = "";
async
Quick start: user account
use Client;
const API_ID: i32 = 0;
const API_HASH: &str = "";
async
Examples
19 runnable examples covering everything from sending a message to your Saved Messages to a full FSM order bot.
See examples/README.md for the full list with descriptions and notes on when to use quick_connect vs Client::builder().
Connecting
quick_connect is the fast path. For anything more specific, use the builder:
use Client;
let = builder
.api_id
.api_hash
.session
.catch_up
.connect
.await?;
.catch_up(true) replays missed updates after a reconnect, and .retry_policy(...) / .restart_policy(...) let you customize retry and reconnect behavior. Session storage, transport, and proxy options each have their own section below. Full docs at docs.rs/ferogram.
Dispatcher and filters
use ;
let mut dp = new;
dp.on_message;
dp.on_message;
dp.on_message;
while let Some = stream.next.await
Filters compose with &, |, !. Built-ins cover command, private, group, channel, text, text_contains, media, photo, document, forwarded, reply, from_user, album, custom, and more. Callback queries and inline queries route through the same dispatcher via on_callback_query / on_inline_query / on_inline_send.
Middleware
dp.middleware;
Runs in registration order. Call next.run(upd) to pass control forward, or return early to stop the chain.
FSM
use ;
use Arc;
dp.with_state_storage;
dp.on_message_fsm;
MemoryStorage is built in. To persist state across restarts, implement StateStorage for Redis, a database, or anything else. State keys scope per-user, per-chat, or per-user-in-chat via StateKeyStrategy. See ferogram-fsm for details.
Session backends
builder.session // binary file (default)
builder.in_memory // no persistence
builder.session_string // base64 string
builder.session_backend // sqlite
builder.session_backend // turso
The base64 string backend is useful for serverless or containers where writing to disk isn't an option. To bring your own, implement SessionBackend from ferogram-session.
Transport and proxy
use TransportKind;
builder.transport // default
builder.transport // DPI bypass, plain MTProxy secrets
builder.transport // TLS camouflage, 0xee secrets
// MTProxy from a t.me link
builder.proxy_link
// SOCKS5
builder.socks5
// Race transports, use first to connect
builder.probe_transport
// Fall back through DoH + Telegram special-config if TCP is blocked
builder.resilient_connect
See ferogram-connect for the framing layer underneath.
Raw API
When the high-level API isn't enough, client.invoke() takes any TL function directly (current layer exposed as tl::LAYER). It's the escape hatch, not the normal path, but it's always there:
use tl;
let req = SetBotCommands ;
client.invoke.await?;
client.invoke_on_dc.await?; // target a specific DC
See ferogram-tl-types for all generated types and functions.
Error handling
use ;
match client.send_message.await
FLOOD_WAIT is handled automatically. To disable it:
use NoRetries;
builder.retry_policy
Shutdown
let = builder...connect.await?;
shutdown.cancel; // graceful
client.disconnect; // immediate
What's covered
See FEATURES.md for the full list with method signatures. If something is missing, open a feature request or drop by t.me/FerogramChat. the raw API is always there in the meantime.
Secret chats (end-to-end encrypted) are fully implemented but not published to crates.io yet. The plan is to release once there is enough community demand for it.
Voice and video calls
Group audio, video, and P2P calling are now fully implemented. To get started, check out the tgcalls crate and its examples in the tgcalls repository. It provides seamless integration between ferogram and the official ntgcalls Rust bindings for building Telegram voice and video calling applications.
Crates
Most people only need this crate. But each crate in the workspace is independently publishable if you need just one layer.
| Crate | What it does |
|---|---|
ferogram |
High-level client. Auth, messaging, media, dispatcher, FSM, middleware. |
ferogram-session |
Session types and pluggable storage backends (file, memory, SQLite, LibSQL, base64). |
ferogram-fsm |
FSM state storage and context. StateStorage trait, MemoryStorage, StateContext. |
ferogram-parsers |
Telegram Markdown and HTML entity parsers. |
ferogram-derive |
#[derive(FsmState)] proc macro. |
ferogram-mtsender |
DC connection pool and retry policy. AutoSleep, NoRetries, CircuitBreaker. |
ferogram-connect |
Raw TCP, MTProto framing, obfuscation, SOCKS5, MTProxy, gzip. |
ferogram-mtproto |
MTProto 2.0 session, DH key exchange, message framing, PFS key binding. |
ferogram-crypto |
AES-IGE, RSA, SHA, Diffie-Hellman, PQ factorization, auth key derivation. |
ferogram-tl-types |
Auto-generated TL types, functions, and enums (tracks tl::LAYER). |
ferogram-tl-gen |
Build-time code generator from TL AST to Rust source. |
ferogram-tl-parser |
Parses .tl schema text into a Definition AST. |
The rough dependency chain (build-critical path only):
ferogram
└ ferogram-mtsender
└ ferogram-connect
├ ferogram-mtproto
│ ├ ferogram-tl-types
│ │ └ (build) ferogram-tl-gen
│ │ └ (build) ferogram-tl-parser
│ └ ferogram-crypto
└ ferogram-crypto
Testing
Community
- Channel (releases, announcements): t.me/Ferogram
- Chat (questions, discussion): t.me/FerogramChat
- API docs: docs.rs/ferogram
- GitHub: github.com/ankit-chaubey/ferogram
License
MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.
Usage must comply with Telegram's API Terms of Service.