如何在Dialogflow中跨代理调用保存和检索信息? [英] How do I save and retrieve information across invocations of my agent in Dialogflow?

查看:101
本文介绍了如何在Dialogflow中跨代理调用保存和检索信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我在Google代理上的操作可以在调用之间存储和检索某些信息,例如Cookie。我该怎么做?

解决方案

您要如何执行此操作有很多选择,具体取决于您要执行的操作正在尝试做。尽管存在相似之处,但它并不完全像一个Web cookie。



如果您希望获得与会话cookie等效的信息,则在单个记录中保留的信息对话,那么您的选择是




  • 使用会话ID作为每次调用和跟踪时发送给您的信息的一部分

  • 使用Dialogflow上下文存储要保留的信息

  • 如果您使用的是Google Actions-on-Google JavaScript库,则存储在为您创建的 app.data 对象中。



如果需要相当于一个持久性Cookie,可以在对话之间保留信息,那么您的选择就是




  • 使用作为信息一部分提供的匿名用户ID

  • 如果您使用的是Google Actions-on javascript库,请将其存储在t中。他为您创建了 app.userStorage 对象。

  • 将其作为字符串的一部分存储在 data.google.userStorage 。



有关每一个的更多信息



会话ID



为每个会话创建一个不同的会话ID。您可以通过检查在 sessionId 参数中发送到您的Webhook的JSON来获取此会话ID。



然后在您管理的某种数据存储中查找此内容。



Dialogflow上下文



上下文是Dialogflow可用的强大工具。您将一个上下文作为实现Webhook的一部分返回,并指明该上下文的名称,其生存期(将传递回Webhook的会话数)以及与该上下文关联的任何参数(字符串键/值)



上下文对于帮助确定可能调用的意图特别有用。您可以指示必须由哪些上下文激活才能被Dialogflow识别。



如果您使用的是Google的action-on-node.js库,则可以设置上下文,例如:

  var contextParameters = {
foo:有些东西,
酒吧:您当地的酒吧。
};
app.setContext( remember_this,5,contextParameters);

在调用 app.ask()之前,需要执行此操作 app.tell()



或者您也可以在JSON中执行以下操作: contextOut 响应的一部分

  contextOut:[ 
{
name: remember_this,
lifespan:5,
parameters:{
foo:有些东西,
bar:您当地的酒吧。
}
}
]

下一次您的网络挂钩是调用后,您可以通过查看 result.contexts 数组或使用 app.getContext()或 app.getContextArgument()方法。



使用 app.data



如果您使用的是库,则Google会为您完成一些工作。为您创建了 app.data 对象。您在对象中设置的任何值都可以在会话的整个生命周期中使用-您只需在以后的Webhook调用中读取它们即可。



(在幕后,Google使用了

匿名用户ID p>

当用户首次使用您的操作时,将生成一个用户ID。这个ID不会让您访问有关它们的任何特定信息,也不会用于其他任何操作,但是每次看到该ID时,您都可以确信,它是上次使用它的用户。但是,就像cookie一样,用户可以将其重置,并为您的操作为其生成一个新的ID。



您可以从JSON中获取 originalRequest.user.userId 或使用 app.getUser()。userId 。拥有后,您将使用某种数据存储来存储和检索有关该用户的信息。



使用 app .userStorage



类似于 app.data ,为每个用户为您创建的 app.userStorage 对象。您对该对象所做的任何更改都会保存在与该用户的对话之间。



app.data 不同,但是,这不会存储在上下文中。它具有自己的存储方法。导致...



将其存储在JSON中



如果不使用Google Actions-on-Google库,您仍然可以通过响应访问 userStorage 并直接请求JSON。您需要将其存储为字符串,但是如果需要存储更复杂的对象,则常见的方法是将其字符串化为JSON。



您将存储此字符串响应中 data.google.userStorage 下的值,并可以在 originalRequest.data.user.userStorage 下的值中进行检索请求您的Webhook收到。


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?

解决方案

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.

这篇关于如何在Dialogflow中跨代理调用保存和检索信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆