1
votes

I'm helping create a chatbot that pulls real estate data from a google sheet, and displays that information to a user. The user inputs an address, then the pulled information is assigned to a houseData variable that has been declared outside of the function with the http request. The houseData object has keys like bedrooms, bathrooms, monthlyPayment, address, etc, which is then referenced in the agent.add Payload for displaying the values for the keys to the user in the chat widget. This is all mapped to one intent.

Later on I try to access monthlyPayment of the houseData object in a function mapped to a different intent to see how a user's reported income compares to the payment for the house. The houseData variable is undefined, even though in firebase I can see the information is received from the google sheet in a console.log during the first intent.

Do variable values reset in the fulfillment editor after the intent which specific functions are mapped to is finished? How can I store persistent information in the editor that can be accessed throughout the conversation flow? Is this a situation where I should set a new context?

Thank you.

edit: I added a quick mockup of the code to help visualize the problem:

let houseData;

function sendHouseData(agent) {
  let address = userInput
  return getHouseData(address).then(agent.add(new Payload(
    `Your house has ${houseData.bedrooms} bedrooms and ${houseData.bathrooms} bathrooms.`
  ))).catch(err);
}

function getHouseData(address) {
  https.get(apiURL, (res) => {
    res.on('end', () => {
      houseData = parsedJSONData
    })
  })
}

// ------- LATER ------ //

let userData = {
  income: '',
  qualified: true
}

function qualify(agent) {
  // user self reports their monthly income thru some quick reply options
  if (houseData.monthlyPayment / userData.income < 0.39) { // houseData is undefined at this point
    userData.qualified = false
  }
}

intentMap.set(enterAddress, sendHouseData);
intentMap.set(qualify, qualify)
1

1 Answers

2
votes

Global variables might be maintained across your Dialogflow requests, but it is not guaranteed. From the Cloud Functions Tips & Tricks:

There is no guarantee that the state of a Cloud Function will be preserved for future invocations. However, Cloud Functions often recycles the execution environment of a previous invocation. If you declare a variable in global scope, its value can be reused in subsequent invocations without having to be recomputed.

Reference: https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations

Typically (to cache data for performance reasons) you would store all your data in Firestore/RTDB and if houseData is undefined then pull it out of the database, else you can use houseData directly for a cached copy. In this case though you probably want to keep a record of how old it is and refresh it accordingly.

However, if houseData is going to be different for each user/session then you may be forced to pull it from the database on every request. (Note that if you are running functions and database on Firebase it is going to be fast anyway.)