
Slack: Post Chat (Bots)
This item sends a message to Slack with Bots.
Configs: Common
- Step Name
- Note
Configs
- C1-deprecated: OAuth2 Setting (To change scopes)
- C1: OAuth2 Setting *
- C2: Slack Channel Name *
- C3: Sending Text#{EL}
- C4: Mention (user or email address)
- C5: Attachment Summary (In notifications when no sending text)#{EL}
- C6: Attachment Color
- C7: Attachment Title#{EL}
- C8: Attachment Title Link#{EL}
- C9: Attachment Text#{EL}
Notes
- If C1-deprecated is set, you must set C1: OAuth2 Setting.
- You cannot post to multiple channels.
- In the specified posting channel, please invite questetra_bot to the channel in advance.
- Post “/invite @questetra_bot” on the channel you want to invite
- Reference: Posting a Message to Slack > Inviting a Bot to a channel
- When C4: Mention is specified, it will automatically be added to the end of the text.
Capture

See also
Script (click to open)
- An XML file that contains the code below is available to download
- slack-chat-post-bots.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
main();
function main() {
const oauth2 = decideOAuth2Setting();
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");
const slackIdNum = decideSlackIdNum(oauth2);
if (slackIdNum !== "") {
text = `${text}\n\n<@${slackIdNum}>`;
engine.log('Slack Id:' + slackIdNum);
}
if (attachment["title"] === undefined && attachment["text"] === undefined && text === "") {
throw "Message to send isn't set.";
}
sendMessage(oauth2, channel, text, attachment);
}
/**
* @return 使用する OAuth2 設定
*/
function decideOAuth2Setting() {
const v1 = configs.get("conf_OAuth2");
if (v1 !== null && v1 !== "") {
return v1;
}
return configs.get("conf_OAuth2_V2");
}
/**
* メンション相手のメールアドレス(Slack アカウント)をconfigから読み出す
* メールアドレスから Slack ID を取得する関数を呼ぶ
* @return {String} slackIdNum Slack ID
*/
function decideSlackIdNum(oauth2) {
let mailAddress = "";
let slackIdNum = '';
mailAddressDef = configs.getObject("conf_Mention");
if (mailAddressDef === null) { //固定値
mailAddress = configs.get("conf_Mention");
} else {
const user = engine.findData(mailAddressDef);
if (mailAddressDef.matchDataType("QUSER")) { //ユーザ型
if (user !== null) {
mailAddress = user.getEmail();
}
} else { //文字型
mailAddress = user;
}
}
if (mailAddress !== "" && mailAddress !== null) {
engine.log("email: " + mailAddress);
slackIdNum = usersLookupByEmail(oauth2, mailAddress);
}
return slackIdNum;
}
/**
* メールアドレスから Slack ID を取得する
* users.lookupByEmail https://api.slack.com/methods/users.lookupByEmail
* @param {String} oauth2
* @param {String} email
* @return {String} responseJson.user.id Slack ID
*/
function usersLookupByEmail(oauth2, email) {
const url = 'https://slack.com/api/users.lookupByEmail';
const response = httpClient.begin()
.authSetting(oauth2)
.queryParam("email", email)
.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 `Failed to users lookup By email. status: ${status}`;
}
if (responseJson.ok !== true) {
const error = `Failed to send`;
engine.log(`status: ${status}`);
engine.log(responseTxt);
throw error;
}
return responseJson.user.id;
}
/**
* Send Message with Bots チャット投稿
* @param {String} oauth2
* @param {String} channel
* @param {String} text
* @param {String} attachment
*/
function sendMessage(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;
const url = 'https://slack.com/api/chat.postMessage';
const response = httpClient.begin()
.authSetting(oauth2)
.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 `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;
}
}
Pingback: Online Estimate Storage Flow – Questetra Support