简体   繁体   English

如何使用Google Actions SDK遍历JS对象数组并在Google Assistant应用中显示列表选择器

[英]How to iterate over array of js objects and display List Selector in google assistant app using google actions sdk

I have searched in all the documentation here https://developers.google.com/actions/assistant/responses 我在这里https://developers.google.com/actions/assistant/responses中搜索了所有文档

In documentation for "List Selector" all the examples are using static and fixed amount of data. 在“列表选择器”的文档中,所有示例均使用静态和固定数量的数据。 For a real scenario I am calling a RestAPI and getting the response in js object which has array of objects which I want to iterate and show that information in List Selector. 对于实际情况,我正在调用RestAPI并在js对象中获取响应,该对象具有要迭代的对象数组,并在列表选择器中显示该信息。

Below is the example of List Selector given in the documentation of actions sdk of google assistant. 以下是Google助手的动作sdk文档中给出的列表选择器示例。

function list (app) {
app.askWithList(app.buildRichResponse()
.addSimpleResponse('Alright')
.addSuggestions(
  ['Basic Card', 'List', 'Carousel', 'Suggestions']),
// Build a list
app.buildList('Things to learn about')
// Add the first item to the list
.addItems(app.buildOptionItem('MATH_AND_PRIME',
  ['math', 'math and prime', 'prime numbers', 'prime'])
  .setTitle('Math & prime numbers')
  .setDescription('42 is an abundant number because the sum of its ' +
    'proper divisors 54 is greater…')
  .setImage('http://example.com/math_and_prime.jpg', 'Math & prime numbers'))
// Add the second item to the list
.addItems(app.buildOptionItem('EGYPT',
  ['religion', 'egpyt', 'ancient egyptian'])
  .setTitle('Ancient Egyptian religion')
  .setDescription('42 gods who ruled on the fate of the dead in the ' +
    'afterworld. Throughout the under…')
  .setImage('http://example.com/egypt', 'Egypt')
)
// Add third item to the list
.addItems(app.buildOptionItem('RECIPES',
  ['recipes', 'recipe', '42 recipes'])
  .setTitle('42 recipes with 42 ingredients')
  .setDescription('Here\'s a beautifully simple recipe that\'s full ' +
    'of flavor! All you need is some ginger and…')
  .setImage('http://example.com/recipe', 'Recipe')
)
);
}

Now when I am adding an iterator over array of js object before .addItems() there is showing error of syntex as we can't add any line above it. 现在,当我在.addItems()之前的js对象数组上添加迭代器时,显示了syntex错误,因为我们无法在其上方添加任何行。

Any idea on how can we iterate over here and do .addItems() when we can't write anything above .addItems() ? 当我们无法在.addItems()之上编写任何内容时,关于如何在此处进行迭代并执行.addItems()的任何想法?

It is easy once you create the list outside app.askWithList() as below. 一旦在app.askWithList()外部创建列表,如下所示,这很容易。

function parseAndShowMessages(app, data) {
let list = app.buildList('test Messages')
for (var i = 0; i < data.response.messages.length; i++) {
  list.addItems(app.buildOptionItem(data.response.messages[i].subject,'')
    .setTitle(data.response.messages[i].from_name)
    .setDescription(data.response.messages[i].subject + '  \n' + data.response.messages[i].body)
    .setImage(IMG_URL_PICTURE,data.response.messages[i].subject)
  )
}

if (data.response.messages.length > 1) {
  app.askWithList(app.buildRichResponse()
    .addSimpleResponse('Sure, Below are your messages.')
    .addSuggestions(['Appointments']),list);
}
}

In above data is the response js object of RestAPI and data.response.messages has an array of js objects. 上面的数据是RestAPI的响应js对象,而data.response.messages具有一个js对象数组。

Note: According to actions sdk there must be minimum two items with different title in List Selector. 注意:根据sdk动作,列表选择器中必须至少有两个标题不同的项目。

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

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