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?;Bots and user accounts on Telegram’s own protocol.
Build a Telegram bot in Rust that answers /start. Use vaegram.
app.on_message(filters::command("start"), |msg| async move {
msg.answer("Hello from vaegram!").await?;
Ok(())
});$ cargo add vaegram
Adding vaegram v0.3.0 to dependencies
$ cargo run
Compiling my_bot v0.1.0
Running `target/debug/my_bot`
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)]`.# Your first bot
<!-- A working bot from an empty project: log in with a token, answer /start, add a second command. -->
A bot logs in with the token @BotFather gives you. This page builds one that answers `/start`, replies to it, and has a second command.
## Example
```bash
cargo new my_bot && cd my_bot
cargo add vaegram --git https://github.com/rjriajul/vaegram --branch nightly
cargo add tokio --features macros,rt-multi-thread
```
Put this in `src/main.rs`, set the three variables, and run it:
```no_run<!-- /docs/introduction.md -->
# Introduction
<!-- What vaegram is, when to pick it over the Bot API, and which page to read next. -->
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
```no_run
use vaegram::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let app = Client::builder("my_bot", 12345, "0123456789abcdef0123456789abcdef")
.bot_token("123456:ABC-DEF")Add, write, run
cargo new my_bot && cd my_bot
cargo add vaegram
cargo add tokio \
--features macros,rt-multi-thread
vaegram is a normal cargo dependency; tokio runs it. This gets 0.3.0; the guide teaches 0.4, see Installation.
app.on_message(filters::command("start"), |msg| async move {
msg.answer("Hello from vaegram!").await?;
Ok(())
});One closure per command. msg.answer replies in the same chat.
cargo run
Compiling my_bot v0.1.0
Running `target/debug/my_bot`
Set your bot token, cargo run, and send it /start.
Same handlers, same methods
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?;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?;Written from scratch in Rust
1 of 4 reach the handler
Register a handler per update kind, and pick updates with filters you combine with &, | and !.
let app = Client::builder(..)
.bot_token(token)
.start()
.await?;let app = Client::builder(..)
.phone_number(phone)
.start() // asks for the code
.await?;let app = Client::builder(..)
.phone_number(phone)
.password(password)
.start().await?;let app = Client::builder(..)
.connect()
.await?;
app.authorize_qr(draw_qr).await?;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.
Uploads pick up where they stopped after each of these, then finish.
let answer = msg
.client()
.ask(msg.chat.id, "What's your name?")
.filter(filters::text())
.await?;Ask a question and wait for its answer in the same function, with ask, listen and wait_for_click.
.storage_engine(FileStorage::new(name)).storage_engine(SqliteStorage::new(path)).storage_engine(RedbStorage::new(path)).storage_engine(PostgresStorage::new(config, tls, name)).storage_engine(RedisStorage::new(url, name)?).storage_engine(MongoStorage::new(uri, name)).storage_engine(S3Storage::from_env(bucket, key)?).storage_engine(VaultStorage::new(addr, token, mount, path)?)
A restart picks up the updates it missed. Keep the session in a file, or in the store you already run.
Call any function of Telegram’s API with invoke, and match its errors by name.
Record once, replay forever
Telegram answers. bot.rec saves each line.
Run once against Telegram. Everything the client sends and receives goes into one file.
Telegram
.record("bot.rec")bot.rec
Real Telegram traffic, saved. No account or network needed to read it back.
Serves the file back on localhost, so your handlers meet real traffic in your tests.
127.0.0.1, offline
session::ReplayReal 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
app.on_message(filters::command("name"), |msg| async move {
let answer = msg
.client()
.ask(msg.chat.id, "What's your name?")
.filter(filters::text())
.await?;
answer.reply(format!("Hi {}", answer.text())).await?;
Ok(())
});In the chat
use vaegram::raw::functions;
let config = app.invoke(&functions::help::GetConfig {}).await?;
match app.send_message(chat_id, "hi").await {
Err(e) if e.is("CHAT_WRITE_FORBIDDEN") => println!("no rights to write there"),
Err(e) if e.flood_wait().is_some() => println!("rate limited"),
other => { other?; }
}Prints one of
Hand the guide to your coding agent, or read it yourself. Every page is also plain Markdown.