Skip to main content

ChatBot

Struct ChatBot 

Source
pub struct ChatBot { /* private fields */ }
Expand description

Central hub that connects chat adapters to handler logic.

§Construction

let bot = ChatBot::new(ChatBotConfig::default());

Handlers are registered via the on_* builder methods. Each method accepts an async closure (returning a Future) and stores it for later dispatch.

Implementations§

Source§

impl ChatBot

Source

pub fn new(config: ChatBotConfig) -> Self

Create a new ChatBot with the given configuration.

Source

pub fn add_adapter(&mut self, adapter: impl ChatAdapter + 'static)

Add a chat platform adapter.

Source

pub fn on_mention<F, Fut>(&mut self, handler: F)
where F: Fn(ThreadHandle, IncomingMessage) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler invoked when the bot is mentioned.

Source

pub fn on_message<F, Fut>(&mut self, pattern: Option<String>, handler: F)
where F: Fn(ThreadHandle, IncomingMessage) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for incoming messages.

If pattern is Some, only messages whose text contains the substring will trigger the handler.

Source

pub fn on_subscribed_message<F, Fut>(&mut self, handler: F)
where F: Fn(ThreadHandle, IncomingMessage) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for messages in threads the bot has subscribed to.

Source

pub fn on_action<F, Fut>(&mut self, action_ids: Option<Vec<String>>, handler: F)
where F: Fn(ActionEvent, ThreadHandle) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for interactive action events.

If action_ids is Some, only actions whose action_id is in the list will trigger the handler.

Source

pub fn on_reaction<F, Fut>( &mut self, emojis: Option<Vec<EmojiValue>>, handler: F, )
where F: Fn(ReactionEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for reaction events.

If emojis is Some, only reactions matching one of the listed emoji values will trigger the handler.

Source

pub fn on_slash_command<F, Fut>( &mut self, commands: Option<Vec<String>>, handler: F, )
where F: Fn(SlashCommandEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for slash-command events.

If commands is Some, only commands whose name matches one of the listed strings will trigger the handler.

Source

pub fn on_modal_submit<F, Fut>( &mut self, callback_ids: Option<Vec<String>>, handler: F, )
where F: Fn(ModalSubmitEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for modal-submit events.

If callback_ids is Some, only submissions whose callback_id matches one of the listed strings will trigger the handler.

Source

pub fn on_modal_close<F, Fut>(&mut self, handler: F)
where F: Fn(ModalCloseEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler for modal-close (dismiss) events.

Source

pub async fn run(&mut self) -> Result<(), ChatError>

Start the event dispatch loop.

This method takes ownership of the stored adapters via &mut self, polls each adapter’s recv_event in a round-robin fashion using futures_util::stream::select_all, and dispatches every incoming ChatEvent to the registered handlers.

The loop terminates when all adapters return None (i.e. their event sources are exhausted).

§Errors

Returns Err(ChatError::Closed) if no adapters have been registered.

Trait Implementations§

Source§

impl Debug for ChatBot

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more