How to save and receive information through calls of my agent in Dialogflow?

I would like my actions with a Google agent to save and retrieve certain pieces of information through calls - for example, cookies. How to do it?

+9
source share
6 answers

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.

+19
source

You can save information in a context with a key value parameter.

PRESERVATION OF VALUES IN THE CONTEXT:

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

GETTING VALUES FROM THE CONTEXT

  agent.getContext('context-name'); 

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

+1
source

You can also use a Google Cloud database such as BigQuery or Firestore

0
source

It looks like you can check out of your account: https://developers.google.com/actions/identity/account-linking . When you connect an account, you can collect end-user information that you exchange with Google, providing a unique key. This unique key becomes part of every request you receive from Google, so when you receive this unique key, you look at the information that you have collected from the end user. In your case, you will store credentials or any other key needed to access end-user information. After the initial layout, any new data that you received can be saved along with the original information collected based on the unique key obtained when connecting the account.

0
source

And how can I use app.userStorage in my code? I use the built-in Fullfilment editor in DialogFlow. Search by day. he has no documentation. sees impossible to work.

0
source

Please briefly tell me how I can use the app.userStorage object to store the dialog of each user in the user repository, and then how do I get the parameter value (parameter or variable used in the called intent) in this dialog,

0
source

Source: https://habr.com/ru/post/1273723/


All Articles