Access a Slack Client

Use these Apex classes to get access to a Slack client.

note: The following methods are deprecated on the App.apex class: getConnectedSalesforceUserId, getConnectedSalesforceUserIdMap,getConnectedSlackUserId, getConnectedSlackUserIdMap. The Slack.App class required that you access the listed methods within the context of an Apex Slack app. In the Summer ’23 release, the UserMappingService class exposes these methods without requiring app context. Use the UserMappingService methods instead for an accurate picture of your org mappings.

App Class

The Slack.App class represents a Slack app that enables interacting with a user or bot.

Usage

Use the Slack.RunnableHandler Interface to create an instance of the Slack client.

// Extend the dispatcher (e.g. EventDispatcher, SlashCommandDispatcher, etc.)
// Invoke the ActionHandler and implement the RunnableHandler interface
public class EventDispatcherEventExample extends Slack.EventDispatcher {
    public override Slack.ActionHandler invoke(Slack.EventParameters parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.ack(new Handler(parameters, context));
    }

    public class Handler implements Slack.RunnableHandler {
        Slack.EventParameters parameters;
        Slack.RequestContext context;

        public Handler(Slack.EventParameters parameters, Slack.RequestContext context) {
            this.parameters = parameters;
            this.context = context;
        }

        public void run() {
            Slack.App app = Slack.App.MySlackApp.get();

            // Create an instance of a Slack client
            Slack.BotClient client = app.getBotClientForTeam(this.context.getTeamId());
        }
    }
}

Create an instance of a Slack client depending on the permission scopes you're using.

App Methods

The following are methods for Slack.App.

getAppKey()

Gets the Slack app key, also known as the Slack app ID.

Signature

public String getAppKey()

Return Value

Type: String

getAppClient()

Reserved for future use. Returns a client to invoke Slack API methods as the Slack app in this context.

Signature

public Slack.AppClient getAppClient()

Return Value

Type: Slack.AppClient

getBotClientForTeam(teamId)

Returns a client to invoke Slack API methods as a Slack bot user for the app in the given team.

Signature

public Slack.BotClient getBotClientForTeam(String teamId)

Parameters

teamId

Type: String

The Slack workspace ID used to retrieve the client

Return Value

Type: Slack.BotClient

getUserClientForTeam(teamId, slackUserId)

Returns a client to invoke Slack API methods as a Slack user for the app in the given team.

Signature

public Slack.UserClient getUserClientForTeam(String teamId, String slackUserId)

Parameters

teamId

Type: String

The Slack workspace ID used to retrieve the client.

slackUserId

Type: String

The Slack user ID used to retrieve the client.

Return Value

Type: Slack.UserClient

getConnectedSalesforceUserId(teamId, slackUserId)

Gets the Salesforce user ID for a given Slack user ID within a Slack workspace.

Signature

public String getConnectedSalesforceUserId(
  String teamId,
  String slackUserId
)

Parameters

teamId

Type: String

The team ID used to retrieve the Salesforce user ID.

slackUserId

Type: String

The Slack user ID used to retrieve the Salesforce user ID.

Return Value

Type: String

getConnectedSalesforceUserIdMap(teamId, slackUserIds)

Gets the Salesforce user IDs given a list of Slack user IDs in a Slack workspace for a specific Slack app. If at least one successful mapping is possible, then the returned map contains an entry for each supplied Slack user ID.

Signature

public Map<String, String> getConnectedSalesforceUserIdMap(
  String teamId,
  List<String> slackUserIds
)

Parameters

teamId

Type: String

The Slack team ID associated with the Salesforce user IDs.

slackUserIds

Type: List<String>

Map of Slack user ID and Salesforce user ID.

Return Value

Type: Map<String, String>

getConnectedSlackUserId(teamId, salesforceUserId)

Gets the Slack user ID for a given Salesforce ID within a Slack workspace.

Signature

public String getConnectedSlackUserId(
  String teamId,
  String salesforceUserId
)

Parameters

teamId

Type: String

The Slack team ID associated with the Slack user ID.

salesforceUserId

Type: String

The Salesforce user ID associated with the Slack user ID.

Return Value

Type: String

getConnectedSlackUserIdMap(teamId, salesforceUserIds)

Gets the Slack user mappings given a list of Salesforce User IDs in a Slack workspace for a specific Slack app. If at least one successful mapping is possible, then the returned map contains an entry for each supplied Salesforce user ID.

Signature

public Map<String, String> getConnectedSlackUserIdMap(
  String teamId,
  List<String> salesforceUserIds
)

Parameters

teamId

Type: String

The Slack team ID associated with the Slack user ID.

salesforceUserIds

Type: List<String>

The Salesforce user IDs associated with the Slack user ID.