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)