Quick start
Set the bot token
Section titled “Set the bot token”Create a bot with @BotFather, then put the token in the process environment:
export TELEGRAM_BOT_TOKEN='replace with your bot token'Repository examples also read a .env file in the working directory. Copying the template does not overwrite an existing file:
cp -n .env.example .envWrite the bot
Section titled “Write the bot”Save the following code as your application’s src/main.rs:
use anyhow::{Context as _, Result};use jingzhe::{Bot, Context, command};
#[command(description = "Start")]async fn start(context: Context) -> Result<()> { context.reply("Hello from Jingzhe!").await?; Ok(())}
#[tokio::main]async fn main() -> Result<()> { let token = std::env::var("TELEGRAM_BOT_TOKEN") .context("Set TELEGRAM_BOT_TOKEN to the token from @BotFather")?;
Bot::new(token)?.command(start).run_polling().await?;
Ok(())}Bot::new, command registration, and request construction do not access the network. Network operations start only when run_polling is awaited.
cargo runSend /start to the bot in Telegram and it should reply with the welcome message. Press Ctrl-C to stop long polling.
- Use API requests to call built-in Telegram methods directly.
- Use commands and state to register multiple commands and share application state.
- Use keyboards and callbacks to add inline buttons and typed callbacks.