Placing JS Functions in your Main JS File
For your chatbot we recommend that you place the bulk of your Javascript work in a proper Javascript file and keep your Rivescript "objects" very simple.
An Example...
Your Rivescript file could simply call a Javascript function like below.
> object getRandomTerm javascript
return getRandomTerm();
< object
+ random
- Finding a random term... <call>getRandomTerm</call>
Notice how this will run the getRandomTerm()
function, like the one below, that can be in your code.js file.
function getRandomTerm() {
var randomIndex = Math.floor(Math.random() * chatbot.db.length);
currentItem = chatbot.db[randomIndex]
return currentItem.term;
}
The chain of returns: The Javascript function will return the "term" (in this case) to the Rivescript object, which will then return that value to your script file