
Slack: ユーザ ID 取得
この工程は、Slack のユーザ ID を取得します。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- conf_OAuth2
- C1-a: HTTP 認証設定 (Questetra が登録済みの Bot を使用する場合)
- conf_Token
- C1-b: HTTP 認証設定 (独自に登録した Bot を使用する場合)
- conf_Quser
- C2: Questetra ユーザ もしくは メールアドレス *
- conf_SlackUserId
- C3: Slack ユーザ ID を保存するデータ項目 *
Notes
- Slack アプリの設定ページで OAuth & Permissions を開くと表示される Bot User OAuth Token が、C1-b に設定する Bot トークンです
- この自動工程には、以下のスコープが必要です 使用する Slack Bot に以下のスコープがなければ、OAuth & Premissions の Bot Token Scopes に追加してください
- users:read.email
Capture

See Also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- slack-userid-get.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
main();
function main() {
const auth = decideAuth();
const mailAddress = getmailAddress();
const slackUserId = usersLookupByEmail(auth, mailAddress).id;
const resultDataDefNum = configs.getObject("conf_SlackUserId");
engine.setData(resultDataDefNum, slackUserId);
}
/**
* 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 `No Authorization Setting.`;
}
}
/**
* ユーザのメールアドレス(Slack アカウント)をconfigから読み出す
* @return {String} mailAddress mail address
*/
function getmailAddress(){
let mailAddress = "";
const mailAddressDef = configs.getObject("conf_Quser");
if (mailAddressDef === null) { //固定値
mailAddress = configs.get("conf_Quser");
} 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);
} else {
throw `No Questetra User or Email address.`;
}
return mailAddress;
}
/**
* メールアドレスから Slack User オブジェクト を取得する
* users.lookupByEmail https://api.slack.com/methods/users.lookupByEmail
* @@param {AuthSettingWrapper}
* @param {String} email
* @return {String} responseBody User オブジェクト
*/
function usersLookupByEmail(auth, email) {
const url = 'https://slack.com/api/users.lookupByEmail';
const response = httpClient.begin()
.authSetting(auth)
.queryParam("email", email)
.get(url);
const status = response.getStatusCode();
const responseJson = response.getResponseAsString();
let responseBody;
try {
responseBody = JSON.parse(responseJson);
} catch (e) {
engine.log("failed to parse as json");
engine.log(`status: ${status}`);
engine.log(responseJson);
throw `Failed to users lookup By email. status: ${status}`;
}
if (responseBody.ok !== true) {
const error = `Failed to get`;
engine.log(`status: ${status}`);
engine.log(responseJson);
throw error;
}
return responseBody.user;
}