
Slack: Get User Name
This item gets Slack username and/or email address by Slack ID.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_Token
- C1: Authorization Setting in which Bot Token is set *
- conf_SlackUserId
- C2: Slack User ID *
- conf_UserName
- C3: Data item to save User Name
- conf_Quser
- C4: Data item to save Email Address or Questetra User
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
- The required scopes for this item are as follows. If your Slack Bot does not have the following scopes, add them to the Bot Token Scopes on the “OAuth & Permissions” page
- users:read
- users:read.email
- The user name to be saved is not their display name but their full name
Capture

See Also
Script (click to open)
- An XML file that contains the code below is available to download
- slack-username-get.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 slackUserId = retrieveSlackUserId();
const userNameDef = configs.getObject('conf_UserName');
const quserDef = configs.getObject('conf_Quser');
if (userNameDef === null && quserDef === null) {
throw 'No data item to save the result is set.';
}
const { userName, emailAddress } = getUserInfo(auth, slackUserId);
if (userNameDef !== null) {
engine.setData(userNameDef, userName);
}
if (quserDef !== null) {
setEmailAddress(quserDef, emailAddress);
}
}
/**
* Slack のユーザ ID を config から読み出す
* @return {String} slackUserId ユーザ ID
*/
function retrieveSlackUserId(){
const slackUserId = engine.findData(configs.getObject('conf_SlackUserId'));
if (slackUserId === null) {
throw 'Slack User ID is blank.';
}
return slackUserId;
}
/**
* Slack のユーザ ID からユーザ情報を取得する
* users.info https://api.slack.com/methods/users.info
* @param {AuthSettingWrapper} auth
* @param {String} slackUserId
* @return {Object} userInfo
* @return {String} userInfo.userName ユーザ名
* @return {String} userInfo.emailAddress メールアドレス
*/
function getUserInfo(auth, slackUserId) {
const url = 'https://slack.com/api/users.info';
const response = httpClient.begin()
.authSetting(auth)
.queryParam('user', slackUserId)
.get(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
let responseJson;
try {
responseJson = JSON.parse(responseTxt);
} catch (e) {
engine.log('Failed to parse response as json.');
engine.log(responseTxt);
throw `Failed to get user info. status: ${status}`;
}
if (responseJson.ok !== true) {
engine.log(`status: ${status}`);
engine.log(responseTxt);
throw `Response is not ok. error: ${responseJson.error}`;
}
const userName = responseJson.user.profile.real_name;
const emailAddress = responseJson.user.profile.email;
return { userName, emailAddress };
}
/**
* データ項目にメールアドレスをセットする
* @param {DataDefinitionView} dataDef
* @param {String} emailAddress
*/
function setEmailAddress(dataDef, emailAddress) {
if (emailAddress === undefined) { // スコープが不足していてメールアドレスを取得できなかった場合
throw 'Failed to get Email Address. Scope users:read.email is required.';
}
// 文字型データ項目の場合
if (dataDef.matchDataType('STRING_TEXTFIELD')) {
engine.setData(dataDef, emailAddress);
return;
}
// ユーザ型の場合
const quser = quserDao.findByEmail(emailAddress); // メールアドレスでユーザを検索
if (quser === null) {
throw `Questetra User not found. email: ${emailAddress}`;
}
engine.setData(dataDef, quser);
}