Why Rust is a shot in the "brains" of your chat bot
\n\nIf 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\nWe'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.
Environment setup and first launch
\n\nBefore writing code, make sure you have Rust installed. If not, run:
\n\ncurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\n\nAdd cargo to PATH and create a new project:
cargo new rust_tg_bot\ncd rust_tg_bot\n\nNow open Cargo.toml and add dependencies:
[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\nThese 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).
Getting the token and writing the bot "skeleton"
\n\nGo to @BotFather, create a new bot, and copy the token. Save it as an environment variable:
\n\nexport TELOXIDE_TOKEN=your_token_here\n\nNow let's write the simplest version of the bot that responds to the /start command:
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\nRun 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.
How does it work?
\n- \n
Bot::from_env()— reads the token from theTELOXIDE_TOKENenvironment 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
Adding command and argument handling
\n\nA 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:
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