Telegram bot in Rust: why is it worth trying?
\nRust 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.
\nIn 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\nWhat you need to get started
\nBefore 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
/newbotand follow the instructions \n - Basic understanding of async/await — in Rust this is done via
tokioorasync-std\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\nStep 1: Creating the project and installing dependencies
\nOpen your terminal and run:
\ncargo new my_telegram_bot\ncd my_telegram_bot\nNow edit the Cargo.toml file, adding dependencies:
[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"\nExplanation: teloxide is our main tool, tokio is the async runtime, log and env_logger are for convenient logging.
Step 2: First launch — echo bot
\nLet's write a simple bot that replies with the same text it received. Replace the contents of src/main.rs with:
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}\nNote: Bot::from_env() looks for the environment variable TELOXIDE_TOKEN. Create a .env file in the project root or export the token manually:
export TELOXIDE_TOKEN="your_bot_token"\nRun the bot: cargo run. Send it any message in Telegram — it should reply. Congratulations, your first Telegram bot in Rust is working!
Step 3: Handling commands and filtering messages
\nA simple echo is boring. Let's add /start and /help commands. We'll use teloxide macros to create a dialogue machine.
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