简体   繁体   English

带有NodeJS和Google行动的列表项

[英]List Item with NodeJS and Action-On-Google

I have a problem, I'm creating an app for google assistant, and I have to put items in a list, the problem is that I do not know how many elements the list contains. 我有一个问题,我正在为Google助手创建一个应用程序,我必须将项目放在列表中,问题是我不知道列表包含多少个元素。 The official documentation have an example on how to statically insert the elements into how I can donate them? 官方文档中有一个示例,说明如何将元素静态插入到捐赠方式中? this is link documentation https://developers.google.com/actions/assistant/responses#sample_code_7 this is the example of the documentation 这是链接文档https://developers.google.com/actions/assistant/responses#sample_code_7这是文档的示例

if (!conv.surface.capabilities.has('actions.capability.SCREEN_OUTPUT')) {
  conv.ask('Sorry, try this on a screen device or select the ' +
    'phone surface in the simulator.');
  return;
}
// Create a list
conv.ask(new List({
  title: 'List Title',
  items: {
    // Add the first item to the list
    'SELECTION_KEY_ONE': {
      synonyms: [
        'synonym 1',
        'synonym 2',
        'synonym 3',
      ],
      title: 'Title of First List Item',
      description: 'This is a description of a list item.',
      image: new Image({
        url: 'IMG_URL_AOG.com',
        alt: 'Image alternate text',
      }),
    },
    // Add the second item to the list
    'SELECTION_KEY_GOOGLE_HOME': {
      synonyms: [
        'Google Home Assistant',
        'Assistant on the Google Home',
    ],
      title: 'Google Home',
      description: 'Google Home is a voice-activated speaker powered by ' +
        'the Google Assistant.',
      image: new Image({
        url: 'IMG_URL_GOOGLE_HOME.com',
        alt: 'Google Home',
      }),
    },
    // Add the third item to the list
    'SELECTION_KEY_GOOGLE_PIXEL': {
      synonyms: [
        'Google Pixel XL',
        'Pixel',
        'Pixel XL',
      ],
      title: 'Google Pixel',
      description: 'Pixel. Phone by Google.',
      image: new Image({
        url: 'IMG_URL_GOOGLE_PIXEL.com',
        alt: 'Google Pixel',
      }),
    },
  },
}));

in java would come like this, but in nodejs how can I do 在java中会是这样,但是在nodejs中我该怎么办

//list with my data
List<Element> elements= new ArrayList<>();


List<ListSelectListItem> items = new ArrayList<>();
ListSelectListItem item;

for (Element _e : elements) {
   item = new ListSelectListItem();
   item.setTitle(_e.getTitele())
        .setDescription(_e.getDesctiprions())
        .setOptionInfo(
            new OptionInfo().setKey(_e.getKey())
        .setImage(
            new Image().setUrl(_e.getUrlImage());
    items.add(item);
}

I'm a Java developer and recently I'm learning NodeJS so I still have to understand some things, thanks in advance for the help 我是Java开发人员,最近正在学习NodeJS,因此我仍然必须了解一些内容,在此先感谢您的帮助

You can use similar principles to generate a list and send it out, as shown below. 您可以使用类似的原理来生成列表并将其发送出去,如下所示。 You may need to make specific affordances to get your data into a format that could be adapted to your Action. 您可能需要提供特定的能力,才能将数据转换为适合您的操作的格式。

const listItems = {};
// Let's say we already have an array
ourArray.forEach(item => {
  listItems[item.key] = {
    synonyms: item.synonyms,
    title: item.title,
    description: item.description,
    image: new Image({
      url: item.imageUrl,
      alt: item.imageAlt,
    }),
  }
});

conv.ask(new List({
  title: 'List Title',
  items: listItems
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM