Create an Apex Handler

You can create an Apex class either via the Developer Console in your org or by creating these files in the force-app/main/default/classes directory in your SFDX project.

  • YourClassName.cls: The Apex class
  • YourClassName.cls-meta.xml: Apex class metadata

For more information, see Create an Apex Class.

Extend the dispatcher based on the type of operation that you’re handling.

1// Action
2public class MyActionDispatcher extends Slack.ActionDispatcher {}
3
4// Event
5public class MyEventDispatcher extends Slack.EventDispatcher {}
6
7// Shortcut
8public class MyShortcutDispatcher extends Slack.ShortcutDispatcher {}
9
10// Slash commands
11public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {}

Override the invoke() Method 

  1. Pass in your Slack parameters and context and return a Slack.ActionHandler instance.

    1public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
    2    return Slack.ActionHandler.modal(new Handler(parameters, context));
    3}

    Slack.ActionHandler has methods to help you structure your handler and enable you to work with modals.

  2. Create a handler that is passed as an argument to Slack.ActionHandler by using the ModalHandler interface.

Handlers can implement Slack.ModalHandler or Slack.RunnableHandler.

Tip

1public class Handler implements Slack.ModalHandler {
2    public Handler (Slack.SlashCommandParameters parameters,
3        Slack.RequestContext context){
4    }
5}
  1. Implement the required methods for the interface by using call().

    1public Slack.ModalView call() {
    2    Slack.ModalView modalView = new Slack.ModalView.builder().build();
    3    return modalView;
    4}

Examples 

Use these examples to implement your Apex handlers.

Basic Slash Command 

This example creates a handler for a slash command, returns the ack() method to avoid the 3-second timeout, and runs a callback method.

1public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {
2
3    public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
4        return Slack.ActionHandler.ack(new Handler());
5    }
6
7    public class Handler implements Slack.RunnableHandler {
8        public void run () {
9            System.debug('Ack Callback');
10        }
11    }
12}

Slash Command Returning a Modal View 

This example handles a slash command and returns a view definition that contains a modal.

1public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {
2
3    public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
4        return Slack.ActionHandler.modal(new Handler(parameters, context), 'my custom modal title');
5    }
6
7    public class Handler implements Slack.ModalHandler {
8
9        Slack.SlashCommandParameters parameters;
10        Slack.RequestContext context;
11
12        public Handler (Slack.SlashCommandParameters parameters, Slack.RequestContext context){
13            this.parameters = parameters;
14            this.context = context;
15        }
16
17        public Slack.ModalView call () {
18
19            Slack.ViewReference viewReference = Slack.View.example_view.get();
20            viewReference.setParameter('title', 'Apex Modal!');
21            viewReference.setParameter('message', context.getAppId() + ' ' + parameters.getText());
22
23            Slack.ModalView modalView = new Slack.ModalView.builder()
24                .viewReference(viewReference)
25                .build();
26
27            return modalView;
28
29        }
30    }
31
32}

Global Shortcut Acknowledging Request 

This example handles a global shortcut by acknowledging the request.

1public class ExampleShortcutDispatcher extends Slack.ShortcutDispatcher {
2
3    public override Slack.ActionHandler invoke(Slack.ShortcutParameters parameters, Slack.RequestContext context) {
4        return Slack.ActionHandler.ack(new Handler(parameters, context));
5    }
6
7    public class Handler implements Slack.RunnableHandler {
8
9        Slack.ShortcutParameters parameters;
10        Slack.RequestContext context;
11
12        public Handler (Slack.ShortcutParameters parameters, Slack.RequestContext context){
13            this.parameters = parameters;
14            this.context = context;
15        }
16
17        public void run () {
18            System.debug('Ack Callback');
19        }
20
21    }
22}

Shortcut Returning a Modal View 

1public class CreateRecordShortcutDispatcher extends Slack.ShortcutDispatcher {
2
3    public override Slack.ActionHandler invoke(Slack.ShortcutParameters parameters, Slack.RequestContext context) {
4        return Slack.ActionHandler.modal(new Handler(parameters, context), 'Create Record');
5    }
6
7    public class Handler implements Slack.ModalHandler {
8
9        Slack.ShortcutParameters parameters;
10        Slack.RequestContext context;
11
12        public Handler (Slack.ShortcutParameters parameters, Slack.RequestContext context){
13            this.parameters = parameters;
14            this.context = context;
15        }
16
17        public Slack.ModalView call () {
18            Slack.ViewReference viewReference = Slack.View.create_record.get();
19            Slack.ModalView modalView = new Slack.ModalView.builder()
20                .viewReference(viewReference)
21                .build();
22            return modalView;
23        }
24    }
25}

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.