Create Apex Handlers for Slack Apps

Use these Apex classes to create handlers for Slack actions, commands, shortcuts, and events.

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.

Examples

This example uses ActionDispatcher to handle a form submission.

This example uses ActionDispatcher to display a view.

You can close the modal and optionally clear the view stack using clearModal(). To close the modal without clearing the view stack, use ack(handler) instead.

For an example on invoking an action handler on the App Home page, such as with SlashCommandDispatcher, see ChatPostMessageRequest.

ActionDispatcher Methods

The following are methods for ActionDispatcher.

invoke(parameters, context)

Invokes the action handler.

Signature

Parameters

parameters

Type: Map<String, Object>

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.


The Slack.ActionHandler class provides methods that control how Slack actions are handled. The handler processes a single request.

Usage

The ActionHandler methods enable you to work with modals. Responding to Slack actions with a modal enables you to 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 3 views simultaneously in a view stack. Only a single view is visible each time. However, the view stack can retain previous views and can return to them with their prior state still 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.

The message_modal.view file looks like this.

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, publish to app home, and so on. 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 that are subject to the trigger_id timeouts, since it may 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.

Signature

Parameters

handler

Type: Slack.RunnableHandler

The handler that clears the modal from the view stack.

Return Value

Type: Slack.ActionHandler

clearModal(handler)

Clears the full stack of modals. By default, Slack pops one modal off the top of the stack.

Signature

Parameters

handler

Type: RunnableHandler

The handler that clears the modal from the view stack.

Return Value

Type: Slack.ActionHandler

modal(handler)

Displays a Loading modal with automatic staging to satisfy the 3-second timeout rule.

Signature

Parameters

handler

Type: Slack.ModalHandler

The handler for the modal.

Return Value

Type: Slack.ActionHandler

modal(handler, stagedModalTitle)

Displays a Loading modal with a title. The modal has automatic staging to satisfy the 3-second timeout rule.

Signature

Parameters

handler

Type: Slack.ModalHandler

The handler for the modal.

stagedModalTitle

Type: String

The title to display on the modal in Slack.

Return Value

Type: Slack.ActionHandler

pushModal(handler)

Pushes another modal to the view stack with automatic staging to satisfy the 3-second timeout.

Signature

Parameters

handler

Type: Slack.ModalHandler

The handler that pushes the modal to the view stack.

Return Value

Type: Slack.ActionHandler

pushModal(handler, stagedModalTitle)

Push another modal to the view stack with a different title.

Signature

Parameters

handler

Type: Slack.ModalHandler

The handler that pushes the modal to the view stack.

stagedModalTitle

Type: String

The title to display on the modal in Slack.

Return Value

Type: Slack.ActionHandler

updateModal(handler)

Updates a modal. This update can happen whether or not the view is visible within the modal's view stack.

Signature

Parameters

handler

Type: Slack.ModalHandler

The handler that updates the modal.

Return Value

Type: Slack.ActionHandler

updateModal(handler, stagedModalTitle)

Updates a modal with a different title. This update can happen whether or not the view is visible within the modal's view stack.

Signature

Parameters

handler

Type: Slack.ModalHandler

The handler that updates the modal.

stagedModalTitle

Type: String

The title to display on the modal in Slack.

Return Value

Type: Slack.ActionHandler


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.

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

Return Value

Type: String

getType()

Returns the type of the block element.

Signature

Return Value

Type: String

getValue()

Returns the value of the block element.

Signature

Return Value

Type: Object


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.

Signature

Return Value

Type: Slack.ActionPayload

name(name)

Signature

Parameters

type

Type: String

Return Value

Type: Slack.ActionPayload.Builder

type(type)

Signature

Parameters

type

Type: String

Return Value

Type: Slack.ActionPayload.Builder

value(value)

Signature

Parameters

value

Type: Object

Return Value

Type: Slack.ActionPayload.Builder