Skip to main content

Crate ferogram_session

Crate ferogram_session 

Source
Expand description

Session persistence types and storage backends for ferogram.

This crate is part of ferogram, an async Rust MTProto client built by Ankit Chaubey.

Most users do not use this crate directly. The ferogram crate wires it up automatically when you call Client::builder().session(...) or .session_string(...).

§What’s in here

  • PersistedSession: the serializable session struct. Holds the DC table (one AuthKey + salt + time offset per DC), update sequence counters (PTS, QTS, date, seq), and the peer access-hash cache.
  • SessionBackend: the trait all backends implement. A single method: save(&PersistedSession) and load() -> Option<PersistedSession>.
  • BinaryFileBackend: stores the session as a binary file on disk. Good for bots and scripts. No extra dependencies.
  • InMemoryBackend: keeps everything in memory. Nothing survives process exit. Used for tests and ephemeral tasks.
  • StringSessionBackend: serializes the session to a base64 string. Useful for serverless environments where you store state in an env var or database column. Load it with Client::builder().session_string(s).
  • SqliteBackend: SQLite-backed storage via rusqlite. Behind the sqlite-session feature flag. Good for local multi-account tooling.
  • [LibSqlBackend]: libSQL / Turso backend. Behind libsql-session. For distributed or edge-hosted session storage.

You can also implement SessionBackend yourself for Redis, PostgreSQL, or anything else.

§Binary format

The file backends start with a version byte:

  • 0x01: legacy (DC table only, no update state or peer cache).
  • 0x02: current (DC table + update state + peer cache).

load() handles both. save() always writes v2.

§Example: export and re-import a session

// Export
let s = client.export_session_string().await?;

// Later, in another process or after a restart:
let (client, _) = ferogram::Client::builder()
    .api_id(12345)
    .api_hash("api_hash")
    .session_string(s)
    .connect()
    .await?;

Structs§

BinaryFileBackend
Stores the session in a compact binary file (v2 format).
CachedMinPeer
A min-user context entry: the user was seen with min=true (access_hash not usable directly) so we store the peer+message where they appeared so that InputPeerUserFromMessage can be constructed on restart.
CachedPeer
A cached access-hash entry so that the peer can be addressed across restarts without re-resolving it from Telegram.
DcEntry
One entry in the DC address table.
DcFlags
Per-DC option flags.
InMemoryBackend
Ephemeral in-process session: nothing persisted to disk.
PersistedSession
Everything that needs to survive a process restart.
SqliteBackendsqlite-session
SQLite-backed session (via rusqlite).
StringSessionBackend
Portable base64 string session backend.
UpdatesStateSnap
Snapshot of the MTProto update-sequence state that we persist so that catch_up: true can call updates.getDifference with the pre-shutdown pts.

Enums§

UpdateStateChange
A single update-sequence change, applied via SessionBackend::apply_update_state.

Traits§

SessionBackend
Synchronous snapshot backend: saves and loads the full session at once.

Functions§

default_dc_addresses
Bootstrap DC address table (fallback if GetConfig fails).