1

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
  • Tried that too, not working getting same following result _{text=Hi John, your order is confirmed and will be delivered to you by 15 Feb, type=text, isHSM=false}_ – Rishi Mittal Apr 15 '20 at 01:17
  • Now it is showing _{payload={type=text,text=Hi I am testing whatsapp, isHSM=false}}_ – Rishi Mittal Apr 15 '20 at 08:52
  • Already done this style also, but got same result – Rishi Mittal Apr 16 '20 at 00:35
  • How about `var payload = {"channel":"whatsapp", "source":"917834811114", "destination":"91999990**34",//I have * done it on purpose "src.name":"googlerishisheet", "message.payload" : JSON.stringify({ "isHSM":"true", "type": "text", "text": "Hi John, your order is confirmed and will be delivered to you by 15 Feb" }) }` and `contentType:"application/x-www-form-urlencoded"` in options? – TheMaster Apr 26 '20 at 08:59
  • writing message.payload as follows done the trick. ` "message.payload":"{\"isHSM\":\"false\",\"text\":\"Testing message\",\"type\":\"text\"}" ` Taken this from [https://curl.trillworks.com/] which converts curl to json ALSO Just checked `contentType:"application/x-www-form-urlencoded" ` is default in google script [https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app] – Rishi Mittal Apr 26 '20 at 10:01
  • 1
    JSON.stringify({ "isHSM":"true", "type": "text", "text": "Hi John, your order is confirmed and will be delivered to you by 15 Feb" }) **also worked** – Rishi Mittal Apr 26 '20 at 10:10

3 Answers3

2

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"
                 })
TheMaster
  • 45,448
  • 6
  • 62
  • 85
0

You may need to add content type as well.

var options = {
                "method" : "post",
                "headers": {
                  "Content-Type": "application/json"
                 },
                 "payload" : payload
             };
UrlFetchApp.fetch(url, options);
Anees Hameed
  • 5,916
  • 1
  • 39
  • 43
0

If you are sending this message as a template message, be sure that your template is approved by WhatsApp or the same template that you're using ("Hi {1}, your order is confirmed and will be delivered to you by {2}") is pre-approved by GupShup. You can check it in your Dashboard.

AliZaidi
  • 27
  • 1
  • 10