Skip to content
Jingzhe

Quick start

Create a bot with @BotFather, then put the token in the process environment:

Terminal
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:

Terminal
cp -n .env.example .env

Save the following code as your application’s src/main.rs:

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.

Terminal
cargo run

Send /start to the bot in Telegram and it should reply with the welcome message. Press Ctrl-C to stop long polling.