# Installation

<!-- Add the crate, choose its features, and get the two numbers every login needs. -->

vaegram is a normal cargo dependency. An MTProto login needs an `api_id` and an `api_hash` from Telegram, and a
bot needs a token from [@BotFather](https://t.me/BotFather) as well.

## Example

This guide teaches 0.4, which is not on crates.io yet (crates.io has 0.3.0). Until it is, install it from the
`nightly` branch on GitHub, which carries 0.4.0-nightly.2:

```bash
cargo add vaegram --git https://github.com/rjriajul/vaegram --branch nightly
cargo add tokio --features macros,rt-multi-thread
```

```toml
[dependencies]
vaegram = { git = "https://github.com/rjriajul/vaegram", branch = "nightly" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
```

```no_run
use vaegram::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let app = Client::builder("my_bot", 12345, "0123456789abcdef0123456789abcdef")
        .bot_token("123456:ABC-DEF")
        .start()
        .await?;
    println!("logged in as {}", app.get_me().await?.first_name);
    Ok(())
}
```

## Rust version

The crate needs Rust 1.85 or newer and is an edition 2024 crate. `#[tokio::main]` needs tokio's `macros` and
`rt-multi-thread` features, as above.

## Features

No feature is on by default, so a build pulls only what you name. Add them where you add the crate:

```bash
cargo add vaegram --git https://github.com/rjriajul/vaegram --branch nightly --features serde,bot-api
```

The list of features with what each turns on is in the [introduction](/docs/introduction.md#features). Two worth
knowing before you start:

- `serde` makes the types you receive serializable, which most logging and storage code wants.
- A storage feature (`sqlite`, `redb`, `postgres`, `redis`, `mongodb`, `s3`, `vault`) replaces the session file
  with that engine. Without one, the client writes a file.

## api_id and api_hash

Both come from <https://my.telegram.org>: log in, open "API development tools", create an application, and copy
its `api_id` (a number) and `api_hash` (32 hex characters). They belong to the application, not to an account, so
one pair serves every bot and every user login that app makes. Bot API mode ignores them, since it has no session
with Telegram's servers, but the builder still takes them.

Pass them to `Client::builder(name, api_id, api_hash)`. The first argument is the client's name, and it decides
the session file's path: `"my_bot"` gives `my_bot.session` in the working directory, `"bots/echo"` gives
`bots/echo.session`. `ClientBuilder::workdir` picks the directory instead.

## The session file

The first run logs in and saves the login to `<name>.session`. Later runs read it and skip the login, so a bot
does not need its token in the code once the file exists, and a user account does not get a new code on every
restart. The file is created readable only by you (0600 on Unix). Bot API mode is the exception: it keeps peers
in memory and writes no session.

Two things follow from that:

- One client at a time can open a session file. While it is open the file has a `<name>.session.lock` next to
  it, and a second client on the same file, in this process or another, fails with `ResourceBusy`. A crash
  releases the lock; the empty `.lock` file stays.
- The session file is the login. Anyone with a copy is logged in as that account, so keep it out of git and out
  of logs. `ClientBuilder::in_memory()` writes nothing at all, and `export_session_string()` puts the login in one
  line for an environment variable.

## The short name

The `vae` crate is the same library under a shorter name: `vae = { git = "https://github.com/rjriajul/vaegram", branch = "nightly" }`
for now (same branch as below), then `use vae::Client;`. Everything is re-exported, `prelude` included. Only the `macros` feature needs a word: give a `#[derive(BotCommands)]` enum
`#[command(crate = "vae")]`, or the generated code names `vaegram`.

## Installing a nightly

0.4 is not on crates.io yet. The `nightly` branch moves as new nightlies land, and cargo records the exact commit in
`Cargo.lock`, so a build stays the same until you run `cargo update`. To pin one commit, use `rev` instead of
`branch`:

```toml
vaegram = { git = "https://github.com/rjriajul/vaegram", rev = "f4479fd" }
```

Once a nightly is on crates.io, cargo still skips it under `vaegram = "0.4"`, since pre-releases need asking for:
pin it with `=`, as in `vaegram = "=0.4.0-nightly.2"`. When 0.4 is stable, a plain `vaegram = "0.4"` follows the
releases.

## Common mistakes

- Calling `.bot_api(..)` or `#[derive(BotCommands)]` without the matching feature: the method does not exist and
  the compiler says so. Add `bot-api` or `macros`.
- Expecting `serde` to be on. Nothing is: a type that is not `Serialize` means the feature is missing.
- Running the program twice at once on one session file. Give each process its own name, or run one process.
- Committing `*.session`. It is the account's login.

## See also

- [API docs](https://docs.rs/vaegram/latest/vaegram/client/struct.ClientBuilder.html) for `ClientBuilder` and
  every setting.
- [Introduction](/docs/introduction.md) for the feature table and what vaegram is.
- [Sessions and storage](/docs/sessions-and-storage.md) for session strings, the engines and where the state lives.
- [Your first bot](/docs/first-bot.md) once the crate is in.