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.

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.

  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.

    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.

  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.

  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>.

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

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.

  3. Add the hello.view-meta.xml metadata file in the same directory.

  4. Deploy your view definition.

    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.

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.

  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.

    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.

  4. Deploy your Slack app definition.

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.

  3. Deploy your Slack action.

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.

  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.

  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.