Skip to main content

Crate layer_client

Crate layer_client 

Source
Expand description

§layer-client — Production-grade async Telegram client

A fully async, production-ready Telegram client built on top of the MTProto protocol. Inspired by and architecturally aligned with grammers.

§Features

  • ✅ Full async/tokio I/O
  • ✅ User login (phone + code + 2FA SRP)
  • ✅ Bot token login (bot_sign_in)
  • FLOOD_WAIT auto-retry with configurable policy
  • ✅ Update stream (stream_updates) with typed events
  • ✅ Raw update access
  • NewMessage, MessageEdited, MessageDeleted, CallbackQuery, InlineQuery
  • ✅ Callback query answering
  • ✅ Inline query answering
  • ✅ Dialog iteration (iter_dialogs)
  • ✅ Message iteration (iter_messages)
  • ✅ Peer resolution (username, phone, ID)
  • ✅ Send / edit / delete messages
  • ✅ Forward messages
  • ✅ DC migration handling
  • ✅ Session persistence
  • ✅ Sign out

§Quick Start — User Account

use layer_client::{Client, Config, SignInError};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Client::connect(Config {
        session_path: "my.session".into(),
        api_id:       12345,
        api_hash:     "abc123".into(),
        ..Default::default()
    }).await?;

    if !client.is_authorized().await? {
        let token = client.request_login_code("+1234567890").await?;
        let code  = "12345"; // read from stdin

        match client.sign_in(&token, code).await {
            Ok(_)                                 => {}
            Err(SignInError::PasswordRequired(t)) => {
                client.check_password(t, "my_password").await?;
            }
            Err(e) => return Err(e.into()),
        }
        client.save_session().await?;
    }

    client.send_message("me", "Hello from layer!").await?;
    Ok(())
}

§Quick Start — Bot

use layer_client::{Client, Config, update::Update};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Client::connect(Config {
        session_path: "bot.session".into(),
        api_id:       12345,
        api_hash:     "abc123".into(),
        ..Default::default()
    }).await?;

    client.bot_sign_in("1234567890:ABCdef...").await?;
    client.save_session().await?;

    let mut updates = client.stream_updates();
    while let Ok(update) = updates.next().await {
        if let Update::NewMessage(msg) = update {
            if !msg.outgoing() {
                client.send_message_to_peer(msg.chat().unwrap().clone(), msg.text()).await?;
            }
        }
    }
    Ok(())
}

Re-exports§

pub use update::Update;

Modules§

update
High-level update types delivered by [crate::Client::next_update].

Structs§

AutoSleep
Automatically sleep on FLOOD_WAIT and retry once on I/O errors.
Client
The main Telegram client. Cheap to clone — internally Arc-wrapped.
Config
Configuration for Client::connect.
Dialog
A Telegram dialog (chat, user, channel).
LoginToken
Opaque token returned by crate::Client::request_login_code.
NoRetries
Never retry.
PasswordToken
Opaque 2FA challenge token returned in SignInError::PasswordRequired.
RetryContext
Context passed to RetryPolicy::should_retry on each failure.
RpcError
An error returned by Telegram’s servers in response to an RPC call.
UpdateStream
Asynchronous stream of Updates.
UpdatesConfiguration
Configuration for Client::stream_updates.

Enums§

InvocationError
The error type returned from any Client method that talks to Telegram.
SignInError
Errors returned by crate::Client::sign_in.

Traits§

RetryPolicy
Controls how the client reacts when an RPC call fails.