Skip to content
Jingzhe

Webhook

Webhook support is available through the optional webhook feature and uses Axum 0.8. It does not pull an HTTP server into the default build:

Cargo.toml
jingzhe = { version = "0.1.0", features = ["webhook"] }
src/main.rs
use jingzhe::{Bot, Context, WebhookConfig, command};
#[command(description = "Start")]
async fn start(context: Context) -> anyhow::Result<()> {
context.reply("Jingzhe webhook received the command.").await?;
Ok(())
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let token = std::env::var("TELEGRAM_BOT_TOKEN")?;
let public_url = std::env::var("WEBHOOK_URL")?;
let secret = std::env::var("WEBHOOK_SECRET")?;
Bot::new(token)?
.command(start)
.run_webhook(
WebhookConfig::new(public_url)
.listen("127.0.0.1:3000")
.secret_token(secret),
)
.await?;
Ok(())
}

The public URL must use HTTPS when connected to the Telegram cloud API. The local listener defaults to 127.0.0.1:3000; TLS is normally terminated by a reverse proxy. With a local Bot API server, an HTTP URL can be enabled explicitly with .allow_http() on WebhookConfig.

run_webhook validates the configuration, binds the listener, and then explicitly calls setWebhook. It preserves pending updates by default and does not publish command menus automatically. On shutdown it waits for in-flight requests and does not delete the Telegram webhook.

Repository example:

Terminal
cargo run -p jingzhe --features webhook --example webhook

Set WEBHOOK_URL and WEBHOOK_SECRET.

Merge the webhook route into an existing Axum service with bot.webhook_router(&config)?:

let router = bot.webhook_router(&config)?.with_state(shared_state);

This method does not start a second server and does not call setWebhook. The application owns webhook publication, listening, reverse proxying, and lifecycle management.

See examples/axum_integration.rs.

The service validates X-Telegram-Bot-Api-Secret-Token and reserves processing capacity before reading the request body:

Condition Response
Missing or invalid authentication 401
Service at capacity 503
Body too large 413
Invalid JSON 400
Handler failure 500
Handler completed successfully 200

WebhookConfig defaults to concurrency 1 and a 1 MiB request-body limit. Concurrency can be set from 1–1024, and the body limit can be raised up to 16 MiB. Higher concurrency does not guarantee completion order matching update_id.