Back to Blog

How to Retrieve data from a database

In this article we will show you how you can enhance your customer experience by integrating your chatbot with a database. To be more specific, we will look at how we can retrieve information from a database and use that in conversations with your bot. We will do that by showing you how to:

In addition to retrieving data from a database, it is also possible to push data to a database (POST). Read more about how to integrate your chatbot with a database.

Extracting data and user input

Before we start extracting data from our database, we need some data that we can use to extract only the relevant information from the database. Extracting data can be done by capturing user input and by using the payload. Read more about it in working with databases.

In this example, we will use the phone number of the user that we receive by the payload as the user interaction via WhatsApp

Retrieve data (GET)

In this example, we’ll be working with a database from Restdb. Setting up and filling the database is part of a previous is step, as described in working with databases. At this stage our, database contains only 1 record.

Chatbot Database

To retrieve data from your the RestDB database, we’ll be working with APIs. If you’re using RestDB you can access developers mode by clicking the gear/settings icon in the top right. Then head over to API docs -> Javascript. From there, you can copy and paste the GET code from RestDB to an action in Flow.ai.

Chatbot GET request

The next step is to alter the code in Flow.ai’s Action, so that we can retrieve only relevant data. In this case that is, data that is related to a user that’s sending a message. We can do that for example by using the name that is related to the user’s profile. To increase security you could also use the phone number, date of birth, or a combination.


async payload => {
  
  var fullName = ""
  
  // Check if name exists
  if("fullName" in payload.user.profile) {
     fullName = payload.user.profile.fullName
   }
    

  const options = {
    method: 'GET',
    url: `https://integratedb-74ce.restdb.io/rest/userdata?q={"name": "${fullName}"}`,
    headers: {
      'cache-control': 'no-cache',
     'x-apikey': 'YOUR SECRET API KEY'
    }
  };
  
  let result
  
  try {
    const { data } = await request(options)
    
    result = data
  } catch (err) {
    console.error(err)
    throw err
  }
  
  const { name, email } = result[0] 
  // notice API may return more then 1 item in the array
  
  return new Message(`Hi ${name}, is ${email} still your email address?`)
}

Personalize the experience

We now have more data that is related to this specific person. For example, we can send him a confirmation email. Don’t forget to ask for confirmation, just to check if the e-mail address is up to date. If not, we can use a PUT request to update the database. But that’s to much detail for now.

The get request will return information in code. That means that we need code to display our message to the user. You might be familiar with our drag & drop interface but under the hood that’s all code as set out in our code replies. Let’s see if we can make something nice out of our GET request.

Personalized experience
Get StartedContact us