Skip to content
Jingzhe

Commands and events

src/main.rs
use jingzhe::{Context, command};
struct AppState {
greeting: String,
}
#[command(description = "Start")]
async fn start(context: Context<AppState>) -> anyhow::Result<()> {
context.reply(&context.state().greeting).await?;
Ok(())
}
#[command(name = "settings", description = "Open settings")]
async fn open_settings(context: Context<AppState>) -> anyhow::Result<()> {
context.reply("Settings").await?;
Ok(())
}

Command names default to the function name and can be overridden with name = "settings". If no description is provided, the command name is used. Names, descriptions, and signatures are checked at compile time.

Use #[event(UpdateEvent::InlineQuery)] to bind an event field to its model type at compile time. Register the generated definition with .event(...); no global scanning or network access occurs.

use jingzhe::{
AnswerInlineQuery, Bot, Context, InlineQuery, InlineQueryResultArticle,
InputTextMessageContent, UpdateEvent, event,
};
#[event(UpdateEvent::InlineQuery)]
async fn inline_query(
context: Context,
query: InlineQuery,
) -> anyhow::Result<()> {
let result = InlineQueryResultArticle::new(
"result-1",
"Reply",
InputTextMessageContent::new(query.query),
);
context
.request(AnswerInlineQuery::new(query.id, vec![result.into()]))
.await?;
Ok(())
}
fn inline_bot(token: String) -> jingzhe::Result<Bot> {
Ok(Bot::new(token)?.event(inline_query))
}

UpdateEvent covers all currently modeled Update fields and is #[non_exhaustive], so future Telegram events can be added without breaking downstream exhaustive matches. The lower-level .on_event(...) remains available for unknown extension fields.

let bot = Bot::new(token)?
.state(AppState { greeting: "Welcome to Jingzhe".into() })
.command(start)
.command(open_settings)
.on_message(handle_message)
.callback::<Action>(handle_action);
bot.validate()?;
bot.publish_commands().await?;
bot.run_polling().await?;

Call .state(...) before registering handlers. Command menus are published only by publish_commands().await; registration alone does not access the network.

State is shared through Arc<State>, and state() returns &State. State does not need to implement Clone, and the whole value is not automatically wrapped in a Mutex. Each update gets a new Context. Multi-message workflows and persistence are owned by the application.

Context exposes:

  • state(): access shared state.
  • event(): access the complete Update.
  • message(), chat(), sender(): safely access optional fields.
  • request(): send typed requests.
  • reply(): reply while referencing the current accessible message.

reply() references the current accessible message by default when its ID is non-zero. Use reply_with(text, None) to disable the reference, or pass ReplyParameters to control it. For keyboards, formatting, and other options, use context.request(SendMessage::new(...)).

  • Commands match official bot_command entities at offset zero in new message updates. Lengths use UTF-16.
  • /start and /start@this_bot_username match, while commands addressed to another bot do not.
  • A matched command does not fall through to .on_message(...). Unmatched commands and ordinary messages can use the message fallback.
  • Edited messages, channel messages, and unknown events are ignored by default. Opt in with .on_update(...).
  • Explicit event handlers take priority over command, message, and callback routes. A field can be registered only once.
  • Duplicate command names fail in validate() or before startup.

Handlers can return their own error type, such as anyhow::Result<()>. .on_error(...) can observe asynchronous failures without converting them into success.

See the complete examples/polling.rs.