
Slack: メッセージ送信 (Block Kit 対応)
Slack: Post Message (Block Kit supported)
この工程は、Slack にメッセージを投稿します。Blocks に対応しています。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- conf_OAuth2
- C1-a: HTTP 認証設定 (Questetra が登録済みの Bot を使用する場合)
- conf_Token
- C1-b: HTTP 認証設定 (独自に登録した Bot を使用する場合)
- conf_Channel
- C2: チャンネル ID (名前も可) *
- conf_Thread
- C3: スレッド ID
- conf_ReplyBroadcast
- C4: 返信をチャンネル全体にブロードキャストする
- conf_Blocks
- C5: メッセージ (Block Kit)#{EL}
- conf_Text
- C6: メッセージ (Markdown) *#{EL}
Notes
- この自動工程を使用するには、開始: Slack: メッセージ受信時 の Notes 部に記載の手順にしたがって、Slack アプリを作成・インストールしてください
- 作成した Slack アプリの設定ページで OAuth & Permissions を開くと表示される Bot User OAuth Token が、C1-b に設定する Bot トークンです
- Block Kit のメッセージは、Block Kit Builder で作成できます(要 Slack ログイン)
Capture

See Also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- slack-message-post.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
function main() {
const auth = decideAuth();
const channel = readStringData('conf_Channel');
if (channel === null) {
throw new Error('Channel must not be blank.');
}
const thread = readStringData('conf_Thread');
const replyBroadcast = configs.getObject('conf_ReplyBroadcast');
const text = readText();
const blocks = readBlocks();
sendMessage(auth, channel, thread, replyBroadcast, blocks, text);
}
/**
* config から認証設定値を読み出す
* 両方とも設定されていれば、auth-type="OAUTH2" の方を優先
* 両方とも設定されていなければ、スクリプトエラー
* @return auth
*/
function decideAuth() {
const auth_OAuth2 = configs.getObject("conf_OAuth2");
const auth_Token = configs.getObject("conf_Token")
if (auth_OAuth2 !== null ){
return auth_OAuth2;
} else if (auth_Token !== null){
return auth_Token;
} else {
throw new Error('No Authorization Setting.');
}
}
function readStringData(configName) {
const def = configs.getObject(configName);
if (def !== null) {
return engine.findData(def);
}
// editable=true にも対応
const value = configs.get(configName) ?? '';
return value === '' ? null : value;
}
function readText() {
const text = configs.get("conf_Text") ?? '';
if (text === '') {
throw new Error('Markdown must not be blank.');
}
return text;
}
function readBlocks() {
const blocksText = configs.get('conf_Blocks') ?? '';
if (blocksText === '') {
return null;
}
const errorMsg = `Block Kit must be JSON like below.\n{ "blocks": [...] }`;
let blocks;
try {
blocks = JSON.parse(blocksText).blocks;
} catch (e) {
throw new Error(errorMsg);
}
if (blocks === undefined) {
throw new Error(errorMsg);
}
return blocks;
}
/**
* Send Message with Bots チャット投稿
* @param {Object} auth
* @param {String} channel チャンネルID
* @param {String} thread スレッドID
* @param {Object} blocks
* @param {String} text
*/
function sendMessage(auth, channel, thread, replyBroadcast, blocks, text) {
const jsonReq = {};
jsonReq["channel"] = channel;
if (thread !== null) {
jsonReq["thread_ts"] = thread;
jsonReq["reply_broadcast"] = replyBroadcast;
}
if (blocks !== null) {
jsonReq['blocks'] = blocks;
}
jsonReq["text"] = text;
const url = 'https://slack.com/api/chat.postMessage';
const response = httpClient.begin()
.authSetting(auth)
.body(JSON.stringify(jsonReq), "application/json; charset=UTF-8")
.post(url);
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 new Error(`Failed to send. status: ${status}`);
}
if (responseJson.ok !== true) {
engine.log(`status: ${status}`);
engine.log(responseTxt);
throw new Error(`response is not ok. error: ${responseJson.error}`);
}
}