Creating a Telegram bot in Rust: a step-by-step guide for the bold

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

Telegram bot in Rust: why is it worth trying?

\n

Rust is not just another systems programming language. It offers incredible speed, memory safety without a garbage collector, and amazing concurrency. In a world where every millisecond of delay can cost you a user, a Telegram bot in Rust becomes a trump card for a developer. You get performance comparable to C++, but with a much more pleasant syntax and a strict type system that catches errors at compile time.

\n

In this guide, we will go from installing tools to publishing a working bot. You will learn how to handle commands, work with states, and even deploy the bot on a server. If you are already familiar with the basics of Rust but have never written bots — welcome. If you are new to Rust — don't worry, we will move step by step.

\n\n

What you need to get started

\n

Before diving into the code, make sure you have:

\n
    \n
  • Rust and Cargo — install via rustup (official website rustup.rs). Check the version: rustc --version
  • \n
  • Telegram bot token — get it from @BotFather in Telegram. Just send /newbot and follow the instructions
  • \n
  • Basic understanding of async/await — in Rust this is done via tokio or async-std
  • \n
\n

We will use the teloxide library — this is perhaps the most mature and documented framework for creating Telegram bots in Rust. It is built on tokio and provides a convenient API for all types of interactions.

\n\n

Step 1: Creating the project and installing dependencies

\n

Open your terminal and run:

\n
cargo new my_telegram_bot\ncd my_telegram_bot
\n

Now edit the Cargo.toml file, adding dependencies:

\n
[package]\nname = "my_telegram_bot"\nversion = "0.1.0"\nedition = "2021"\n\n[dependencies]\nteloxide = { version = "0.12", features = ["macros"] }\ntokio = { version = "1", features = ["full"] }\nlog = "0.4"\nenv_logger = "0.10"
\n

Explanation: teloxide is our main tool, tokio is the async runtime, log and env_logger are for convenient logging.

\n\n

Step 2: First launch — echo bot

\n

Let's write a simple bot that replies with the same text it received. Replace the contents of src/main.rs with:

\n
use teloxide::prelude::*;\n\n#[tokio::main]\nasync fn main() {\n    env_logger::init();\n    log::info!("Starting echo bot...");\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            bot.send_message(msg.chat.id, text).await?;\n        }\n        Ok(())\n    })\n    .await;\n}
\n

Note: Bot::from_env() looks for the environment variable TELOXIDE_TOKEN. Create a .env file in the project root or export the token manually:

\n
export TELOXIDE_TOKEN="your_bot_token"
\n

Run the bot: cargo run. Send it any message in Telegram — it should reply. Congratulations, your first Telegram bot in Rust is working!

\n\n

Step 3: Handling commands and filtering messages

\n

A simple echo is boring. Let's add /start and /help commands. We'll use teloxide macros to create a dialogue machine.

\n
use teloxide::prelude::*;\nuse teloxide::utils::command::BotCommand;\n\n#[derive(BotCommand)]\n#[command(rename = "lowercase", description = "Available commands:")]\nenum Command {\n    #[command(description = "Start communication")]\n    Start,\n    #[command(description = "Show help")]\n    Help,\n}\n\nasy                

Blogs

Book Recommendations