
Slack: Post Message (Block Kit supported)
This item posts a message to Slack. Blocks are available.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_OAuth2
- C1-a: Authorization Setting (Bot registered by Questetra)
- conf_Token
- C1-b: Authorization Setting (Bot registered by you)
- conf_Channel
- C2: Channel ID (name is also available) *
- conf_Thread
- C3: Thread ID
- conf_ReplyBroadcast
- C4: Reply will be broadcast to the whole channel
- conf_Blocks
- C5: Message (Block Kit)#{EL}
- conf_Text
- C6: Message (Markdown) *#{EL}
Notes
- To use “C1-b: Authorization Setting”, create and install a Slack App following the instructions in the Notes section of Start: Slack: Message Received
- Open “OAuth & Permissions” on the App Setting page and the “Bot User OAuth Token” shown there is your Bot Token to set in C1-b
- Block Kit message can be created on Block Kit Builder (Slack login required)
Capture

See Also
Script (click to open)
- An XML file that contains the code below is available to download
- slack-message-post.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 auto step
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}`);
}
}