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

Build anything on Telegram, in Rust.

Get started

Bots and user accounts on Telegram’s own protocol.

Your agentExample

Build a Telegram bot in Rust that answers /start. Use vaegram.

  1. Readllms.txt21 guide pages
  2. Readdocs/first-bot.mdMarkdown
  3. Wrotesrc/main.rs/start handler
    app.on_message(filters::command("start"), |msg| async move {
        msg.answer("Hello from vaegram!").await?;
        Ok(())
    });
Terminal

$ cargo add vaegram

Adding vaegram v0.3.0 to dependencies

$ cargo run

Compiling my_bot v0.1.0

Running `target/debug/my_bot`

my_botbot
  1. /start
  2. Hello from vaegram!
01

Docs your agent can read.

For coding agents

Every page ships twice: once for you, and once as plain text for the model writing code next to you.

# vaegram

> vaegram is a Rust client library for Telegram, speaking MTProto directly as a user account or as a bot, with an optional Bot API mode. This is the guide. API reference: <https://docs.rs/vaegram>.

## Introduction and setup

- [Introduction](/docs/introduction.md): what vaegram is, MTProto vs the Bot API, and which pages to read first.
- [Installation](/docs/installation.md): add the crate, pick features, get an api_id and api_hash.
- [Your first bot](/docs/first-bot.md): a bot that answers /start, built from an empty project.
- [Your first user account](/docs/first-user-account.md): log in as a person and send a message to yourself.

## Basics

- [Sending messages](/docs/sending-messages.md): text, formatting, replies, edits, and the return value of a send.
- [Receiving updates](/docs/receiving-updates.md): handlers, `run`, the update loop, and filtering what you get.
- [Filters](/docs/filters.md): pick updates with `filters::`, and combine them with `&`, `|` and `!`.
- [Commands](/docs/commands.md): parse `/command` text, and derive an enum from it with `#[derive(BotCommands)]`.
21 pages, one line eachOpen llms.txt
02

Or write it yourself, in three steps.

Add, write, run

  1. Terminal

    cargo new my_bot && cd my_bot

    cargo add vaegram

    cargo add tokio \

    --features macros,rt-multi-thread

    1

    Add the crate

    vaegram is a normal cargo dependency; tokio runs it. This gets 0.3.0; the guide teaches 0.4, see Installation.

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

    Write a handler

    One closure per command. msg.answer replies in the same chat.

  3. Terminal

    cargo run

    Compiling my_bot v0.1.0

    Running `target/debug/my_bot`

    • /start
    • Hello from vaegram!

    3

    Run it

    Set your bot token, cargo run, and send it /start.

Your first bot, step by step
03

One client, two kinds of account.

Same handlers, same methods

A bot

Logs in with a token from @BotFather and works from the updates Telegram sends it.

let app = Client::builder("my_bot", api_id, api_hash)
    .bot_token(token)
    .start()
    .await?;

A person’s account

Logs in with a phone number, the code Telegram sends and the 2FA password, then reaches its chats and history.

let app = Client::builder("my_account", api_id, api_hash)
    // no token: start() asks for the phone, code and 2FA
    .start()
    .await?;
04

Everything a Telegram client needs.

Written from scratch in Rust

filters::command("start")
  • private/start
  • group/start
  • privatehi
  • grouphi

1 of 4 reach the handler

Handlers and filters

Register a handler per update kind, and pick updates with filters you combine with &, | and !.

Log in any way

A bot with its token, a person with what Telegram asks for.

An upload that stops at a dead connection, a server error and a FLOOD_WAIT, and finishes.

video.mp4100%

Transfers that survive

Uploads pick up where they stopped after each of these, then finish.

src/main.rs
let answer = msg
    .client()
    .ask(msg.chat.id, "What's your name?")
    .filter(filters::text())
    .await?;

Conversations

Ask a question and wait for its answer in the same function, with ask, listen and wait_for_click.

online

.storage_engine(FileStorage::new(name))

Sessions that remember

A restart picks up the updates it missed. Keep the session in a file, or in the store you already run.

API layer234app.invoke(&GetConfig {})

Every raw function

Call any function of Telegram’s API with invoke, and match its errors by name.

05

Test your bot without Telegram.

Record once, replay forever

Telegram answers. bot.rec saves each line.

  1. Record

    Run once against Telegram. Everything the client sends and receives goes into one file.

    .record("bot.rec")
  2. bot.rec

    1. inhi
    2. outpong: hi
    3. in/start
    4. outpong: /start

    Real Telegram traffic, saved. No account or network needed to read it back.

  3. Replay

    Serves the file back on localhost, so your handlers meet real traffic in your tests.

    session::Replay
Read the testing guide
06

Read it in a minute.

Real code from the guide

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
}

In the chat

  1. /start
  2. Hello from vaegram!

Start with your first bot.

Hand the guide to your coding agent, or read it yourself. Every page is also plain Markdown.