Access a Slack Client

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

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.

Note

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.

1// Extend the dispatcher (e.g. EventDispatcher, SlashCommandDispatcher, etc.)
2// Invoke the ActionHandler and implement the RunnableHandler interface
3public class EventDispatcherEventExample extends Slack.EventDispatcher {
4    public override Slack.ActionHandler invoke(Slack.EventParameters parameters, Slack.RequestContext context) {
5        return Slack.ActionHandler.ack(new Handler(parameters, context));
6    }
7
8    public class Handler implements Slack.RunnableHandler {
9        Slack.EventParameters parameters;
10        Slack.RequestContext context;
11
12        public Handler(Slack.EventParameters parameters, Slack.RequestContext context) {
13            this.parameters = parameters;
14            this.context = context;
15        }
16
17        public void run() {
18            Slack.App app = Slack.App.MySlackApp.get();
19
20            // Create an instance of a Slack client
21            Slack.BotClient client = app.getBotClientForTeam(this.context.getTeamId());
22        }
23    }
24}

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

1public 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

1public 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

1public 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

1public 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

1public String getConnectedSalesforceUserId(
2  String teamId,
3  String slackUserId
4)

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

1public Map<String, String> getConnectedSalesforceUserIdMap(
2  String teamId,
3  List<String> slackUserIds
4)

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

1public String getConnectedSlackUserId(
2  String teamId,
3  String salesforceUserId
4)

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

1public Map<String, String> getConnectedSlackUserIdMap(
2  String teamId,
3  List<String> salesforceUserIds
4)

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.

Beta Feature

This feature is not generally available. It is not part of your purchased Services. This feature is subject to change, may be discontinued with no notice at any time in SFDC’s sole discretion, and SFDC may never make this feature generally available. Make your purchase decisions only on the basis of generally available products and features. This feature is made available on an AS IS basis and use of this feature is at your sole risk.