Slack: Post Message (Block Kit supported)

Slack: メッセージ送信 (Block Kit 対応)

This item posts a message to Slack. Blocks are available.

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_Token
C1: Authorization Setting in which Bot Token is set *
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 this item, 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
  • 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 = configs.getObject("conf_Token");
    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);
}

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}`);
    }
}
%d bloggers like this: