如何将 userStorage 与新的 Dialogflow v2 API 一起使用?

How do I use userStorage with the new Dialogflow v2 API?

例如,在 API v1 中,我可以这样做:

app.userStorage.favoriteColor = blue;

如果我在稍后的会话中回来,我可以通过这样做来访问这个值:

let myFavoriteColor = app.userStorage.favoriteColor;

我一直在研究 API V2 文档(与 V1 相比几乎不存在),发现 Google 的一个示例中使用了类似的东西看到 here,其中信息存储在 conv.user.storage.zip:

app.intent('weather_report', function(conv) {
  let zip = conv.arguments.get('zipcode');
  conv.data.zip = zip;
  conv.ask(getWeatherReport(zip));
  conv.ask(new Confirmation(`Should I remember ${zip} for next time?`));
});

app.intent('remember_zip', function(conv, params, confirmation) {
  if (confirmation) {
    conv.user.storage.zip = conv.data.zip;
    conv.close('Great! See you next time.');
  } else conv.close('Ok, no problem.');
});

然后在此处访问保存的邮政编码:

app.intent('weather_report', function(conv) {
  let zip = conv.arguments.get('zipcode');
  if (zip) {
    conv.close(getWeatherReport(zip));
  } else if (conv.userStorage.zip) {
    conv.ask(new SimpleResponse(getWeatherReport(conv.user.storage.zip)));
    conv.ask(new Suggestion('Try another zipcode'));
  } else {
    conv.ask('What\'s your zip code?');
  }
});

app.intent('provide_zip', function(conv) {
  conv.user.storage.zip = conv.arguments.get('zipcode');
  conv.close(getWeatherReport(conv.user.storage.zip));
});

在这里,他们同时使用了 conv.user.storage.zipconv.userStorage.zip,这在第一个代码块中是不存在的。这只是一个拼写错误还是这里发生了什么我不理解的事情?

是的,打错了。正确的 v2 属性 是 conv.user.storage 而不是 conv.userStorage.

感谢杰夫指出!该页面应该今天更新。