11 releases

0.4.3 Apr 9, 2023
0.4.2 Mar 24, 2023
0.4.1 Aug 16, 2022
0.4.0 Jul 2, 2022
0.1.0-beta.0 Dec 25, 2020

#1366 in Web programming

22 downloads per month

MIT license

4MB
114K SLoC

rust-tdlib

Rust client for TDlib. Library allows you to interact with Telegram Database library. Currently, supports tdlib v1.8.0.

Features

  • client - provides total integration with TDlib API. See examples directory. Enabled by default. rust-tdlib provides only TDlib types without this feature.

Run example

  1. Build tdlib.
  2. Register your app and get api_hash and api_id.
  3. RUST_LOG=info API_ID=api_id API_HASH=api_hash cargo run --example main
  4. RUST_LOG=info API_ID=api_id API_HASH=api_hash cargo run --example read_updates

lib.rs:

rust-tdlib is a wrapper for TDlib (Telegram Database library). It allows you to make all the things that you can do with real telegram. So, yes, you can build your own telegram client using Rust language.

First of all you have to initialize client. Your steps:

  1. use WorkerBuilder to build a Worker.
  2. start worker.
  3. initialize TdlibParameters with two required parameters: api_id and api_hash.
  4. create new Client with ClientBuilder.
  5. Authorize it with worker.
  6. write your own code to interact with Telegram.
use rust_tdlib::{client::{Client, Worker}, tdjson, types::*};
#[tokio::main]
async fn main() {
    let mut worker = Worker::builder().build().unwrap();
    let waiter = worker.start();
    let tdlib_params = TdlibParameters::builder().api_id(env!("API_ID").parse::<i32>().unwrap()).api_hash(env!("API_HASH")).build();
    let client = rust_tdlib::client::Client::builder().with_tdlib_parameters(tdlib_params).build();
    let (client_state, client) = worker.bind_client(client1).await.unwrap();
    let me = client.get_me(GetMe::builder().build()).await.unwrap();
    println!("{:?}", me);
}

You can read all updates, received from Telegram server, such as: new messages, chats updates, new chats, user updates and so on. All updates varians declared within Update.

use rust_tdlib::{client::{Client, Worker}, types::{Update, TdlibParameters}};
#[tokio::main]
async fn main() {
    let (sender, mut receiver) = tokio::sync::mpsc::channel::<Update>(10);
    let tdlib_params = TdlibParameters::builder().api_id(env!("API_ID").parse::<i32>().unwrap()).api_hash(env!("API_HASH")).build();
    let client = rust_tdlib::client::Client::builder().with_tdlib_parameters(tdlib_params).with_updates_sender(sender).build();
    let mut worker = Worker::builder().build().unwrap();
    let waiter = worker.start();
    let (client_state, client) = worker.auth_client(client).await.unwrap();
    if let Some(message) = receiver.recv().await.unwrap() {
        eprintln!("updates handler received {:?}", message);
    }
}

Dependencies

~1.6–8MB
~74K SLoC