actions-on-googleactions-builder

What is the usage of loop statement in CollectionBrowse in @assistant/conversation?


I want to show items dynamically by using a for loop in CollectionBrowse. I'm using the code below, but actions on google returns an error when I use it.

const {
  conversation,
  Simple,
  Card,
  Image,
  Button,
  List,
  Link,
  Table,
  CollectionBrowse,
  Suggestion,
  Schema
}  = require('@assistant/conversation');
const functions = require('firebase-functions');
const app = conversation({debug:true});

app.handle('callApi', (conv) => {
  conv.add(new CollectionBrowse({
   items:[
        {
          title: 'Item #1',
          description: 'Description of Item #1',
          footer: 'Footer of Item #1',
          image: {
            url: 'https://developers.google.com/assistant/assistant_96.png',
          },
          openUriAction: {
            url: 'https://www.example.com',
          },
        },
        {
          title: 'Item #2',
          description: 'Description of Item #2',
          footer: 'Footer of Item #2',
          image: {
            url: 'https://developers.google.com/assistant/assistant_96.png',
          },
          openUriAction: {
            url: 'https://www.example.com',
          },
        },
     {
          title: 'Item #3',
          description: 'Description of Item #3',
          footer: 'Footer of Item #3',
          image: {
            url: 'https://developers.google.com/assistant/assistant_96.png',
          },
          openUriAction: {
            url: 'https://www.example.com',
          },
        }
      ]
  }));
});

exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);

I would like to change my code to something along the lines of the example below.

conv.add(new CollectionBrowse({
   items:[
        for(var i = 1; i < 4; i++){   <------------------------- i want it!!!!!!
         {
           title: 'Item #'+i,
           description: 'Description of Item #'+i,
           footer: 'Footer of Item #'+i,
           image: {
             url: 'https://www.example.com',
           },
           openUriAction: {
             url: 'https://www.example.com',
           },
         }
       }
      ]
  }));

What should I do with the for loop statement, if not this way, is there another way?

Thank you.


Solution

  • I found the answer myself!

    You can create the array using a loop, and then create a new CollectionBrowse object using this array.

    const {
      conversation,
      Simple,
      Card,
      Image,
      Button,
      List,
      Link,
      Table,
      CollectionBrowse,
      Suggestion,
      Schema
    }  = require('@assistant/conversation');
    const functions = require('firebase-functions');
    const app = conversation({debug:true});
    
    app.handle('callApi', (conv) => {
      
      var titleArr = ['Item #1','Item #2','Item #3'];
      var descriptionArr = ['Description #1','Description #2','Description #3'];
      var footerArr = ['footer #1','footer #2','footer #3'];
      var imageArr = [{url: 'https://developers.google.com/assistant/assistant_96.png'},{url: 'https://developers.google.com/assistant/assistant_96.png'},{url: 'https://developers.google.com/assistant/assistant_96.png'}];
      var openUriActionArr = [{url: 'https://www.example.com'},{url: 'https://www.example.com'},{url: 'https://www.example.com'}];
      var itemsArr = [];
      
      for(var i = 0; i<3; i++){
        itemsArr.push({
          title : titleArr[i],
          description : descriptionArr[i],
          footer : footerArr[i],
          image : imageArr[i],
          openUriAction : openUriActionArr[i]
        });
      }
        
      conv.add(new CollectionBrowse({
       items: itemsArr
      }));
    });
    
    exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);