actions-on-google - 在 actions-on-google Nodejs v2 中构建列表

标签 actions-on-google

我可能遗漏了一些非常明显的东西,但我还没有想出如何在 tne actions-on-google Nodejs 客户端库的 v2 中以编程方式构建列表。

换句话说,我想做类似下面的conv.ask代码的事情,但事先不知道项目,所以需要创建一个列表,将项目添加到列表中,然后动态询问列表方式。我可以在 v1 中使用:

var rList = app.buildList("Please select one option:");
for (var r =0; r < resp_text.length; r++) {
  rList.addItems(app.buildOptionItem(resp_value[r], resp_matches[r]).setTitle(resp_text[r]));
}
app.askWithList(question_str, rList);

...所以我基本上是在寻找与上述等价的 v2。

感谢任何帮助,谢谢!

conv.ask(new List({
    title: 'Things to learn about',
    items: {
      // Add the first item to the list
      'MATH_AND_PRIME': {
        synonyms: [
          'math',
          'math and prime',
          'prime numbers',
          'prime',
        ],
        title: 'Title of the First List Item',
        description: '42 is an abundant number',
        image: new Image({
          url: 'https://example.com/math_and_prime.jpg',
          alt: 'Math & prime numbers',
        }),
      },
      // Add the second item to the list
      'EGYPT': {
        synonyms: [
          'religion',
          'egypt',
          'ancient egyptian',
      ],
        title: 'Ancient Egyptian religion',
        description: '42 gods ruled on the fate of the dead in the afterworld',
        image: new Image({
          url: 'http://example.com/egypt',
          alt: 'Egypt',
        }),
      },
      // Add the last item to the list
      'RECIPES': {
        synonyms: [
          'recipes',
          'recipe',
          '42 recipes',
        ],
        title: '42 recipes in 42 ingredients',
        description: 'A beautifully simple recipe',
        image: new Image({
          url: 'http://example.com/recipe',
          alt: 'Recipe',
        }),
      },
    },
  }));

最佳答案

好吧,回答我自己的问题以防它能帮助别人......

做到这一点的方法(或至少一种方法)是动态创建一个项目数组并在列表构造函数中使用它:

 var ritems = {}
 for (var r=0; r < resp_text.length; r++) {
     ritems[resp_value[r]] = {
         synonyms: [resp_matches[r]],
         title: resp_text[r]
     }
 }
 conv.ask(question_str);
 conv.ask(new List({
     title: "Please select one option:",
     items: ritems
 }))

关于actions-on-google - 在 actions-on-google Nodejs v2 中构建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222662/

相关文章:

node.js - 如何在 Dialogflow v1 中跨多个意图(超过 2 个)传递值

actions-on-google - 如何判断 Webhook 请求是否位于 DialogFlow V2 的沙箱中?

dialogflow-es - Google DialogFlow 上的类型 {} 操作中不存在 Conv.user.storage 属性

dialogflow-es - Actions on Google - 位置权限

javascript - 我想在 Action on Google 中添加列表

node.js - 如何从 REST API 输出信息

actions-on-google - Actions on Google 启动自定义操作(不是主要 actions.intent.MAIN)

alexa - 更改 Alexa 履行中已识别 intent 的选项

node.js - 错误 : expected_inputs[0]. input_prompt.rich_initial_prompt: 'item[1]' 不能为空

java - 如何处理对外部 REST 服务的请求并将响应返回给 Google Assistant?