
Slack: チャット投稿 (Bots) (Slack: Post Chat (Bots))
Bots 機能を使って Slack にメッセージを投稿します。
Configs:共通設定
- 工程名
- メモ
Configs
- C1-deprecated: Slack Bot のトークン
- C1: OAuth2 設定 *
- C2: 投稿するチャンネルの名前 *
- C3: 送信するテキスト#{EL}
- C4: Attachment の要約 (送信テキストが空の場合に、通知で使用される)#{EL}
- C5: Attachment の色
- C6: Attachment のタイトル#{EL}
- C7: Attachment のタイトルリンク#{EL}
- C8: Attachment のテキスト#{EL}
Notes
- 廃止予定(C1-deprecated: Slack Bot のトークン)が設定されている場合、C1: OAuth2 を設定してください。
- 複数の投稿チャネルを指定することはできません。
Capture

See also
Script (click to open)
- 下記のスクリプトを記述した XML ファイルをダウンロードできます
- slack-chat-post-bots.xml (C) Questetra, Inc. (MIT License)
- Professional をご利用であればファイルの内容を改変することでオリジナルのアドオンとして活用できます
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;
}
}