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.
-
Navigate to https://api.slack.com/apps and click Create New App.
-
Select From an app manifest, and choose the workspace you want to develop your app in.
-
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.
-
Click Next. Verify your app summary and click Create.
-
After you create the app, click Install to Workspace.
-
From the Basic Information page, copy the App ID value.
-
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.
-
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>
. -
-
View the app manifest file in Slack and confirm that the URLs are updated.
A view definition is translated to blocks at runtime.
-
Using VS Code for your scratch org, create a folder
viewdefinitions
inforce-app/main/default
. -
Create a file called
hello.view
, inside theviewdefinitions
folder. Include this YAML syntax. -
Add the
hello.view-meta.xml
metadata file in the same directory. -
Deploy your view definition.
If you see an error when deploying, run
sf update stable
to update to the latest version. Runsf 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:
-
Create a folder
slackapps
insideforce-app/main/default
. -
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 onapi.slack.com
. -
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
andclientKey
fields correspond to App ID and Client ID onapi.slack.com
. TheappToken
starts withxapp-1-
. To locate the app token, see the App-Level Tokens section on the Basic Information page. -
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.
-
In the
classes
directory, open the SayHello.cls class. -
Paste in the following code and save the file.
-
Deploy your Slack action.
Let's see your Slack app in action.
-
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.
-
When prompted to connect your app, click the Connect button in Slack.
To connect your Slack app and scratch org:
-
Accept the OAuth screen from Slack.
-
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 domainconnect-efficiency-1197-dev-ed
, and click Continue. -
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.
-
When you're connected successfully, a success message is displayed in the browser.
-
You can now close the window and return to Slack.
-
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.