pub struct BotBuilder { /* private fields */ }Expand description
Builder for constructing bots with handlers
Handlers are indexed as they are registered, so dispatch is a hash lookup rather than a scan. When two handlers claim the same command or button id, the first one registered wins.
Implementations§
Source§impl BotBuilder
impl BotBuilder
Sourcepub fn command<H, Args>(self, name: impl Into<String>, handler: H) -> Selfwhere
H: IntoHandler<Args>,
pub fn command<H, Args>(self, name: impl Into<String>, handler: H) -> Selfwhere
H: IntoHandler<Args>,
Register a command handler
Handlers use the extractor/responder pattern:
// Simple handler
async fn ping() -> &'static str {
"Pong!"
}
// With extractors
async fn greet(user: User) -> String {
format!("Hello, {}!", user.name)
}
bot.command("ping", ping)
.command("greet", greet)Sourcepub fn command_with_description<H, Args>(
self,
name: impl Into<String>,
description: impl Into<String>,
handler: H,
) -> Selfwhere
H: IntoHandler<Args>,
pub fn command_with_description<H, Args>(
self,
name: impl Into<String>,
description: impl Into<String>,
handler: H,
) -> Selfwhere
H: IntoHandler<Args>,
Register a command handler with a description
The description is used for slash command menus (e.g., Telegram’s /command list).
Register a button handler with pattern matching
Pattern can end with * for prefix matching (e.g., “confirm_*”).
Exact ids take priority over prefix patterns.
Sourcepub fn message<H, Args>(self, handler: H) -> Selfwhere
H: IntoHandler<Args>,
pub fn message<H, Args>(self, handler: H) -> Selfwhere
H: IntoHandler<Args>,
Register a catch-all message handler
Only one message handler is used; later registrations are ignored.
Sourcepub fn fallback<H, Args>(self, handler: H) -> Selfwhere
H: IntoHandler<Args>,
pub fn fallback<H, Args>(self, handler: H) -> Selfwhere
H: IntoHandler<Args>,
Register a handler for events nothing else claimed
Consulted after command, button, and message routing: an unregistered
command or an unmatched button reaches the fallback rather than being
dropped. Plain messages still prefer the message
handler when one is registered.
Only one fallback is used; later registrations are ignored.
Sourcepub fn commands(&self) -> impl Iterator<Item = CommandInfo<'_>>
pub fn commands(&self) -> impl Iterator<Item = CommandInfo<'_>>
Get all registered commands with their descriptions, in registration order
Sourcepub fn has_commands(&self) -> bool
pub fn has_commands(&self) -> bool
Whether any command handler is registered