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
  1. Code Mods

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.

Rivescript
> 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.

Javascript
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

Placing JavaScript in a proper .js file produces the familiar code highlighting that makes your code much easier to write, edit, and debug.

PreviousStoring Data for a UserNextUsing Topics