javascript - 使用 NodeJS 和 Action-On-Google 列出项目

标签 javascript java node.js node-modules actions-on-google

我有一个问题, 我正在为谷歌助手创建一个应用程序,我必须将项目放入列表中,问题是我不知道列表包含多少个元素。 官方文档有一个关于如何静态插入元素的示例,我如何捐赠它们? 这是链接文档 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',
      }),
    },
  },
}));

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

我是一名Java开发人员,最近正在学习NodeJS,所以我仍然需要了解一些东西,提前感谢您的帮助

最佳答案

您可以使用类似的原理来生成列表并发送出去,如下所示。您可能需要提供特定的功能来将数据转换为适合您的操作的格式。

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

关于javascript - 使用 NodeJS 和 Action-On-Google 列出项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855931/

相关文章:

javascript - 播放来自 Service Worker 的声音

javascript - 如何让对象在 v-bind 和 v-for 中使用

java - 如何将子节点添加到最后一个父节点

node.js - 如何在 Mongoose 中的虚拟对象中填充对象

node.js - Meteor FS.Collection 访问服务器上的集合

javascript - JS Regex - 匹配每个未转义的特定字符

JavaScript 数组访问元素

java - IDocumentFilter 使用 HashMap 作为字典 Menu 来验证 JTextField

java - 如何通过java读取pg_xlog目录下的WAL文件

javascript - 如何使用 javascript 轻松修改 yaml 文件?