0

Below is my code where I parsed a json file and used in a loop to "for" loop in order to print it out onto a console

process.env.DEBUG = 'actions-on-google:*';
const apikey = "xxxxxx";
const https = require('https');
const ActionsSdkApp = require("actions-on-google");
const DialogflowApp = require('actions-on-google').DialogflowApp;
const app = new ActionsSdkApp({request, response});
//ActionsSdkApp({request, response} vs  DialogflowApp({ request: req, response: res })?????? which one do I use????
function responseHandler(app) {
    var topic = app.getArgument('topic');
    https.get(`https://example.com/${topic}?apiKey=${apikey}`, (res) => {
        let body = "";
        res.on('data', data => {
            console.log('Reading Data');
            body += data.toString();
        });
        res.on('end', () => {
            try {
                const profile = JSON.parse(body);
                for(let i = 0; i<profile.data.length;i++) {
                    console.log(" description: " + profile.data[i].description + " title: " + profile.data[i].title + ); // in the json file there are vales called "description" and "title" that I want on my list 
            }} catch (e) {
            app.ask("Sorry, I was unable to load information. Please repeat the search term.");
            console.error("error: " + e.message);
        }
    });
})

}

I'm following the example on actions by google documentation and I'm trying to create a list with all the json objects in my json file but I'm having difficulty. Below is my attempt at it:

function list () {
    const app = new ActionsSdkApp({request, response});
    app.askWithList('Here are a few things I found. Pick one that looks interesting',
        app.buildList('Things to learn about')
    // i want add my loop here....but I how would I add it probably?? for(let i = 0; i<profile.data.length;i++)
            .addItems
                .setTitle(" title: " + profile.data[i].title)
                .setDescription(" description: " + profile.data[i].description)
    );
};

Can anyone offer any advice to help me build the list? Also, do I import the actionsdk library or the dialogflow library?

bob dole
  • 21
  • 4

1 Answers1

1

For starters, which library you use depends on which set of tools you're using. If you are using Dialogflow - use that library. If you are using the Actions SDK - use that one instead. (Unless you have a good reason to use the Actions SDK, you probably should be using the Dialogflow library if you're not sure.)

The app.buildList() command returns a List, so you can use addItem() to add items to that list. There is nothing saying you have to chain them, you can call addItem() on that list one at a time in your loop. I haven't tested this, but your code might look something like:

var list = app.buildList('Things to learn about');
for( let i=0; i<profile.data.length;i++ ){
  var title = profile.data[i].title;
  var desc  = profile.data[i].description;
  var key   = "key-"+i;  // This is what you'll get from the user
  var alias = [];        // This really should be what the user can say that is equivalent
  var item = app.buildOptionItem( key, alias )
    .setTitle( title )
    .setDescription( desc );
  list.addItem( item );
}
app.askWithList( 'Pick one', list );
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Hi, I recently went back to this project and I'm stumped. I'm hoping to make a google assistant app that retrieves a list of research articles based on the users query. For example, if a user wants to read articles on "battery technology" the app will display a list of cards and each card cardwould have the title and a URL link to the article. My question is if I really need an "alias" variable, can the "key" variable be the search term the user requests, and lastly if the user clicks on a card, will the URL automatically open to the article. – bob dole Jan 22 '18 at 15:30
  • It isn't clear, but it sounds like you're asking a completely different question. If so, go ahead and ask it as a new StackOverflow question. If you're trying to clarify your original question because this didn't answer it, go ahead an update it. If this answers your question (which was about building the response in a loop), accepting or upvoting the answer are always appreciated. – Prisoner Jan 22 '18 at 15:50
  • Sorry for not giving enough background. I made a new post on https://stackoverflow.com/questions/48402373/building-a-list-in-actions-on-google I gave an upvote but its not going to show up because I don't have enough rep points – bob dole Jan 23 '18 at 12:58