You have many options for how you want to do this, depending on what you are trying to do. This is not exactly like a web cookie, although there are similarities.
If you need the equivalent of a session cookie, information that is saved during one conversation, then your options
- Using the session identifier provided as part of the information sent to you on each call, and tracking this during execution.
- Saving the information you want to save using the Dialogflow context
- If you use the JavaScript library for actions on Google, storing it in the
app.data object created for you.
If you want the equivalent of a long cookie to keep information between chains, your options
- Using an anonymous user ID provided as part of the information sent to you on each call, and tracking this when executed.
- If you use the action-on-google javascript library, save it in the
app.userStorage object created for you. - Saving it as part of a line in a JSON response in
data.google.userStorage .
Additional information about each of these
Session ID
A different session identifier is created for each of your sessions. You can get this session ID by examining the JSON sent to your website in the sessionId parameter.
Then you can see this in the data warehouse that you are managing.
dialog context
Contexts are powerful tools available with Dialogflow. You return the context as part of your execution web site and specify the context name, its lifespan (how many more rounds of the conversation will be sent back to your web host) and any parameters related to the context (string key / value pairs).
Contexts are especially useful for determining what intentions can be called. You can specify which contexts should be active for the Intent to be recognized by Dialogflow.
If you are using the action-on-google node.js library, you can set the context using something like this:
var contextParameters = { foo: "Something foothy", bar: "Your local bar." }; app.setContext( "remember_this", 5, contextParameters );
You need to do this before you call app.ask() or app.tell() .
Or you can make an equivalent in JSON as part of the contextOut block of the response
"contextOut": [ { "name": "remember_this", "lifespan": 5, "parameters": { "foo": "Something foothy", "bar": "Your local bar." } } ]
The next time you call webhook, you can get this context by looking at the result.contexts array or using the app.getContext() or app.getContextArgument() methods in the library.
Using app.data
If you use the library, Google has done a certain part of the work for you. The app.data object app.data created for you. Any values ββspecified in the object are available for the life of the session - you simply read them in subsequent calls to your web host.
(Google uses context for this under covers, so thereβs no magic. Both work together and you can do both.)
Anonymous user
When the user first uses your action, a user ID is generated. This identifier does not give you access to any specific information about them and is not used for any other actions, but each time you see it, you can be sure that it is the same user who used it in the previous case . However, as in a cookie, the user can reset, and a new identifier will be created for them for your action.
You will get this from JSON in originalRequest.user.userId or using app.getUser().userId . After that, you will use a certain type of data warehouse to store and retrieve information about this user.
Using app.userStorage
Like app.data , there is also an app.userStorage object created for you for each user. Any changes you make to this object are saved between conversations that you have with this user.
Unlike app.data , however, this is not stored in context. It has its own storage method. That leads to...
Saving in JSON
If you are not using the google action library, you still have access to userStorage through the response and JSON request directly. You need to save this as a string, but if you need to save a more complex object, the general method is to compress it as JSON.
You save this value in data.google.userStorage in the response and can get it in originalRequest.data.user.userStorage in the request that your web check receives.