Monday, 13 May 2019

How to do For-loop in DialogFlow Response Table card?

I am working on DialogFlow and able to use both SimpleResponse and TableCard response.

This is my SimpleResponse working code, well I am returning a very huge(200+ character) text message and a lot of it is static text. So I want to make use of Table card..

app.intent(TEST, (conv) => {

    /* logic for getting 
            the JSON data */

    var appName, status, podIP, version;
    var itemIds = [];
    for (var i = 0; i < data.length; i++) {
        appName = data[i].metadata.labels.app;
        version = data[i].metadata.labels.version;
        status = data[i].status.phase;
        itemIds.push(data[i].metadata.labels.app + ........);
    }
    console.log("Final Response " + itemIds);
    conv.ask(new SimpleResponse({
        speech: 'Here is the requested data',
        text: `${itemIds}`
    }));

});

This is my code for Table card, I will be getting 5 to 7 elements from for-loop and I can't figure out how to use these values in the table rows and how to get dynamic rows based on number of elements from for-loop.

app.intent(TEST, (conv) => {

    /* logic for getting 
            the JSON data */

    var appName, status, podIP, version;
    var itemIds = [];
    for (var i = 0; i < data.length; i++) {
        appName = data[i].metadata.labels.app;
        version = data[i].metadata.labels.version;
        status = data[i].status.phase;
        itemIds.push(data[i].metadata.labels.app + ........);
    }
    console.log("Final Response " + itemIds);
    conv.ask(new SimpleResponse({
        speech: 'Here is the requested data',
        text: `${itemIds}`
    }));

    conv.ask(new Table({
        dividers: true,
        columns: ['appName', 'version', 'status'],
        rows: [
            ['row 1 item 1', 'row 1 item 2', 'row 1 item 3']
        ],
    }))

});

I tried using con.ask(Table) inside for-loop, but that was a bad idea I guess. Moreover we cant have multiple conv.ask tables.

Is this even possible? I see some Dialogflow apps with big tables, are those tables hard-coded or is the data dynamically inserted?



from How to do For-loop in DialogFlow Response Table card?

No comments:

Post a Comment