0

I'm trying to print all results received from the following request (this code not working):

function searchForProducts(agent) {
   // category_name = 'Cooking' for example
   const category_name = agent.parameters.category_name;  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           let Categories = res.data[0];

           if (Categories) {                
                for(var i=0;i<res.data.length;i++){
                    agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
                }
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   });      
}

The problem I'm facing is the agent can only print on result through the following code (this record working but return only one URL):

function searchForProducts(agent) {
   const category_name = agent.parameters.category_name;  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           let Categories = res.data[0];

           if (Categories) {
                agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   });  
    
}

What I'm doing wrong?

============ After Applying Proposed Solution ================

Hello, since yesterday I'm testing but with no luck to get the exact problem. Here what I did:

  1. I created new Intent which will trigger your code once "Test" received.
  2. I have test the code using different ways, and here the result:

Category_Name = “Cooking”:

- Dialogflow Agent Test: Worked (https://i.stack.imgur.com/o4Uis.jpg).

- Web Agent Test: Not Worked (https://i.stack.imgur.com/yLWNU.jpg).

- Dialogflow Web Messenger: Worked (https://i.stack.imgur.com/tmSRi.jpg).

- Twilio: Not Worked (https://i.stack.imgur.com/fu4hG.jpg) and error message was (https://i.stack.imgur.com/8rVaz.jpg).

But, when I changed the Category_Name = “Small%20Appliances”:

- Dialogflow Agent Test: Worked (https://imgur.com/undefined).

- Web Agent Test: Worked (https://imgur.com/undefined).

- Dialogflow Web Messenger: Worked (https://i.stack.imgur.com/1be6R.jpg).

- Twilio: Worked (https://i.stack.imgur.com/TKpKF.jpg).

Here a link for web test (I will keep Category_name=’Cooking’) so you can see the result:

https://bot.dialogflow.com/004077c2-d426-472c-89f0-4997e2955d59

What do you think?

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
alawibh
  • 217
  • 1
  • 4
  • 16
  • I tested your code and it works fine for me, but it seems that it only loops through `res.data[0]` 29 times. Is this expected? Also what error are you getting at your 1st code? Can you edit your question and include the error as well? – Ricco D Aug 03 '21 at 02:16
  • The output I got is `https://alaswadtrading.com/index.php?route=product/product&product_id=2 BEKO STANDARD COOKER HOOD 60CM - INOX` being shown 29 times. – Ricco D Aug 03 '21 at 02:16
  • Hi, there is no error but no result appear for me, can you show me how the result appearing for you with 29 products? When I put the code, the agent return nothing for me, can you guide me? – alawibh Aug 03 '21 at 12:39
  • This is the result when I use the first code snippet, nothing. check out this snapshot (https://imgur.com/SgPQ4ud), but when I put below code snippet I got result, check out this snapshot (https://imgur.com/KFtgmvr). What I am doing wrong? – alawibh Aug 03 '21 at 12:52

1 Answers1

0

Using your code above I was able to loop through agent.add() and got the results. I used test as user input that matches intent "test intent".

Here is a snippet of the 29 similar outputs of your code provided:

enter image description here


Here is the full code:

index.js

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const axios = require('axios'); 
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });


  function yourFunctionHandler(agent) {
   const category_name = 'Cooking';  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           console.log(res.data);
           let Categories = res.data[0];

           if (Categories) {                
                for(var i=0;i<res.data.length;i++){
                    agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${Categories.ProductID}\n\n${Categories.Name}`);
                }
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   }); 
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('test intent', yourFunctionHandler);
  agent.handleRequest(intentMap);
});

package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "10"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "firebase-functions": "^2.0.2",
    "firebase-admin": "^5.13.1",
    "googleapis": "^27.0.0",
    "actions-on-google": "2.2.0",
    "dialogflow-fulfillment": "0.6.1",
    "axios": "0.21.1"
  }
}

Just a suggestion, since your objective is to show all products under a certain "category" you might want to loop through the whole res.data. In this example I just used category_name = 'Cooking' and printed all products that is under the specified category.

function yourFunctionHandler(agent) {
   const category_name = 'Cooking';  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1`).then(function (res) {
           let Categories = res.data;
           if (Categories) {                
                 for (const product of Categories ){
                    agent.add(`https://alaswadtrading.com/index.php?route=product/product&product_id=${product.ProductID}\n\n${product.Name}`);
                }
           } else {
                agent.add(`No items found in the selected category (${category_name})`);
           }

           resolve();
       });
   }); 
  }

This will result to: enter image description here

Ricco D
  • 6,873
  • 1
  • 8
  • 18
  • Dear, I have edited the the question and reply you with the strange result I got, please take a look. – alawibh Aug 04 '21 at 18:00
  • @alawibh you get errors for "Web Test Agent" because it does not support line breaks as per https://cloud.google.com/dialogflow/es/docs/integrations/web-demo#limits. For twilio I'm not sure since the [dialogflow response](https://github.com/GoogleCloudPlatform/dialogflow-integrations/blob/master/twilio/server.js#L53) is only being sent to the Twilio API. You can check [Twilio API docs](https://www.twilio.com/docs/messaging/twiml/message) for more details. – Ricco D Aug 05 '21 at 02:26
  • Hi again, I saw that web test agent does not support breaks, but it showed the result once you put Category_Name = “Small%20Appliances”, look on the attached snapshot, it ignore "\n\n" and respond (https://imgur.com/xiMXEFz) – alawibh Aug 05 '21 at 19:25
  • I see, since your latest question is specific for dialogflow web test agent. I suggest to create a new question so the community can contribute! :) – Ricco D Aug 06 '21 at 00:14
  • Hi dear, yes, as you said, your solution worked for Dialogflow. I will see what to do for the rest. – alawibh Aug 06 '21 at 17:36
  • Dear friend, could you please clearify for me what is the difference between using promise along with axios.get compared with using axios.get alone and how this can affect the results? – alawibh Aug 10 '21 at 04:24
  • I'm not very familiar with axios, you can post a new question so community members that are knowledgeable in axios can help you out. – Ricco D Aug 12 '21 at 00:53