Storing Data for a User
The Rivescript Way
Rivescript offers a way to store data in your chatbot. For example, you could use the following script.
For a more detailed explanation go here.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
localStorage
WayUsing 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.
Learn more about localStorage in Javascript here.
Next, create the script needed to use these functions, like below.
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.