Skip to main content

BotBuilder

Struct BotBuilder 

Source
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

Source

pub fn new() -> Self

Create a new bot builder

Source

pub fn command<H, Args>(self, name: impl Into<String>, handler: H) -> Self
where 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)
Source

pub fn command_with_description<H, Args>( self, name: impl Into<String>, description: impl Into<String>, handler: H, ) -> Self
where H: IntoHandler<Args>,

Register a command handler with a description

The description is used for slash command menus (e.g., Telegram’s /command list).

Source

pub fn button<H, Args>(self, pattern: impl Into<String>, handler: H) -> Self
where H: IntoHandler<Args>,

Register a button handler with pattern matching

Pattern can end with * for prefix matching (e.g., “confirm_*”). Exact ids take priority over prefix patterns.

Source

pub fn message<H, Args>(self, handler: H) -> Self
where H: IntoHandler<Args>,

Register a catch-all message handler

Only one message handler is used; later registrations are ignored.

Source

pub fn fallback<H, Args>(self, handler: H) -> Self
where 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.

Source

pub fn commands(&self) -> impl Iterator<Item = CommandInfo<'_>>

Get all registered commands with their descriptions, in registration order

Source

pub fn has_commands(&self) -> bool

Whether any command handler is registered

Source

pub fn route(&self, event: Event<'_>) -> Option<&AnyHandler>

Find the handler that should serve an event

Commands and exact button ids resolve with a single hash lookup; only wildcard button patterns fall back to a scan. An event no route claims resolves to the fallback handler, if any.

Trait Implementations§

Source§

impl Default for BotBuilder

Source§

fn default() -> BotBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ChatActionSenderBounds for T
where T: Send + Sync + ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.