Chat bots in Rust: from idea to ready bot in one evening

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Rust is a shot in the "brains" of your chat bot

\n\n

If you're used to writing bots in Python and think it can't get any faster — you haven't tried Rust. Rust provides not just speed, but control over every byte. Chat bots in Rust work lightning fast, consume minimal memory, and don't crash with runtime errors. In this guide, we'll build a real Telegram bot that processes commands, responds to messages, and does it with performance unattainable by interpreted languages.

\n\n

We'll write the bot from scratch using the teloxide and reqwest crates. You'll see how easily asynchronous Rust handles thousands of requests, and why creating a chat bot in Rust is an investment in your project's stability.

\n\n

Environment setup and first launch

\n\n

Before writing code, make sure you have Rust installed. If not, run:

\n\n
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
\n\n

Add cargo to PATH and create a new project:

\n\n
cargo new rust_tg_bot\ncd rust_tg_bot
\n\n

Now open Cargo.toml and add dependencies:

\n\n
[dependencies]\nteloxide = { version = "0.12", features = ["macros"] }\nreqwest = { version = "0.11", features = ["json"] }\ntokio = { version = "1", features = ["full"] }\nserde = { version = "1.0", features = ["derive"] }
\n\n

These crates are the foundation of our bot. teloxide is a powerful framework for the Telegram API, and reqwest will allow us to make HTTP requests (e.g., to external APIs).

\n\n

Getting the token and writing the bot "skeleton"

\n\n

Go to @BotFather, create a new bot, and copy the token. Save it as an environment variable:

\n\n
export TELOXIDE_TOKEN=your_token_here
\n\n

Now let's write the simplest version of the bot that responds to the /start command:

\n\n
use teloxide::prelude::*;\n\n#[tokio::main]\nasync fn main() {\n    pretty_env_logger::init();\n    log::info!("Starting chat bot in Rust...");\n\n    let bot = Bot::from_env();\n\n    teloxide::repl(bot, |bot: Bot, msg: Message| async move {\n        if let Some(text) = msg.text() {\n            if text == "/start" {\n                bot.send_message(msg.chat.id, "Hello! I'm a bot in Rust 🦀")\n                    .await?;\n            }\n        }\n        Ok(())\n    })\n    .await;\n}
\n\n

Run cargo run and send the bot /start. If everything is fine — you'll receive a greeting. Note: writing a bot in Rust with teloxide requires minimal code but delivers maximum performance.

\n\n

How does it work?

\n
    \n
  • Bot::from_env() — reads the token from the TELOXIDE_TOKEN environment variable.
  • \n
  • teloxide::repl — an infinite loop for processing messages.
  • \n
  • Each message is passed to a closure where we check the text and send a response.
  • \n
\n\n

Adding command and argument handling

\n\n

A simple response is boring. Let's make an /echo command that repeats the user's text. For this, we'll use the Command annotation from teloxide:

\n\n
use teloxide::utils::command::BotCommand;\n\n#[derive(BotCommand)]\n#[command(rename = "lowercase", description = "Available commands:")]\nenum Command {\n    #[command(description = "Show greeting.")]\n    Start,\n    #[command(description = "Repeat your text.")]\n    Echo(String),\n}\n\nasync fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {\n    match cmd {\n        Command::Start => {\n            bot.send_message(msg.chat.id, "Hello! I'm an echo bot in Rust.").await?;\n        }\n        Command::Echo(text) => {\n            bot.send_message(msg.chat.id                

Blogs

Book Recommendations