Skip to content
↑ ↓ move↵ openEvery page also as Markdown: add .md

Guide

Introduction

What vaegram is, when to pick it over the Bot API, and which page to read next.

Introduction

vaegram is a Rust client for Telegram that speaks MTProto, the protocol the official apps use. You can run a bot with it, or a normal person’s account.

Example

use vaegram::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let app = Client::builder("my_bot", 12345, "0123456789abcdef0123456789abcdef")
        .bot_token("123456:ABC-DEF")
        .start()
        .await?;

    app.on_message(filters::command("start"), |msg| async move {
        msg.answer("Hello from vaegram!").await?;
        Ok(())
    });

    app.run().await
}

MTProto, and the Bot API

Telegram has two APIs. MTProto is the one its own apps use: a binary protocol over TCP with an encrypted session and a login key exchange, and the API on top of it has every method Telegram offers. vaegram implements it in Rust, generates a type for every function in Telegram’s schema, and puts a friendly API of methods, handlers and filters on top.

The Bot API is Telegram’s HTTPS wrapper around a smaller part of that. If you want it, the bot-api cargo feature runs the same Client over it: Client::builder(..).bot_api(token) keeps your handlers, filters and method calls and sends them as Bot API calls. Requests with no Bot API counterpart fail with Unsupported. The feature is experimental in the 0.4.0 nightlies, and a client uses one transport or the other; Bot API mode vs MTProto compares them.

User accounts and bots

Both use the same Client. A bot logs in with a token from @BotFather, set with .bot_token(..), and acts as a bot: it can reach most users who have talked to it, and it works from the updates Telegram sends it rather than from an account’s chats.

A user account logs in with a phone number, the code Telegram sends, and the two-step verification password if the account has one; leave out .bot_token(..) and start asks for them on stdin. It is a full client of that account: its chats and history, and any peer it has met, whether a chat it joined, a username it looked up, or a person who wrote to it.

Features

The crate has no default features: add the ones you use.

Feature What it enables
serde Serialize for the types you receive (Message, User, Chat, events, …).
json ToJson for every TL type and function, and JSON for raw updates and recordings.
bot-api The HTTP Bot API as a raw client, and as a transport for the whole Client.
webhook-server bot_api::Webhook::serve, an HTTP server that receives a bot’s webhook.
macros #[derive(BotCommands)], which writes commands::BotCommands for an enum.
sqlite, redb, redis, mongodb, postgres, s3, vault A storage engine for the session and update state.
dialogue-json Dialogue states kept in a JSON file.
regex filters::regex and filters::matches.
websocket ClientBuilder::websocket, MTProto over TLS on *.web.telegram.org.
dns-config ClientBuilder::dns_fallback, addresses from Telegram’s DNS config.
web-proxy Proxy::Web, an MTProxy behind an HTTPS relay.
metrics, opentelemetry Request observers in the observe module.
unstable-raw The TL objects behind raw updates, and the session plumbing below Client. May change in any release.

Where to go next

Read Installation first: it adds the crate and gets the api_id and api_hash that every login needs. After that, Your first bot if the account is a bot, or Your first user account if it is a person.

Common mistakes

  • Mixing up the two credentials. api_id and api_hash belong to the application and come from my.telegram.org; a bot token belongs to one bot and comes from @BotFather. Both are needed for a bot.
  • Planning a user account on the Bot API. Bot API mode logs in as a bot with a token, so a person’s account always uses MTProto.

See also

The whole guide

21 pages in 5 parts. Read them in order, or start at the part you need.