dialogflow / dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
Apache License 2.0
598 stars 281 forks source link

How to do for-loop in DialogFlow Response Table card? #236

Open sidd-harth opened 5 years ago

sidd-harth commented 5 years ago

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?

shnathoskhan commented 5 years ago

I am also faced this query. any one give a tips or suggestions regards this.

sidd-harth commented 5 years ago

Try this,

let appName, status, podIP, version;
let rows = [];
for (var i = 0; i < data.length; i++) {
    appName = data[i].appName;
    version = data[i].version;
    status = data[i].status;
    rows.push([appName, version, status]);
}

conv.ask('Here is your table');

conv.ask(new Table({
    dividers: true,
    columns: ['appName', 'version', 'status'],
    rows: rows,
}));
anandwana001 commented 5 years ago

@sidd-harth It's not working. I also not able to find the Table card code in the src folder.