Apex SDK for Slack (Beta)
Documentation Changelog
Project Structure
App Metadata
Assets
Event Handling
Event Subscriptions
Integration Users
Event Examples
Data Providers
Expressions
Input Validation
Permission Scopes
Considerations and Limitations
Slack provides the Events API to simplify how your app responds to activities. Make sure that you enable event subscriptions on your Slack app, and then select the event types that you want to subscribe to.
When creating your Slack app at api.slack.com/apps/, you can add or remove event subscriptions in one of several ways.
Events aren’t the same as user-triggered actions. For user-triggered actions, see Interactive Components.
Tip
To handle an event, register the event handler by defining it in your app metadata and create an Apex class that extends the Slack.EventDispatcher. You must also specify an integration user to run your Slack events in Salesforce.
Register the event handlers in your .slackapp file. For example, define apex__action__AppHomeOpened if you’re handling the app_home_opened event in an Apex class named AppHomeOpened.
1description: App Home Example App
2# Other command and shortcut definitions here
3# Define the action binding for the app_home_opened event
4events:
5 app_home_opened:
6 action:
7 definition: apex__action__AppHomeOpened
8 title: Example event handler for app_home_opened
9 description: Event fires when a user opens the home tab.For synthetic events, register them in the .slackapp file. However, you don’t need to define synthetic events in the app manifest at api.slack.com/apps/.
Message events are represented by the Slack.MessageEvent class. Use Slack.MessageEvent to handle Slack events of message type. The message event type includes:
message.app_homemessage.channelsmessage.groupsmessage.immessage.mpimIn your .slackapp file, use the events.message property to define your handler for any message.* events.
1description: Handle Message.* Events
2name: MessageDemoApp
3events:
4 message:
5 action:
6 definition: apex__action__messageEvent
7 title: Event handler for various message.* events
8 description: Use the events.message property to define your message.* event handlerUse the channel_type property to determine the message.* event you’re handling. For example, the message.im type returns channel_type with the im value.
1"event": {
2 "type": "message",
3 "channel": "C0000000005",
4 "user": "U0000000001",
5 "text": "This is a message from a DM",
6 "ts": "1648762903933",
7 "channel_type": "im"
8 }Additionally, message events can have a subtype. For example, the channel_join subtype is represented by the Slack.MessageChannelJoinEvent class. For more information, see how the events map to Slack Apex classes.
1{
2 "type": "message",
3 "subtype": "channel_join",
4 "text": "Someone has joined the channel",
5 "ts": "1403051575.000407",
6 "user": "U023ABCDE",
7}For a full list of message subtypes, see Message subtypes in the Slack API documentation.
Some events contain subclasses that are included in their payloads. For example, the channel_rename event is represented by the Slack.ChannelRenameEvent class. It returns the channel property represented by the Slack.ChannelRenameEvent.Channel class.
Tip
To implement an event handler, extend the Slack.EventDispatcher class.
1public class EventDispatcherExample extends Slack.EventDispatcher {
2 public override Slack.ActionHandler invoke(Slack.EventParameters parameters, Slack.RequestContext context) {
3 return Slack.ActionHandler.ack(new Handler(parameters, context));
4 }
5
6 public class Handler implements Slack.RunnableHandler {
7 Slack.EventParameters parameters;
8 Slack.RequestContext context;
9
10 public Handler(Slack.EventParameters parameters, Slack.RequestContext context) {
11 this.parameters = parameters;
12 this.context = context;
13 }
14
15 public void run() {
16 // Do something with the runnable handler
17 // such as updating the app home view
18 // or posting a message
19 }
20 }
21}Event handlers receive these properties from Slack.RequestContext.
appId—The app’s unique ID.channelId-The channel ID.enterpriseId—The organization ID for Enterprise Grid. null values are accepted.teamId—The Slack workspace ID.userId-The Slack user ID.For example, the app uses the user ID from the event payload and displays a view. You can publish your view using the Slack.ViewsPublishRequest and Slack.ViewsPublishResponse classes. For an example, see Onboarding with App Home.
When the event is invoked, you receive the event parameters using the Slack.Event class. For example, you can handle the group_rename event using Slack.GroupRenameEvent and respond with a message using Slack.ChatPostMessageResponse in the run() method of your event handler.
1Slack.App app = Slack.App.ApexSlackApp.get();
2Slack.BotClient botClient = app.getBotClientForTeam(context.getTeamId());
3Slack.Event event = parameters.getEvent();
4String channelId = '';
5String channelName = '';
6
7if (event instanceof Slack.GroupRenameEvent) {
8 Slack.GroupRenameEvent groupRenameEvent = (Slack.GroupRenameEvent) event;
9 channelId = groupRenameEvent.getChannel().getId();
10 channelName = groupRenameEvent.getChannel().getName();
11} else if (event instanceof Slack.ChannelRenameEvent) {
12 Slack.ChannelRenameEvent channelRenameEvent = (Slack.ChannelRenameEvent) event;
13 channelId = channelRenameEvent.getChannel().getId();
14 channelName = channelRenameEvent.getChannel().getName();
15}
16
17Slack.ChatPostMessageResponse response = botClient.chatPostMessage(
18 Slack.ChatPostMessageRequest.builder().channel(channelId).text('The channel was renamed to ' + channelName).build()
19);Similarly, you can get the parameters using the corresponding Apex class for the Slack event. For example, use the Slack.AppHomeOpenedEvent class for the app_home_opened event.
1Slack.AppHomeOpenedEvent appHomeOpened = (Slack.AppHomeOpenedEvent) parameters.getEvent();
2String userId = appHomeOpened.getUser();The Apex SDK sample app provides examples on event handling. See Sample Apex SDK for Slack App.
Tip
Apex SDK for Slack is subject to Slack’s rate limits. Slack’s Event API invokes the app_rate_limited event when a single app receives more than 30,000 events in a single hour from a single workspace. It’s automatically sent when your app’s event subscriptions are rate limited or disabled.
Apex SDK for Slack apps automatically subscribes to app_rate_limited. But a default event handler is unavailable. To determine if your Slack client has reached a rate limit error, look for these fields in the response.
ratelimited: Most Apex classes that call Slack Client API methods include this field, which means that your request is rate limited and you must wait a certain period before retrying the request.rate_limited: For Apex classes calling Slack Client API methods that involve posting to a channel, this field is included in the response if the app posted too many messages. Apps aren’t allowed to post more than one message per second per channel, although short bursts that exceed the limit are allowed.Beta Feature