Deploy a Slack App

After you set up your development environment and configure your SFDX project, you can create and deploy a Slack app by following these steps.

Prerequisites 

To deploy a Slack app, you must have a Slack account. If you don’t have a Slack account, see slack.com/get-started#/create.

Create a Slack App 

  1. Navigate to https://api.slack.com/apps and click Create New App.

  2. Select From an app manifest, and choose the workspace you want to develop your app in.

  3. Use this app manifest to create your Say Hello app. Update <your-name> with a unique name to differentiate your app. Ignore the <your-app-ID> placeholder for now as you’re updating this information later.

    1_metadata:
    2  major_version: 1
    3  minor_version: 1
    4display_information:
    5  name: <your-name> Say Hello App
    6  description: Say Hello Example App
    7features:
    8  app_home:
    9    home_tab_enabled: true
    10    messages_tab_enabled: false
    11    messages_tab_read_only_enabled: false
    12  bot_user:
    13    display_name: Say Hello
    14    always_online: false
    15  slash_commands:
    16    - command: /apex-hello
    17      url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
    18      description: Say Hello
    19      should_escape: false
    20oauth_config:
    21  redirect_urls:
    22    - https://auth.slack-apps.salesforce.com/slack_oauth_callback/<your-app-ID>
    23  scopes:
    24    user:
    25      - reactions:read
    26      - chat:write
    27    bot:
    28      - channels:read
    29      - chat:write
    30      - commands
    31      - groups:read
    32      - reactions:read
    33      - users:read
    34settings:
    35  event_subscriptions:
    36    request_url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
    37    bot_events:
    38      - app_home_opened
    39      - app_uninstalled
    40      - grid_migration_finished
    41      - grid_migration_started
    42      - team_access_granted
    43      - team_access_revoked
    44      - tokens_revoked
    45  interactivity:
    46    is_enabled: true
    47    request_url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
    48    message_menu_options_url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
    49  org_deploy_enabled: false
    50  socket_mode_enabled: false
    51  token_rotation_enabled: false

    An app manifest enables you to create, configure, and copy your Slack apps easily. If you see an error while saving the manifest, check your indentation. For the manifest schema, see Create and configure apps with manifests.

    Note

  4. Click Next. Verify your app summary and click Create.

  5. After you create the app, click Install to Workspace.

  6. From the Basic Information page, copy the App ID value.

  7. On the Basic Information page, scroll down to the App-Level Tokens section and generate a token. To enable routing on your app’s interactions and event payloads over WebSockets, add the connections:write scope.

    Note of the App ID, App Token, and secrets on this page. We’re using them shortly for the SlackApp metadata file.

    Tip

  8. Navigate to these pages and update the URLs. Replace <your-app-ID> in the app manifest file.

    • Interactivity & Shortcuts > Interactivity

      Update the Request URL, and save your changes.

    • Interactivity & Shortcuts > Select Menus

      Update the Options Load URL, and save your changes.

    • Slash Commands > Edit Command

      Update the Request URL, and save your changes. Repeat this step for each command you have.

    • OAuth & Permissions

      Update the Redirect URLs, click Done, and click Save URLs.

    • Event Subscriptions

      Update the Request URL. Save your changes.

    If any of the request URL is missing from the fields, copy and paste it from another Request URL field. The request URL follows the format https://<Apex SDK for Slack-URL>/a/<your-app-ID>.

    Tip

  9. View the app manifest file in Slack and confirm that the URLs are updated.

Create and Deploy a View Definition 

A view definition is translated to blocks at runtime.

  1. Using VS Code for your scratch org, create a folder viewdefinitions in force-app/main/default.

  2. Create a file called hello.view, inside the viewdefinitions folder. Include this YAML syntax.

    1description: "Say Hello Example"
    2schema:
    3  properties:
    4    name:
    5      type: string
    6      required: true
    7components:
    8  - definition: modal
    9    properties:
    10      title: "Hello {!view.properties.name}"
    11    components:
    12      - definition: section
    13        properties:
    14          text: "You did it!"
  3. Add the hello.view-meta.xml metadata file in the same directory.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<ViewDefinition xmlns="http://soap.sforce.com/2006/04/metadata">
    3    <apiVersion>54.0</apiVersion>
    4    <isProtected>false</isProtected>
    5    <masterLabel>Hello World Example</masterLabel>
    6    <targetType>slack</targetType>
    7</ViewDefinition>
  4. Deploy your view definition.

    1sf project deploy start

    If you see an error when deploying, run sf update stable to update to the latest version. Run sf autocomplete --refresh-cache after you update Salesforce CLI to ensure that autocomplete works correctly on any new commands.

If you’ve been using Salesforce CLI for a long time, and have previously set up Toolbelt with SFDX, uninstall the old unused plugins using sf plugins uninstall salesforce-alm and sf plugins uninstall salesforcedx.

Tip

Create and Deploy a SlackApp Definition 

The SlackApp metadata type contains configuration information such as the app’s commands and shortcut handlers, and the app’s token and secrets.

To create the metadata definition:

  1. Create a folder slackapps inside force-app/main/default.

  2. Create a file called HelloSlackApp.slackapp, inside the slackapps folder. SlackApp files use YAML syntax. The Slash command defined as follows must match your Slack App configuration on api.slack.com.

    1description: Hello World Example App
    2commands:
    3  /apex-hello:
    4    action:
    5      definition: apex__action__SayHello
    6    title: Say Hello
    7    description: A command to say Hello
  3. In the force-app/main/default/slackapps directory, create a HelloSlackApp.slackapp-meta.xml file. Replace the * placeholders in the example with the values for your Slack app.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<SlackApp xmlns="http://soap.sforce.com/2006/04/metadata">
    3    <appKey>*</appKey>
    4    <appToken>*</appToken>
    5    <botScopes>
    6        chat:write,chat:write.public,incoming-webhook,channels:history,groups:history,channels:read,reactions:read,commands
    7    </botScopes>
    8    <clientKey>*</clientKey>
    9    <clientSecret>*</clientSecret>
    10    <isProtected>false</isProtected>
    11    <masterLabel>Hello Slack App</masterLabel>
    12    <signingSecret>*</signingSecret>
    13    <userScopes>
    14        im:read,im:write,channels:read,channels:write,chat:write,groups:read,mpim:read,mpim:write,users:read,groups:write
    15    </userScopes>
    16</SlackApp>

    The appKey and clientKey fields correspond to App ID and Client ID on api.slack.com. The appToken starts with xapp-1-. To locate the app token, see the App-Level Tokens section on the Basic Information page.

    Tip

  4. Deploy your Slack app definition.

    1sf project deploy start

Create and Deploy a Slack Action 

Let’s add some interactivity for the /apex-hello slash command you introduced in the app manifest, using a slash command dispatcher.

  1. In the classes directory, open the SayHello.cls class.

  2. Paste in the following code and save the file.

    1public class SayHello 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), 'Say Hello');
    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           String name = parameters.getText();
    19           Slack.ViewReference viewReference = Slack.View.hello.get();
    20           viewReference.setParameter('name', name);
    21           Slack.ModalView modalView = new Slack.ModalView.builder()
    22               .viewReference(viewReference)
    23               .build();
    24           return modalView;
    25       }
    26   }
    27}
  3. Deploy your Slack action.

    1sf project deploy start

Run Your Slack App 

Let’s see your Slack app in action.

  1. From the Slack Desktop client, go to a public channel and enter the following slash command: /apex-hello.

    If the slash command isn’t available, make sure you’ve clicked the Install to Workspace button as described in Create a Slack App.

    Tip

  2. When prompted to connect your app, click the Connect button in Slack.

To connect your Slack app and scratch org:

  1. Accept the OAuth screen from Slack.

  2. When you get to the Salesforce login page, select the Would you rather connect to a sandbox? link, if you’re not already on test.salesforce.com.

    If your scratch org uses a custom domain, for example, connect-efficiency-1197-dev-ed.cs91.my.salesforce.com, click Use Custom Domain, enter your custom domain connect-efficiency-1197-dev-ed, and click Continue.

    Tip

  3. Sign in with your scratch org username and password. When prompted to select which channel you want to allow your app to post to, select the channel and press Allow.

  4. When you’re connected successfully, a success message is displayed in the browser.

  5. You can now close the window and return to Slack.

  6. Execute the slash command again: /apex-hello World and observe that the modal appears.

    If you see an error that a channel is not found when running a slash command, it usually means the bot doesn’t have access to post a message to the channel. Try adding the bot to the channel by typing @<app name> and then following the onscreen instructions.

    Tip

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.