Slack: Get User ID

Slack: Get User ID

Slack: ユーザ ID 取得

This item gets a user ID on Slack.

Basic Configs
Step Name
Note
Auto Step icon
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_Quser
C2: Questetra User or Email Address *
conf_SlackUserId
C3: Data item to save Slack User ID *

Notes

  • The Bot User OAuth Token that is displayed when you open “OAuth & Permissions” on the Slack app settings page is the Bot token to set in C1-b
  • 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.email

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • slack-userid-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

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;
}

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading