How to handle api failure in gupshup bot http call

192 Views Asked by At

I am making a http call from the gupshup IDE bot as below.

function MessageHandler(context, event) {
         if(event.message. == "postdata") {
         var url = "https://abcserver.com/sm/postData";
         var header = {"token":"ca916a68d94","Content-Type": "application/x-www-form-urlencoded"};
         var param = "userName=John&phoneNumber=1123111111";
         context.simplehttp.makePost(url,param,header);
     }
   function HttpResponseHandler(context, event) {
           var result= JSON.parse(event.getresp);
          if(result=="success")
       context.sendResponse("We have successfully stored your data");
   }

I need a way to handle the failure i.e if the url (https://abcserver.com/sm/postData) is not reachable then I don't get any callback, HttpResponseHandler is not called in this case and the bot stops abruptly. I need a way to know that the corresponding api request has failed.I tried using try catch but it doesn't work.

Any link to the correct documentation or code example is welcome.

0

There are 0 best solutions below