javascript [slack] #software

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript [slack] #software相关的知识,希望对你有一定的参考价值。

/***********************************
 * Slack Notifications API
 * check out https://api.slack.com/docs/formatting or
 * https://api.slack.com/docs/attachments
 ***********************************/
function SlackAPI(config) {
  this.webhookUrl = config.webhookUrl;

  // Send a message to slack. The config can 
  // be as simple as a string or an object
  // for passing more complex messages.
  this.sendMessage = function(config) {
    if(typeof config == 'object') {
      postToSlack(this.webhookUrl, config);
    } else {
      postToSlack(this.webhookUrl, { text : config });
    }
  };
  
  // Take care of all the messy stuff like
  // retries and status codes.
  function postToSlack(url, payload) {
    var options = {
      method: 'POST',
      payload: JSON.stringify(payload),
      muteHttpExceptions: true
    };
    var retries = 3;
    while(retries > 0) {
      try {
        var resp = UrlFetchApp.fetch(url,options);
        if(resp.getResponseCode() == 200) {
          return true;
        } else {
          Logger.log(
            Utilities.formatString(
              "WARNING: Slack returned status code of %s and a message of: %s",
              resp.getResponseCode(),
              resp.getContentText()
            )
          );
          Logger.log('Waiting 1 seconds then retrying...');
          Utilities.sleep(1000);
          retries--;
        }
      } catch(e) {
        Logger.log("ERROR: Something failed in UrlFetchApp. Retrying in 1 second...");
        Utilities.sleep(1000);
        retries--;
      }
    }
    throw "Either UrlFetchApp is broken or the Slack Webhook is not configured properly.";
  }
};
// Usage:
var slack = new SlackAPI({
  webhookUrl : "https://hooks.slack.com/services/.../.../..." 
});
slack.sendMessage('This is a test');
slack.sendSimpleMessage({	//or sendMessage(
  channel: "#general", 
  username: "webhookbot", 
  text: "This is posted to #general and comes from a bot named webhookbot.", 
  icon_emoji: ":ghost:",
  //icon url
  "icon_url" : "https://ssl.gstatic.com/awfe30/aw3_cm_20160229_030026_RC4/cm/18EFA99EA70FA5326D7FAB2041DA3661.cache.png",
  //attachment
  "attachments": [
      {
        "fallback": "Your AdWords account has not had any impressions in the past hour!",
        "color": "danger",
        "fields": [
          {
            "title": "Account",
            "value": AdWordsApp.currentAccount().getName(),
            "short": true
          },
          {
            "title": "Priority",
            "value": "Critical",
            "short": true
          }
        ]
      }
    ]
});
 
//---------------------------------------------------------------------------------------------------
//simple example
function sendSlackMessage(text, channel) {
  var slackMessage = {
    text: text,
    icon_url:
        'https://www.gstatic.com/images/icons/material/product/1x/adwords_64dp.png',
    username: 'AdWords Scripts',
    channel: channel
  };

  var options = {
    method: 'POST',
    contentType: 'application/json',
    payload: JSON.stringify(slackMessage)
  };
  UrlFetchApp.fetch(SLACK_URL, options);
}

以上是关于javascript [slack] #software的主要内容,如果未能解决你的问题,请参考以下文章

javascript gas2slack来自webhook

javascript CloudWatch到AWS Lambda到Slack Channel Alerts和Charts。通过SNS主题通过Lambda函数将CloudWatch警报发布到Slack通

javascript 为Slack主题添加ssb-interop.js

javascript 用于Slack消息的AWS CodeDeploy Lamda格式化程序 - node.js

Slack团队切换至TypeScript,简化大型JS代码库管理

javascript 从Slack导入联系人的可编写脚本的脚本。现有联系人还将更新其头像和社交个人资料。联系人