将嵌套数据从 google sheet 发送到 API

Sending nested data from google sheet to API

我正在尝试在 google 脚本中实现以下 REST

我用过的代码如下

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);
}

我得到的结果是

我也试过以下方法

  1. "payload":JSON.stringify(payload) 结果 - {text=嗨,约翰,您的订单已确认并将在 2 月 15 日之前交付给您,type=text,isHSM=false}

  2. "message":{"payload" : { "isHSM":"false", "type": "text", "text": "Hi I am testing whatsapp" }}}; 并将 contentType 设置为 x-www.....urlencoded..

结果 - {payload={type=text,text=嗨,我正在测试 whatsapp,isHSM=false}}

  1. "src.name":"googlerishisheet", "message.payload.isHSM":"false", "message.payload.type": "text", "message.payload.text": "Hi I am testing whatsapp" }; 结果 - 同样的问题

您可能还需要添加内容类型。

var options = {
                "method" : "post",
                "headers": {
                  "Content-Type": "application/json"
                 },
                 "payload" : payload
             };
UrlFetchApp.fetch(url, options);

文档指出 message.payload 值的类型是 object。但是,您似乎应该将其作为纯字符串发送,同时将 payload contentType 保持为 "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"
                 })

如果您将此消息作为模板消息发送,请确保您的模板已获得 WhatsApp 的批准,或者您正在使用的相同模板 ("Hi {1}, your order is confirmed and will be delivered to you by {2}") 已获得 GupShup 的预先批准。您可以在仪表板中查看它。