Use these Apex classes to create handlers for Slack actions, commands, shortcuts, and events.
ActionDispatcher Class
The Slack.ActionDispatcher class enables you to invoke an action handler such as in response to a form submission.
Usage
Extend the ActionDispatcher class and override the invoke() method.
1public class MyActionDispatcher extends Slack.ActionDispatcher{23 public override Slack.ActionHandler invoke(Map<String, Object>parameters, Slack.RequestContext context){4 return Slack.ActionHandler.modal(new Handler(context));5}6}
Examples
This example uses ActionDispatcher to handle a form submission.
1public class ExampleFormSubmissionDispatcher extends Slack.ActionDispatcher{23 public override Slack.ActionHandler invoke(Map<String, Object>parameters, Slack.RequestContext context){4 return Slack.ActionHandler.modal(new Handler(context));5}67 public class Handler implements Slack.ModalHandler{8 Slack.RequestContext context;9 public Handler(Slack.RequestContext context){10 this.context = context;11}12 public Slack.ModalView call(){13 Map<String, Object>formData = context.getFormData();14 Slack.ViewReference viewReference = Slack.View.message_modal.get();15 viewReference.setParameter('title', 'Form Field Value');16 viewReference.setParameter('message', (String)formData.get('FormFieldName'));17 Slack.ModalView modalView = new Slack.ModalView.builder()18 .viewReference(viewReference)19 .build();20 return modalView;21}22}23}
This example uses ActionDispatcher to display a view.
1public class createOpportunity extends Slack.ActionDispatcher{2 public override Slack.ActionHandler invoke(Map<String, Object>parameters, Slack.RequestContext context){3 return Slack.ActionHandler.modal(new Handler(parameters, context));4}5 public class Handler implements Slack.ModalHandler{6 Map<String, Object>parameters;7 Slack.RequestContext context;8 public Handler(Map<String, Object>parameters, Slack.RequestContext context){9 this.parameters = parameters;10 this.context = context;11}12 public Slack.ModalView call(){13 Slack.ViewReference viewReference = Slack.View.create_opportunity.get();14 viewReference.setParameter('title', 'Record Created');15 viewReference.setParameter('message', 'Your opportunity was created');16 Slack.ModalView modalView = new Slack.ModalView.builder()17 .viewReference(viewReference)18 .build();19 return modalView;20}21}
You can close the modal and optionally clear the view stack by using clearModal(). To close the modal without clearing the view stack, use ack(handler) instead.
1public class ExampleViewSubmissionDispatcher extends Slack.ActionDispatcher{23 public override Slack.ActionHandler invoke(Map<String, Object>parameters, Slack.RequestContext context){4 // Tell the framework how to respond first56 // Close the modal and clear the whole stack7 return Slack.ActionHandler.clearModal(new Handler(parameters, context));8}910 public class Handler implements Slack.ModalHandler{1112 Map<String, Object>parameters;13 Slack.RequestContext context;1415 public Handler(Map<String, Object>parameters, Slack.RequestContext context){16 this.parameters = parameters;17 this.context = context;18}1920 public void run(){21 // Process the request (e.g. context.getFormData())22}23}24}
For an example of invoking an action handler on the App Home page, such as with SlashCommandDispatcher, see ChatPostMessageRequest.
The action parameters or other parameters of EventParameters, ShortcutParameters, or SlashCommandParameters type.
context
Type: Slack.RequestContext
Describes the context in which a Slack action is invoked.
Return Value
Type: Slack.ActionHandler
Usage
To access the Slack context, use the invoke() method. This method is passed in by an instance of RequestContext. It exposes information about the source of the action in Slack, performs custom logic, and obtains the correct Slack client to respond to Slack based on the request.
Each action type receives different information in its RequestContext.
ActionHandler Class
The Slack.ActionHandler class provides methods that control how Slack actions are handled. The handler processes a single request.
Usage
With the ActionHandler methods, you can work with modals. By responding to Slack actions with a modal you can display dynamic and interactive information. Before opening a modal, compose a view object to define the layout of the initial view.
A modal can hold up to three views simultaneously in a view stack. Only one view is visible each time. But the view stack can retain previous views and return to them with their prior state in place.
An app can push new views onto a modal’s view stack or update an existing view within that stack, including the currently visible view.
To use this Apex class, specify the Slack namespace when creating an instance of this class. Override the invoke() method returning a Slack.ActionHandler.
Examples
This example pushes a modal onto the modal view stack.
1public class createRecord extends Slack.ActionDispatcher{23 public override Slack.ActionHandler invoke(Map<String, Object>parameters, Slack.RequestContext context){4 // the slack action handler is returned immediately5 return Slack.ActionHandler.pushModal(new Handler(parameters, context));6}78 public class Handler implements Slack.ModalHandler{910 Map<String, Object>parameters;11 Slack.RequestContext context;1213 public Handler(Map<String, Object>parameters, Slack.RequestContext context){14 this.parameters = parameters;15 this.context = context;16}1718 public Slack.ModalView call(){19 Slack.ViewReference viewReference = Slack.View.message_modal.get();20 viewReference.setParameter('title', 'Title');21 viewReference.setParameter('message', 'Message');22 Slack.ModalView modalView = new Slack.ModalView.builder()23 .viewReference(viewReference)24 .build();25 return modalView;26}2728}29}
The message_modal.view file looks like this.
1description: "This is a simple component that displays a message in a modal"2schema:3 properties:4 title:5 type: string6 required: true7 message:8 type: string9 required: true10components:11 - definition: modal12 properties:13 title: "{!view.properties.title}"14 components:15 - definition: section16 properties:17 text: "{!view.properties.message}"
ActionHandler Methods
The following are methods for ActionHandler.
ack(handler)
Acknowledge the payload back to Slack to avoid the 3-second timeout. After running this method, you can perform Slack Client API calls such as to post messages and publish to app home. For example, you can listen to the app_home_opened event and publish a new view to the user’s Home tab. ack() takes care of responding back to Slack immediately to avoid the 3-second timeout.
We don’t recommend using ack() for displaying modals subject to the trigger_id timeouts because doing so can cause your code to fail intermittently, depending on the environment.
To create, update, or push a modal view, use pushModal(), modal(), or updateModal() instead. These methods ack back to Slack within 3 seconds so that the trigger_id from Slack doesn’t expire using a staging modal. Using these methods, your Handler must return a ModalView for Slack to display. See Create an Apex Handler.
Contains the name and values associated with the block element that triggered the action.
Usage
Use the ActionPayload class to register an event, such as an onchange or onclick event, and associate the event to an action. You can retrieve the value from the payload to display it in the view or use the value to perform a query and so on.
Specify your action payload in the run() callback method.
1// Set the context using Slack.RequestContext2Slack.ActionPayload actionPayload = context.getActionPayload();3 if(actionPayload != null){4 String name = actionPayload.getName();5 // do something with the payload6}
See ChatPostMessageRequest for an example that retrieves the value of the payload and publish it to a view.
ActionPayload Methods
The following are methods for ActionPayload.
getName()
Returns the name of the block element.
Signature
1public String getName()
Return Value
Type: String
getType()
Returns the type of the block element.
Signature
1public String getType()
Return Value
Type: String
getValue()
Returns the value of the block element.
Signature
1public Object getValue()
Return Value
Type: Object
ActionPayload.Builder Class
Contains methods to build an instance of the Slack.ActionPayload class.
A Builder object is obtained by invoking one of the ActionPayload.Builder methods defined by the ActionPayload class.
ActionPayload.Builder Methods
The following are methods for ActionPayload.Builder.
build()
Returns an instance of the Slack.ActionPayload object.
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.