Storing data during a session and between sessions with Google Actions

Jeremy Wilken
2 min readFeb 8, 2018

--

Sometimes you need to keep track of information during your conversation when building your voice bot. There is a simple way to do this by using the data property for session and userStorageproperty for between sessions.

I’ve been building a Google Action for tracking the next time you can see the space station fly over you. I opted to ask for permission to access the user’s current location, so I can get accurate geolocation data to determine the next flyover. In my action, when I ask for permission the user might say yes or no. If they say no, I really wanted to keep track of this so I don’t prompt again but exit gracefully.

Storing session data

Inside of your action handler, you can use the app.data property to store values. Its just an empty property, so you can store anything on it that you’d like. Here I just set a boolean to track if I’ve asked for permissions already, so I don’t ask again.

const functions = require('firebase-functions');
const DialogflowApp = require('actions-on-google').DialogflowApp;
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const requestLocation = (app) => {
app.data.requestedLocation = true;
app.askForPermission('To give you accurate details', app.SupportedPermissions.DEVICE_PRECISE_LOCATION);
}
const getInfo = (app) => {
if (!app.data.requestedLocation) {
requestLocation(app);
}
// remainder of action
}
const app = new DialogflowApp({request, response});
const actions = new Map();
actions.set('request-location', requestLocation);
actions.set('info', info);
app.handleRequest(actions);
});

The values stored onapp.data are persisted as long as the action is running, or until the conversation ends (either by error or by your design).

Storing data between sessions

Similarly, we can use the app.userStorage property to set data for a particular user across multiple sessions. For example, you might want to offer a different greeting for someone who has used your app before.

const functions = require('firebase-functions');
const DialogflowApp = require('actions-on-google').DialogflowApp;
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const welcome = (app) => {
if (!app.userStorage.visited) {
app.ask(`Welcome to my action! You can write a longer intro here.`)
app.userStorage.visited = true;
} else {
app.ask(`Welcome, how may I help?`);
}
}
const app = new DialogflowApp({request, response});
const actions = new Map();
actions.set('welcome', welcome);
app.handleRequest(actions);
});

Here we simply set a boolean on app.userStorage.visited to help us track if they have visited before. You might want to be smarter and track how long ago they visited and use the longer intro if some time has passed.

--

--

Jeremy Wilken

I talk to my devices. Host of Design for Voice podcast. Google Developer Expert for Assistant and Angular. Work on @VMwareClarity project.