Search code examples
axiostwiliodialogflow-eswhatsapptwilio-api

Problem with my Agent when Integrated with Twilio for WhatsApp


Hello Everyone,

Since one week I'm trying to integrate Dialogflow with Twilio for WhatsApp messaging.

The integration has been configured successfully, but I'm facing strange error when retrieving our products through HTTP request using Axios.

Test code is here:

function testLoopReply(agent) {
   const category_name = 'Small%20Appliances';  
   return new Promise((resolve, reject) => {
       axios.get(`https://sheetdb.io/api/v1/qvlk728a5p23g/search?Categories=*${category_name}*&Status=1&limit=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();
       });
   }); 
  }

Consider scenario No. 1:

  • Set (Category_Name = “Cooking”), the webhook response have multiple text responses:
{
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "https://alaswadtrading.com/index.php?route=product/product&product_id=2\n\nBEKO STANDARD COOKER HOOD 60CM - INOX"
        ]
      }
    },
    ...
    {
      "text": {
        "text": [
          "https://alaswadtrading.com/index.php?route=product/product&product_id=44\n\nBEKO GAS COOKER 90X60 CLOSE DOOR"
        ]
      }
    }
  ],
  "outputContexts": []
}

In this scenario, the agent reply with those multiple results normally as per attached snapshot:

Test-With-Cooking

But Twilio does not respond and an error 11200 triggered as per attached snapshots:

Test-With-Cooking-Twilio

Test-With-Cooking-Twilio-Error-Message

Now, consider scenario No. 2

  • Set (Category_Name = “Small%20Appliances”), the webhook response have multiple text responses:
{
  "fulfillmentText": "https://alaswadtrading.com/index.php?route=product/product&product_id=1\n\nBEKO TURKISH COFFEE MACHINE SINGLE CUP SKY BLUE",
  "outputContexts": []
}

In this scenario, the agent reply with the results normally as per attached snapshot:

Test-With-Small

Also, Twilio worked fine and respond normally as per attached snapshot:

Test-With-Small-Twilio

I have contacted Dialogflow and Twilio but with no luck to find a solutions, and here their reply:

Twilio-Support-1

Twilio-Support-2

I'm not sure what is the problem and how I can figure it out in order to publish our agent.

Kindly, advice?

Related Post: Agent unable to print all results received from Axion library request in Dialogflow


Solution

  • Twilio developer evangelist here

    Sorry, I don't normally work with Dialogflow, but I do know how to debug Twilio, so I need to ask how you have connected the two.

    The clear difference here is what the agent object does when it is given just one piece of text with agent.add compared to multiple calls to agent.add. When you call it more than one time it seems to generate an array of fulfillmentMessages rather than a string of fulfillmentText which works with Twilio.

    It looks like you are using the existing DialogFlow integration, which, according to this code, will only send back the response text in the fulfillmentText part of your agent response.

    So, you should either work out how to update the DialogFlow integration so that it can handle multiple fulfillmentMessages or in your loop, build up your response so that it can be sent as one fulfillmentText, with something like:

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