Skip to main content

RiveScript

Struct RiveScript 

Source
pub struct RiveScript {
    pub debug: bool,
    pub utf8: bool,
    pub depth: usize,
    pub case_sensitive: bool,
    pub sessions: Arc<dyn SessionManager + Send + Sync>,
    /* private fields */
}
Expand description

RiveScript represents a single chatbot personality in memory.

Fields§

§debug: bool§utf8: bool§depth: usize§case_sensitive: bool§sessions: Arc<dyn SessionManager + Send + Sync>

Implementations§

Source§

impl RiveScript

Source

pub fn new() -> Self

Initialize a new RiveScript chatbot personality.

A single instance of RiveScript is able to have its own set of responses (“brain”) independently of other instances of RiveScript. Also, by default, RiveScript keeps track of temporary user variables (such as recent reply history and any variables the bot has learned about them) at in local memory of this instance, with each instance keeping its own separate data store.

Source

pub fn set_unicode_punctuation(&mut self, re: Regex)

Replace the Unicode punctuation regexp when running with UTF-8 mode enabled.

In UTF-8 mode, the user’s message is (for the most part) left untouched, with only backslashes and HTML angle brackets stripped. This can cause matching errors though if common punctuation symbols were left intact, for example, a trigger that looks for + hello bot might not match the string “Hello bot.” because of the period at the end.

The default regexp is [.,!?;:] which matches common English punctuation symbols to be removed. In case you need to customize this, you can provide your own regexp here.

Source

pub fn set_session_manager( &mut self, manager: impl SessionManager + Send + Sync + 'static, )

Replace the default in-memory User Variable Session manager with an alternative.

Source

pub fn load_directory(&mut self, path: &str) -> Result<bool, Box<dyn Error>>

Load a directory of RiveScript documents (.rive or .rs extension) from a folder on disk. Example

    let mut bot = RiveScript::new();
    bot.load_directory("../eg/brain").expect("Couldn't load directory!");
Source

pub fn load_file(&mut self, path: &str) -> Result<bool, Box<dyn Error>>

Load a RiveScript document by filename on disk. Example

    let mut bot = RiveScript::new();
    bot.load_file("../eg/brain/eliza.rive").expect("Couldn't load file from disk!");
Source

pub fn stream(&mut self, source: String) -> Result<bool, Box<dyn Error>>

Stream a string containing RiveScript syntax into the bot, rather than read from the filesystem. Example

    let mut bot = RiveScript::new();
    let code = String::from(
        "
        + hello bot
        - Hello, human!
        ",
    );
    bot.stream(code).expect("Couldn't parse code!");
Source

pub fn sort_triggers(&mut self)

Sort the internal data structures for optimal matching.

Source

pub async fn reply( &mut self, username: &str, message: &str, ) -> Result<String, String>

Get a reply from the chatbot.

Source

pub fn set_subroutine<F>(&mut self, name: &str, f: F)
where F: for<'a> Fn(&'a mut Proxy<'a>, Vec<String>) -> BoxFuture<'a, Result<SubroutineResult, String>> + Send + Sync + 'static,

Define an object macro handler from a Rust function.

This is a named function that you can call from RiveScript using the <call> tag. The parameters to your function will be the RiveScript interpreter and the array of arguments (shell quote style) passed in to the call.

Example: <call>example "hello world"</call>

Source

pub fn set_handler( &mut self, language: &str, loader: impl LanguageLoader + 'static, )

Set a handler for custom object macros written in other programming languages.

Source

pub fn current_username(&self) -> Result<String, String>

Get the current user’s username.

This is only valid from within a reply context, e.g. from a Rust object macro subroutine.

Source

pub async fn set_uservar(&self, username: &str, name: &str, value: &str)

Set a user variable for a user.

Equivalent to <set name=value> in RiveScript for the username.

Source

pub async fn get_uservar(&self, username: &str, name: &str) -> String

Get a user variable from a user.

Equivalent to <get name> in RiveScript.

Returns the string “undefined” if not set.

Source

pub async fn set_uservars(&self, username: &str, vars: HashMap<String, String>)

Set many user variables for a given user.

With this function, you could restore a full set of user variables (e.g. which were previously retrieved from get_uservars) by providing a full HashMap of key/value pairs.

Source

pub async fn get_uservars(&self, username: &str) -> HashMap<String, String>

Get all stored user variables for a given user.

Source

pub async fn get_all_uservars(&self) -> HashMap<String, HashMap<String, String>>

Get all stored user variables about all users.

This function may be most useful when using the default in-memory user variable storage. It returns a HashMap of usernames paired to the HashMap of all of their data.

If you are using a third-party storage driver (such as to use Redis or SQL), you will probably not want to call this function in case it scrapes your entire table end-to-end and returns ALL data about ALL users.

Source

pub fn debug_print_brain(&self)

Debugging: print the loaded bot’s brain (AST) to console.

Source

pub fn debug_sorted_replies(&self)

Debugging: print the sorted trigger lists.

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.