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

时间:2018-05-07 21:25:38

标签: actions-on-google

我可能会遗漏一些非常明显的东西,但我还没弄清楚如何在动作在谷歌Nodejs客户端库的v2中以编程方式构建List。

换句话说,我想做下面的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',
        }),
      },
    },
  }));

1 个答案:

答案 0 :(得分:5)

好的,回答我自己的问题,万一它会帮助别人......

执行此操作的方式(或至少一种方法)是动态创建项数组并在列表构造函数中使用它:

 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
 }))