Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save and retrieve information across invocations of my agent in Dialogflow?

I would like my Actions on Google agent to store and retrieve certain pieces of information across invocations - like a cookie. How do I do this?

like image 613
Lord Loh. Avatar asked Nov 28 '17 16:11

Lord Loh.


People also ask

Does Dialogflow store data?

Stay organized with collections Save and categorize content based on your preferences. Try the snippets: Import, deploy, and explore code snippets in context using a new Dialogflow agent.

What is Dialogflow fulfillment?

Fulfillment is code that's deployed as a webhook that lets your Dialogflow agent call business logic on an intent-by-intent basis. During a conversation, fulfillment allows you to use the information extracted by Dialogflow's natural-language processing to generate dynamic responses or trigger actions on your backend.

What is action and parameters in Dialogflow?

Dialogflow sends an API interaction response for each step of slot filling. For each of these slot filling responses, the intent and action will be the same, and the parameters collected so far will be provided. When building an agent, you provide prompts that the agent will use to get parameter data from the end-user.


2 Answers

You have a lot of options on how you want to do this, depending on exactly what you're trying to do. It isn't exactly like a web cookie, although there are similarities.

If you want the equivalent of a session cookie, information that is retained during a single conversation, then your options are

  • Using the Session ID provided as part of the information sent to you on each invocation and tracking this in your fulfillment.
  • Storing information you want retained using a Dialogflow context
  • If you are using the actions-on-google JavaScript library, storing this in the app.data object created for you.

If you want the equivalent of a long-lasting cookie to retain information between conversations then your options are

  • Using the anonymous User ID provided as part of the information sent to you on each invocation and tracking this in your fulfillment.
  • If you are using the actions-on-google javascript library, storing this in the app.userStorage object created for you.
  • Storing it as part of the string in the JSON response under data.google.userStorage.

Some more information about each of these

Session ID

A different Session ID is created for each conversation you have. You can get this Session ID by examining the JSON sent to your webhook in the sessionId parameter.

You can then look this up in a data store of some sort that you manage.

Dialogflow context

Contexts are powerful tools that are available with Dialogflow. You return a context as part of your fulfillment webhook and indicate the name of the context, its lifetime (how many more rounds of the conversation it will be passed back to your webhook), and any parameters associated with the context (string key/value pairs).

Contexts are especially useful in helping determine what intents may be called. You can indicate what contexts must be active for an Intent to be recognized by Dialogflow.

If you're using the actions-on-google node.js library, you can set a 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 do the equivalent in the 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 your webhook is called, you can fetch this context either by looking at the result.contexts array or by using the app.getContext() or app.getContextArgument() methods in the library.

Using app.data

If you're using the library, Google has done some of the work for you. The app.data object is created for you. Any values you set in the object are available for the lifetime of the session - you just read them in later calls to your webhook.

(Under the covers, Google uses a context for this, so there is no magic. The two work together and you're free to do both.)

Anonymous UserID

When a user first uses your action, a user ID is generated. This ID doesn't give you access to any specific information about them, and isn't used for any other action, but every time you see it, you can be assured that it was the same user that used it on a previous occurrence. Just like a cookie, however, the user can reset it and a new ID will be generated for them for your action.

You get this from the JSON at originalRequest.user.userId or by using app.getUser().userId. Once you have it, you'd use a data store of some sort to store and retrieve information about this user.

Using app.userStorage

Similar to app.data, there is also an app.userStorage object that is created for you for each user. Any changes you make to this object are saved in between conversations you have with this user.

Unlike app.data, however, this doesn't get stored in a context. It has its own storage method. Which leads to...

Storing it in JSON

If you're not using the actions-on-google library, you still have access to userStorage through the response and request JSON directly. You need to store this as a string, but if you need to store a more complex object, a common method is to stringify it as JSON.

You'll store this value under data.google.userStorage in the response and can retrieve it under originalRequest.data.user.userStorage in the request your webhook receives.

like image 181
Prisoner Avatar answered Oct 18 '22 03:10

Prisoner


You can save the information in Context with a key value parameter.

SAVING VALUES IN CONTEXT :

agent.set.Context({
  name:'context-name',
  lifespan: 5,
  parameters:{
    'parameter-name':'parameter-value'
    }
});

GETTING VALUES FROM CONTEXT

 agent.getContext('context-name');

For more Details : https://dialogflow.com/docs/contexts/contexts-fulfillment

like image 27
Ajay Venugopal Avatar answered Oct 18 '22 04:10

Ajay Venugopal