0

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

alawibh
  • 217
  • 1
  • 4
  • 16
  • How do you have Twilio hooked up to DialogFlow? – philnash Aug 08 '21 at 23:42
  • https://www.youtube.com/watch?v=bccFFjk76IA&list=PLwsV-N5SOckPDEdxObRPhkGkz5olh--Z5&index=9 – alawibh Aug 09 '21 at 22:18
  • Hey @alawibh, I don't have the time to sift through a 70 minute video to find out what you have done. Can you tell me specifically the part where you take a message from Twilio send it to Dialogflow and then send it back to Twilio to make the reply. I believe that is likely where the bug is here and what we should look at. – philnash Aug 09 '21 at 23:20
  • Can you share the code of the function that Twilio makes a webhook request to? Or tell me where Twilio is making that webhook request? – philnash Aug 10 '21 at 01:10
  • Hi, I thought you would like to know how integration was done, that's why I shared the tutorial with you. Anyway, Twilio trigger the function mentioned above under title "Test code is here:" and function name is "testLoopReply(agent)" which placed in Dialogflow fulfilment. – alawibh Aug 10 '21 at 04:34

1 Answers1

0

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();
       });
   }); 
  }
philnash
  • 70,667
  • 10
  • 60
  • 88
  • Hi dear, it seems my issue is about to be solved. When I changed the code for your suggestion, it worked, but some times, it triggered error saying the length of 1600 chars exceeded. I solve this by limit reply to 11 products at most, any other idea to overcome this issue? – alawibh Aug 10 '21 at 17:39
  • With Twilio you are limited to sending 10 messages at a time and each message can be a maximum of 160 characters, which leads to that error message. You could limit the number of products, or, as you go through the loop, check if adding one more will push your message over 1600 characters and if so, break out of the loop and return at that point. If this does solve your problem, please do mark the answer as correct so that others can see it helped too. Thanks! – philnash Aug 11 '21 at 00:51
  • hello I do have a problem I am using twilio ...it works well in my browser in laptop ...but when I open my app on the smartphone it doesn't work – marc-stupid May 11 '22 at 22:04
  • Hi @marc-stupid, it's likely best if you ask a new question and explain in greater detail what you are trying to do, how it is not working and include any relevant code. I can take a look then. – philnash May 12 '22 at 01:35
  • Hi @philnash , in my django web app I am using twilio ...it works perfectly when I am using any browser from laptop but once I open my web app in my phone browser it doesn't work ...twilio try to connect but then no error or anything appear .. I don't know if there is something I need to add so it can work – marc-stupid May 13 '22 at 15:56
  • Hi @marc-stupid, please [ask a new question](https://stackoverflow.com/questions/ask) and I'll take a look. – philnash May 15 '22 at 02:39