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
impl RiveScript
Sourcepub fn new() -> Self
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.
Sourcepub fn set_unicode_punctuation(&mut self, re: Regex)
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.
Sourcepub fn set_session_manager(
&mut self,
manager: impl SessionManager + Send + Sync + 'static,
)
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.
Sourcepub fn load_directory(&mut self, path: &str) -> Result<bool, Box<dyn Error>>
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!");Sourcepub fn load_file(&mut self, path: &str) -> Result<bool, Box<dyn Error>>
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!");Sourcepub fn stream(&mut self, source: String) -> Result<bool, Box<dyn Error>>
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!");Sourcepub fn sort_triggers(&mut self)
pub fn sort_triggers(&mut self)
Sort the internal data structures for optimal matching.
Sourcepub async fn reply(
&mut self,
username: &str,
message: &str,
) -> Result<String, String>
pub async fn reply( &mut self, username: &str, message: &str, ) -> Result<String, String>
Get a reply from the chatbot.
Sourcepub fn set_subroutine<F>(&mut self, name: &str, f: F)
pub fn set_subroutine<F>(&mut self, name: &str, f: F)
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>
Sourcepub fn set_handler(
&mut self,
language: &str,
loader: impl LanguageLoader + 'static,
)
pub fn set_handler( &mut self, language: &str, loader: impl LanguageLoader + 'static, )
Set a handler for custom object macros written in other programming languages.
Sourcepub fn current_username(&self) -> Result<String, String>
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.
Sourcepub async fn set_uservar(&self, username: &str, name: &str, value: &str)
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.
Sourcepub async fn get_uservar(&self, username: &str, name: &str) -> String
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.
Sourcepub async fn set_uservars(&self, username: &str, vars: HashMap<String, String>)
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.
Sourcepub async fn get_uservars(&self, username: &str) -> HashMap<String, String>
pub async fn get_uservars(&self, username: &str) -> HashMap<String, String>
Get all stored user variables for a given user.
Sourcepub async fn get_all_uservars(&self) -> HashMap<String, HashMap<String, String>>
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.
Sourcepub fn debug_print_brain(&self)
pub fn debug_print_brain(&self)
Debugging: print the loaded bot’s brain (AST) to console.
Sourcepub fn debug_sorted_replies(&self)
pub fn debug_sorted_replies(&self)
Debugging: print the sorted trigger lists.
Auto Trait Implementations§
impl !Freeze for RiveScript
impl !RefUnwindSafe for RiveScript
impl Send for RiveScript
impl Sync for RiveScript
impl Unpin for RiveScript
impl UnsafeUnpin for RiveScript
impl !UnwindSafe for RiveScript
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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