使用 google 脚本向 slack 中的几个人发送 dm

Send dm to several people in slack using google script

我想使用 google 脚本将 dm 发送给 slack 中的几个人。我有他们所有的 webhooks。但我不知道使用多个 webhook 发送它的代码。

消息错误: 异常:无效参数:[https://hooks.slack.com/services/XXXXX,https://hooks.slack.com/services/YYYYY]

  var ss =SpreadsheetApp.getActiveSpreadsheet();
  var ji = ss.getSheetByName('Mailer');
  var url = "[https://hooks.slack.com/services/XXXXX,https://hooks.slack.com/services/YYYYY]";
  var ji_ticket =ji.getRange(3,3,ji.getLastRow()-2,1).getValues();
  var linkticket = ji.getRange(3,4,ji.getLastRow()-2,1).getValues();
  var total_ticket = ji.getRange("C2").getValue();
  var status = ji.getRange("C1").getValue();
  var payload = 
  {
    "blocks": [
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": ":bell: *Pending Ticket* :bell:"
        }
      },
      {
        "type": "divider"
      },
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text":
              "Hi Leaders,\n\n"+ 
              "There are " +total_ticket+ " tickets pending. Kindly click this tickets: "+linkticket
              //"\n\n\nThankyou"
        }
      }
    ]  
  };

Logger.log(payload)
  var params = {
    "method": "post",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };
  if(status=='Yes'){
  return UrlFetchApp.fetch(url,params);
  }
}```

UrlFetchApp.fetch(url,params) 每次调用只能获取一个 URL,因此您可以尝试为每个 webhook 调用 API。

var url1 = "https://hooks.slack.com/services/XXXXX";
var url2 = "https://hooks.slack.com/services/YYYYY";
...

UrlFetchApp.fetch(url1,params);
UrlFetchApp.fetch(url2,params);

参考:

Class UrlFetchApp | fetch