Code: Chatbot
  • Chatbot Code Reference
  • Getting Started
  • Warm-up Project
    • 1 Code Template
    • 2 Read and Discuss
    • 3 Get Familiar with Rivescript
    • 4 Design a Bot with a Purpose
    • 5 Chatbot Evaluation
    • 6 Chatbot Improvement and Reflection
  • Code Mods
    • Add Sound Effects
    • Add Buttons for User Input
    • Using Functions
    • Connecting a Database Using Google Sheets
    • Searching a Database
    • Storing Data for a User
    • Placing JS Functions in your Main JS File
    • Using Topics
    • Create Flipcards
  • Rivescript Reference
  • Chatbot Project Guidebook
Powered by GitBook
On this page
  • The Rivescript Way
  • The Javascript localStorage Way
  1. Code Mods

Storing Data for a User

PreviousSearching a DatabaseNextPlacing JS Functions in your Main JS File

The Rivescript Way

Rivescript offers a way to store data in your chatbot. For example, you could use the following script.

+ my name is *
- <set name=<star>>It's nice to meet you, <get name>.

+ what is my name
- Your name is <get name>, silly!

For a more detailed explanation go .The only limitation is that it only stores the name for the duration of the browser session. In other words if the user leaves your chatbot and comes back another time, his or her name will not be remembered (or stored) by the chatbot. The next way uses Javascript localStorage to store the data for longer periods.

The Javascript localStorage Way

Using this method we can store information that will remain in the browser, even after a browser refresh. (Unless the user clears the browser cache, which is not typical.) First let's create some Javascript functions in our Rivescipt file to store, get, or clear a user's name.

> object storeName javascript
  localStorage.setItem("name", args[0]);
< object

> object getName javascript
  var name = localStorage.getItem("name");
  if (name) return name;
  else return "what is your name?"
< object

> object clearName javascript
    localStorage.removeItem("name");
    return "Name removed.";
< object

Next, create the script needed to use these functions, like below.

+ start // "start" autostarts the bot conversation 
- Hello, <call>getName</call>

+ *
% hello what is your name
- <call>storeName <formal></call>Nice to meet you <call>getName</call>

+ clear
- <call>clearName</call>

You may have noticed that we used <formal> instead of <star> in line 6 to represent the user's input. The only difference between the two is that <formal> maintains any capitalization the user would use. This is useful in this case since we expect that most names begin with a capital letter.

You can modify the examples above to store and retrieve different information.

Learn more about localStorage in Javascript .

here
here