jsonapigoogle-apps-scriptgupshup

Sending nested data from google sheet to API


I am trying to implement following REST in google script REST documentation

Code I have used is as below

function whatsapp() {
  //https://www.gupshup.io/developer/docs/bot-platform/guide/whatsapp-api-documentation#SendText

  var payload = {"channel":"whatsapp", 
                  "source":"917834811114", 
                  "destination":"91999990**34",//I have * done it on purpose  
                  "src.name":"googlerishisheet", 
                 "message.payload" : {
                   "isHSM":"true",
                   "type": "text",
                   "text": "Hi John, your order is confirmed and will be delivered to you by 15 Feb"
                 }
                }
                 
                           
  var url = 'https://api.gupshup.io/sm/api/v1/msg?apikey=0*8e4a487d6d4d3ccd2d52e7f0ffb78f'; // I have done * on purpose
  var options = {"method" : "post",
                 "payload" : payload};
   UrlFetchApp.fetch(url, options);
}

Result I am getting is

enter image description here

I have tried following ways also.

  1. "payload":JSON.stringify(payload) Result - {text=Hi John, your order is confirmed and will be delivered to you by 15 Feb, type=text, isHSM=false}

  2. "message":{"payload" : { "isHSM":"false", "type": "text", "text": "Hi I am testing whatsapp" }}}; and set contentType to x-www.....urlencoded..

Result - {payload={type=text,text=Hi I am testing whatsapp, isHSM=false}}

  1. "src.name":"googlerishisheet", "message.payload.isHSM":"false", "message.payload.type": "text", "message.payload.text": "Hi I am testing whatsapp" }; Result - Same issue

Solution

  • Documentation states that message.payload value is of type object. However, it seems you should send it as plain string, while maintaining the payload contentType as "application/x-www-form-urlencoded"

        "message.payload" : JSON.stringify({
                       "isHSM":"true",
                       "type": "text",
                       "text": "Hi John, your order is confirmed and will be delivered to you by 15 Feb"
                     })