Ask Kirsle: Self-learning RiveScript Bot?

May 9, 2020 (updated May 9, 2020) by Noah

Oliver asks:


I'm looking for a way to solve the following scenario using Rivescript and Python. The chatbot has no answer to a question, in this case it asks the user for the correct answer and records it in the rivescript file.

Do you have an idea? Does it even work in Rivescript?


Best wishes Oliver

Hey Oliver

This can be done with a little bit of creativity. Self-learning bots aren't an officially supported feature in RiveScript, but you can get something to work using object macros that write new replies out to .rive files to grow the bot's brain over time.

There's an example for this in the JavaScript edition of RiveScript, but its logic could be ported over to Python easily enough:

RiveScript Playground example you can test in your web browser:

The basic ideas are:

  1. A RiveScript trigger of + when i say * you say * allows the user to teach the bot a new reply.
    • e.g.: "when I say hello bot you say Hello human!"
  2. To preserve punctuation and formatting, the Python script running the RiveScript module can store the user's raw, original message into a user variable which the object macro can access:
    • rs.set_uservar(username, "origMessage", message)
  3. The object macro behind the when i say * you say * trigger re-parses the user's message to get the original punctuation and formatting they used.
  4. The object macro appends the new trigger/reply into a learned.rive file and also calls stream() to load the new reply for immediate use by the bot "right now."

The JavaScript example linked above works whether or not the program set the origMessage variable: if the program did not set the variable, the learned reply is just somewhat limited to only include simple text with very few punctuation symbols preserved.



There is 1 comment on this page. Add yours.

Avatar image
Oliver posted on May 12, 2020 @ 17:55 UTC

Thank you

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.