CxD Archive
Code: Chatbot
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. Create the Rivescript Trigger
  • 2. Create the "searchTerms" Function to Process the Search
Export as PDF
  1. Code Mods

Searching a Database

PreviousConnecting a Database Using Google SheetsNextStoring Data for a User

Last updated 2 years ago

This example uses the same database described in the example for , which has column names id, term, definition, and keywords. Your database will likely have different column names.

1. Create the Rivescript Trigger

Create a trigger in your chatbot script like the one below that will respond to the user typing "search" followed by a word. The response will begin with "Searching..." to provide feedback to the user, then call a searchTerms() function that we will define next.

+ search *
- Searching... <call>searchTerms <star></call>

2. Create the "searchTerms" Function to Process the Search

The function below will process the search and return HTML about the results.

> object searchTerms javascript
  var filtered = chatbot.dbFilter2(chatbot.db, "term", args); //used to be args[0]
  var terms = filtered.map(function(row) {
      return `${row.term}: ${row.definition}<hr>`;
    });
  var reply = "Didn't find any matches.";
  if (terms.length) reply = "Here is what I found.<br>" + terms.join(" ");
  chatbot.postReply(reply, 2000);
  return '';
< object

Let's step through what is happening above...

Line 2 uses the chatbot.dbFilter2() function. Notice there are 3 arguments used by the function. - chatbot.db is the database to be searched. - "term" is the column name to be searched. In this case we will search each term name. - args represents the text the user has typed in for the search. The variable filtered now contains the matches of our search.

Lines 3-5 takes each matched item and creates HTML for the term and definition that will be displayed to the user.

Line 6 defines a variable for our reply string. We start with "Didn't find any matches" as a default.

Line 7 modifies our reply with the HTML of terms and definitions if there were any matches.

Line 8 uses the chatbot.postReply() function to display the reply after 2 seconds.

Line 9 simply returns an empty string so that you don't get an undefined showing up in your chatbot in certain cases. The reply text is taken care of in the lines above.

Connecting a Database Using Google Sheets