Slack: Post Chat (Bots)
Sends a message to Slack with Bots.
Configs: Common
  • Step Name
  • Note
Configs
  • C1-deprecated: Slack Bot Token
  • C1: OAuth2 Setting *
  • C2: Slack Channel Name *
  • C3: Sending Text#{EL}
  • C4: Attachment Summary (In notifications when no sending text)#{EL}
  • C5: Attachment Color
  • C6: Attachment Title#{EL}
  • C7: Attachment Title Link#{EL}
  • C8: Attachment Text#{EL}
Script (click to open)

main();
function main() {
  const token = configs.get("Token");
  const oauth2 = configs.get("conf_OAuth2");
  let text = "";
  if (configs.get("Text") !== "" && configs.get("Text") !== null){
    text = configs.get("Text");
  }
  const channel = configs.get("ChannelName");
  
  let attachment = {};

  attachAdd(attachment, "Fallback", "fallback");
  attachAdd(attachment, "Color", "color");
  attachAdd(attachment, "Title", "title");
    if (attachment["title"] !== undefined){
    attachAdd(attachment, "TitleLink", "title_link");
  }
  attachAdd(attachment, "AttachText", "text");

 if (attachment["title"] === undefined && attachment["text"] === undefined && text === ""){
    throw "Message to send isn't set.";
  }

  sendMessage(token, oauth2, channel, text, attachment);
}

/**
  * Send Message with Bots  チャット投稿
  * @param {String} token
  * @param {String} oauth2 
  * @param {String} channel
  * @param {String} text 
  * @param {String} attachment
  */  
function sendMessage(token, oauth2, channel, text, attachment) {
  let jsonReq = {};
  jsonReq["text"] = text;
  jsonReq["channel"] = channel;
  jsonReq["as_user"] = "true";

  let attachArray = [];
  if (Object.keys(attachment).length !== 0) {
    attachArray.push(attachment);
  }

  jsonReq["attachments"] = attachArray;

 
  let request = httpClient.begin()
  if (oauth2 !== "" && oauth2 !== null || token === "" || token === null){
    request = request.authSetting(oauth2);
  }else {
    request = request.bearer(token);
  } 
    request = request.body(JSON.stringify(jsonReq), "application/json; charset=UTF-8");
    const response = request.post("https://slack.com/api/chat.postMessage");

  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();

  let responseJson;
  try {
    responseJson = JSON.parse(responseTxt);
  } catch(e) {
    engine.log("failed to parse as json");
    engine.log(`status: ${status}`);
    engine.log(responseTxt);
    throw `Failed to send. status: ${status}`;
  }

  if (responseJson.ok !== true ) {
    const error = `Failed to send`;
    engine.log(`status: ${status}`);
    engine.log(responseTxt);
    throw error;
  }
}

function attachAdd(attachment, config, attachName){
  const value = configs.get(config);
  if (value !== "" && value !== null){
    attachment[attachName] = value;
  }
}

Notes

  • If the deprecated C1: Slack Bot token is set, set C1: OAuth2.
  • You cannot specify multiple post channels.

Capture

See also

Appendix

  • slack-chat-post-bots.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on
%d bloggers like this: