[Actions-On-Google] Experiences with conversation data

in #programming6 years ago

Experiences with Google Assistant conversation data

I've been working on Google Assistant projects recently and have had quite the headache handling data throughout and between user conversations - I'll provide some insight into each method of handling conversation data.

All examples are assuming a firebase + actions-on-google external webhook dev environment.

Conversation context data

Within each Dialogflow intent you can specify inbound & outbound contexts which can store JSON data between

Intents which have an inbound context will only trigger if said context exists - another intent must have set this context as an active outbound intent.

Dialogflow contexts are temporary data stores, they only last as long as the active conversation (or 10 minutes given a lifetime of 1).

If you increase the intent lifetime then you run the risk of context clutter which can confuse dialogflow intent selection - interrupting the flow of user conversation. You could always manually be erasing the non-applicable contexts at the end of each webhook intent however that seems quite

Context storage examples

Storing example JSON in the 'example_context' context object

conv.contexts.set('example_context', 1, {"example_key": "example_value"});

Retrieving the 'example_key' value from the stored JSON in the 'example_context' context

conv.contexts.get('example_context').parameters['example_key'])

Checking for the existence of the context

If using contexts you'll need to check for the existence of the contexts & their contents - you cannot assume that these will be populated as it's temporary storage.

if (typeof(conv.contexts.get('example_context')) !== 'undefined') {}
if (typeof(conv.contexts.get('example_context').parameters['example_key']) !== 'undefined') {}

Erasing the 'example_context' manually - preventing context clutter!

conv.contexts.set('example_context', 0, {});


Conversation data storage

Conversation data storage enables the temporary storage of individual strings (not JSON data unlike contexts) for the entire lifetime of the google conversation id. This is useful for temporary storage which will be lost after the user closes the app.

Examples

It's far simpler to store data in conv data storage than contexts, as you don't need to worry about context handling in dialogflow - that said, contexts can help direct users to appropriate intents via dialogflow.

conv.data.example_parameter_name = "example_parameter_string";

Here are a couple ways to check for the existence of conv data - as it's temporary these are neccessary checks!

((conv.data).hasOwnProperty('example_parameter_name '))
(typeof(conv.data.example_parameter_name ) !== "undefined")


User storage

User storage is similar to conversation data storage (it can only hold one string per item, no JSON storage) except the data stored is permanent (unless the data is deleted by ourselves during cleanup or by the user manually).

This is very handy for storing data between conversations, however it's limited in size and it cannot store user any identifying information (which is fair).

Examples:

conv.user.storage.example_parameter_name = "example_parameter_string ";

Here are a couple ways to check for the existence of conv data - the user may have deleted them!

((conv.user.storage).hasOwnProperty('example_parameter_name '))
(typeof(conv.user.storage.example_parameter_name ) !== "undefined")

Conv.Followup

I discovered followup functionality recently, it operates similarly to a conversation redirect in that it sends the user to the 'destination_intent' without providing any visual indication that the intent triggered. This followup can be used to trigger intents with specific parameter data input, perhaps not good for storing data but it's a good way of utilizing user input data to trigger intents in a very specific manner.

conv.followup('destination_intent', {example_parameter_key: "example_parameter_value"})


Any questions about actions-on-google development? Reply below.

Best regards,
CM

Sort:  

To listen to the audio version of this article click on the play image.

Brought to you by @tts. If you find it useful please consider upvoting this reply.

You have a minor misspelling in the following sentence:

Here are a couple ways to check for the existence of conv data - as it's temporary these are neccessary checks!
It should be necessary instead of neccessary.

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64359.90
ETH 3105.50
USDT 1.00
SBD 3.87