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

Guide

Your first user account

Log in as a person: a phone number, the code Telegram sends, and the 2FA password.

Your first user account

A user account is a logged-in session of a real Telegram account. It logs in with a phone number instead of a bot token, and then it can read its chats, send messages and run handlers like any other client.

Example

use vaegram::prelude::*;

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

    let me = app.get_me().await?;
    println!("logged in as {} (id {})", me.first_name, me.id);

    app.send_message("me", "Hello from vaegram").await?;
    Ok(())
}

Run it once. It asks for the phone number, the code Telegram sends, and the two-step verification password if the account has one. The next run reads my_account.session and asks nothing.

The login

start with no bot token on the builder asks on stdin, in this order:

  1. “Enter phone number or bot token”: the phone number in international format, + optional. A value with a colon in it is read as a bot token, so pasting a token logs in as that bot.
  2. The confirmation code, after “The confirmation code has been sent via …”. A wrong code is asked for again.
  3. Only when the number has no account yet: a first name, a last name, and whether to accept the terms.
  4. Only with two-step verification on: the password, after Telegram’s hint if it has one, read without echo when stdin is a terminal. A wrong password is asked for again too.

The answers come from stdin line by line, with the prompts written to stdout, so they can be piped in as well as typed. Every step can also be answered from the builder: .phone_number(..), .phone_code(..) and .password(..) are tried first, and whatever is missing is still asked.

Sending and reading

get_me() returns the account as a User: its id, first_name, username. "me" and "self" name the account itself wherever a chat id is taken, which is why send_message("me", ..) is the quickest way to check that a login works.

To read what arrives, register handlers and run:

app.on_message(filters::text(), |msg| async move {
    let from = msg.from_user.as_ref().map_or("unknown", |u| u.first_name.as_str());
    println!("{from} in chat {}: {}", msg.chat.id, msg.text());
    Ok(())
});

app.run().await

A user account gets messages from every chat it is in, so check msg.chat.id before acting. Messages this client sends do not come back to its own handlers; the same account’s messages from another session or device do.

Logging in without stdin

For a server, drive the login yourself: connect opens the connection without asking anything, then send_code, sign_in and, when the account has two-step verification, check_password:

use vaegram::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let app = Client::builder("my_account", 12345, "0123456789abcdef0123456789abcdef")
        .connect()
        .await?;

    let sent = app.send_code("+15551234567").await?;
    let me = match app.sign_in("+15551234567", &sent.phone_code_hash, "12345").await {
        Ok(me) => me,
        Err(e) if e.is_session_password_needed() => app.check_password("the password").await?,
        Err(e) => return Err(e),
    };
    println!("logged in as {}", me.first_name);
    Ok(())
}

send_code returns a SentCode; phone_code_hash must come back with the code, which is why an interactive login does these in two steps. sign_in fails with Error::SignUpRequired on a number without an account, and with SESSION_PASSWORD_NEEDED, as above, when a password is still missing.

Common mistakes

  • Messaging a peer the account has never seen. A user account can use a username it resolves, a chat it is in, or someone who wrote to it; a bare id of a stranger fails with PeerNotFound, where a bot could reach the user.
  • Running the program twice on one session file: the second fails with ResourceBusy, as with a bot.
  • Sharing or committing my_account.session. It is the login, and anyone with it is that account.
  • Expecting a conversation to survive a restart. A listener belongs to the client that registered it: run stopping ends the ones waiting, and one started outside a run fails with ListenerStopped.
  • Assuming one client is alone. Telegram sends every update to every session of the account, so another client on the same account also gets it and runs its own handlers.

See also