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 classYourClassName.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.
// Action
public class MyActionDispatcher extends Slack.ActionDispatcher {}
// Event
public class MyEventDispatcher extends Slack.EventDispatcher {}
// Shortcut
public class MyShortcutDispatcher extends Slack.ShortcutDispatcher {}
// Slash commands
public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {}
Override the invoke() Method
-
Pass in your Slack parameters and context and return a
Slack.ActionHandlerinstance.public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) { return Slack.ActionHandler.modal(new Handler(parameters, context)); }Slack.ActionHandlerhas methods to help you structure your handler and enable you to work with modals. -
Create a handler that is passed as an argument to
Slack.ActionHandlerby using theModalHandlerinterface.
tip: Handlers can implement Slack.ModalHandler or Slack.RunnableHandler.
public class Handler implements Slack.ModalHandler {
public Handler (Slack.SlashCommandParameters parameters,
Slack.RequestContext context){
}
}
-
Implement the required methods for the interface by using
call().public Slack.ModalView call() { Slack.ModalView modalView = new Slack.ModalView.builder().build(); return modalView; }
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.
public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {
public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
return Slack.ActionHandler.ack(new Handler());
}
public class Handler implements Slack.RunnableHandler {
public void run () {
System.debug('Ack Callback');
}
}
}
Slash Command Returning a Modal View
This example handles a slash command and returns a view definition that contains a modal.
public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {
public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
return Slack.ActionHandler.modal(new Handler(parameters, context), 'my custom modal title');
}
public class Handler implements Slack.ModalHandler {
Slack.SlashCommandParameters parameters;
Slack.RequestContext context;
public Handler (Slack.SlashCommandParameters parameters, Slack.RequestContext context){
this.parameters = parameters;
this.context = context;
}
public Slack.ModalView call () {
Slack.ViewReference viewReference = Slack.View.example_view.get();
viewReference.setParameter('title', 'Apex Modal!');
viewReference.setParameter('message', context.getAppId() + ' ' + parameters.getText());
Slack.ModalView modalView = new Slack.ModalView.builder()
.viewReference(viewReference)
.build();
return modalView;
}
}
}
Global Shortcut Acknowledging Request
This example handles a global shortcut by acknowledging the request.
public class ExampleShortcutDispatcher extends Slack.ShortcutDispatcher {
public override Slack.ActionHandler invoke(Slack.ShortcutParameters parameters, Slack.RequestContext context) {
return Slack.ActionHandler.ack(new Handler(parameters, context));
}
public class Handler implements Slack.RunnableHandler {
Slack.ShortcutParameters parameters;
Slack.RequestContext context;
public Handler (Slack.ShortcutParameters parameters, Slack.RequestContext context){
this.parameters = parameters;
this.context = context;
}
public void run () {
System.debug('Ack Callback');
}
}
}
Shortcut Returning a Modal View
public class CreateRecordShortcutDispatcher extends Slack.ShortcutDispatcher {
public override Slack.ActionHandler invoke(Slack.ShortcutParameters parameters, Slack.RequestContext context) {
return Slack.ActionHandler.modal(new Handler(parameters, context), 'Create Record');
}
public class Handler implements Slack.ModalHandler {
Slack.ShortcutParameters parameters;
Slack.RequestContext context;
public Handler (Slack.ShortcutParameters parameters, Slack.RequestContext context){
this.parameters = parameters;
this.context = context;
}
public Slack.ModalView call () {
Slack.ViewReference viewReference = Slack.View.create_record.get();
Slack.ModalView modalView = new Slack.ModalView.builder()
.viewReference(viewReference)
.build();
return modalView;
}
}
}