如何从其他意图获取数据? (将意图的数据传递给其他意图)

How to get data from other intent? (pass data for intents to other intents)

我正在对 google 项目执行一项操作,该项目将使用记住用户所说内容并将其用于其他目的的基本功能。

例如,对话可能是这样的:

1     User: Hello
2     Bot: Hey! What's your name?
3     User: Joel
4     Bot: Your name is Joel, is that correct?
5     User: Yes that is correct
6     Bot: Awesome, it's great to meet you Joel.

我知道最初如何获取信息,但我无法将信息(名称 'Joel')传递给另一个纯粹用于确认的意图。

这是我的:

app.intent('greeting', (conv, params) => {
  const context = conv.contexts.set('context');      // working good
  conv.ask(`I got ${params.name}, is that right?`);
});

app.intent('greeting_1', (conv,params) => {
  const context1 = conv.contexts.get('context');  //not working
  conv.ask(`Awesome, it's great to meet yoy ${params.name}`);
 });

如果这有帮助,特别是我得到的错误读取

TypeError: Cannot read property 'userDecision' of undefined

我如何让它工作?我有一种感觉,我必须将数据保存在 userStorage 中,但我真的只需要在非常基本的情况下这样做,所以我对此并不熟悉。

感谢您的帮助!

首先大家好!

根据您的用例,您最初将数据存储在 UserStorage 中的想法可能是正确的。让我详细说明:

在 Google Node.JS 客户端库

上使用操作保存数据的两种方法

在对话中存储数据

您可以使用 conv.data.name = 'Joel' 来保存对话轮次之间的数据。但是下次用户回来时将无法使用此数据。

永久存储数据(只要用户允许)

这就是 userStorage 发挥作用的地方。您可以使用 conv.user.storage.name = 'Joel' 来记住用户每次回来时的姓名。请记住,这种类型的永久存储可能需要用户的同意,具体取决于他们居住的地方。

Legal note: Obtaining consent prior to accessing userStorage. Some countries have regulations that require developers to obtain consent from the user before they can access, or save certain information (e.g. personal information) in the userStorage. If you operate in one of these countries and you want to access, or save such information in userStorage, you must use the Confirmation helper to ask consent to the user and obtain the consent before you can start storing such information in userStorage.

您还需要在隐私政策中解释您要保存的数据。

对于您的特定用例,在 userStorage 中存储不会更改的数据(即名称)是一种更好的方法。因为如果你只保存一次对话,下次你需要再次请求许可。

这些应该可以满足您在平台上保存数据的基本需求。但是,如果您确实需要更复杂的状态管理解决方案,您可能需要检查 Dialogflow Contexts.


编辑: 我忽略了您已经在使用上下文。但答案仍然存在,如果您只想在对话期间保存数据,则不需要使用上下文。

但我认为您遇到问题的原因是您没有正确设置或获取上下文参数。看看例子 here and here:

context.set()方法获取三个参数;名称、寿命和参数。您只是在代码中传递上下文的名称。

并且,仅使用 contexts.get() 方法不足以读取上下文的参数。你需要像这样阅读它们:

app.intent('Tell Greeting', conv => {
  const context1 = conv.contexts.get('context1')
  const { color, num } = context1.parameters
})

但是,我重复我原来的观点。如果你真的不需要,你不应该让事情变得如此复杂。如果可能,请尝试使用 conv.dataconv.user.storage