Slack: チャット投稿 (Bots) (Slack: Post Chat (Bots))
Bots 機能を使って Slack にメッセージを投稿します。
https://support.questetra.com/ja/addons/slack-chat-post-bots/
2019-02-18 (C) Questetra, Inc. (MIT License)
Configs
  • C1: Slack Bot のトークン *
  • C2: 投稿するチャンネルの名前 *
  • C3: 送信するテキスト #{EL}
  • C4: Attachment の要約 (送信テキストが空の場合に、通知で使用される) #{EL}
  • C5: Attachment の色
  • C6: Attachment のタイトル #{EL}
  • C7: Attachment のタイトルリンク #{EL}
  • C8: Attachment のテキスト #{EL}
Script
main();
function main() {
  const token = configs.get("Token");
  let jsonReq = {};
  if (configs.get("Text") !== "" && configs.get("Text") !== null){
    jsonReq["text"] = configs.get("Text");
  }
  let attachment = {};
  attachAdd(attachment, "Fallback", "fallback");
  attachAdd(attachment, "Color", "color");
  attachAdd(attachment, "Title", "title");
  if (configs.get("Title") !== "" && configs.get("Title") !== null){
    attachAdd(attachment, "TitleLink", "title_link");
  }
  attachAdd(attachment, "AttachText", "text");
  if (attachment !== {}){
    jsonReq["attachments"] = [attachment];
  }
  if (attachment["title"] == null && attachment["text"] == null && jsonReq["text"] == null){
    throw "Message to send isn't set.";
  }
  jsonReq["channel"] = configs.get("ChannelName");
  jsonReq["as_user"] = "true";
  let response = httpClient.begin()
    .bearer(token)
    .body(JSON.stringify(jsonReq), "application/json; charset=UTF-8")
    .post("https://slack.com/api/chat.postMessage");

  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  const responseJson = JSON.parse(responseTxt);
  if (responseJson.ok == false ) {
    const error = "Failed to send \n" + responseTxt;
    throw error;
  }
  engine.log("status:" + status);
  engine.log(responseTxt);
}
function attachAdd(attachment, config, attachName){
  const value = configs.get(config);
  if (value !== "" && value !== null){
    attachment[attachName] = value;
  }
}

Download

Capture

See also