Apex SDK for Slack (Beta)
Documentation Changelog
Enable Slack for Salesforce Beta
Set Up Your Development Environment
Set Up Your SFDX Project
Deploy a Slack App
Sample Apex SDK for Slack App
The sample app includes a variety of view definitions and Apex handlers to demonstrate its core functionalities.
Before you download the sample app, make sure you’ve set up the prerequisites.
There are several versions of the sample app.
apex-end-to-end-example-v5.zip: This file has new changes.
TestEventDispatcherEventExample test exampleActionDispatcherViewSelectedRecord class that handles the onsubmit event on the view_oppportunities_by_account view definition.ApexSlackApp.slackapp app definition for the reaction_added and reaction_removed events.apex-end-to-end-example-v4.zip: This file has new changes.
EventDispatcherEventExample class to demonstrate event support.ApexEndToEndTestSuite.TestDataProviderAccountLookup, TestDataProviderOpportunityLookup, and TestDataProviderSObjectRecord.TestOpportunityLookupDataProvider.RecordDataProvider to EventDispatcherAppHomeOpened.TestAppHomeOpened is now TestEventDispatcherAppHomeOpenedTestCreateRecordDispatcher is now TestShortcutDispatcherCreateRecordTestReactionsGetShortcutDispatcher is now TestShortcutDispatcherReactionsGetTestViewOppByAccountCommandDispatcher is now TestCommandDispatcherViewOppByAccountTestViewRecordCommandDispatcher is now TestCommandDispatcherViewRecord.CreateRecordCommandDispatcher is now CommandDispatcherCreateRecordCreateRecordShortcutDispatcher is now ShortcutDispatcherCreateRecordViewOppByAccountCommandDispatcher is now CommandDispatcherViewOppByAccountViewRecordCommandDispatcher is now ShortcutDispatcherReactionsGet.apex-end-to-end-example-v3.zip: This file has new changes.
OpportunityLookupDataProvider and ViewOppByAccountCommandDispatcher to demonstrate how to use the Select Component using List<Slack.Option> and List<Slack.OptionGroup>.apex-end-to-end-example-v2.zip: Added a ReactionsGetResponse example
After you download the sample app, unzip the file, and move the content of the force-app folder into your SFDX project.
Tip
After setting up your prerequisites, create a Slack app to work with your sample app.
Create an app on api.slack.com and use the following app manifest.
1_metadata:
2 major_version: 1
3 minor_version: 1
4display_information:
5 name: Apex Example App
6features:
7 app_home:
8 home_tab_enabled: true
9 messages_tab_enabled: false
10 messages_tab_read_only_enabled: false
11 bot_user:
12 display_name: apex-end-to-end-example-app
13 always_online: true
14 shortcuts:
15 - name: apex create record
16 type: message
17 callback_id: apex-create-record-message
18 description: apex create record global
19 - name: apex create record
20 type: global
21 callback_id: apex-create-record-global
22 description: Create a record with Apex
23 - name: apex reactions get
24 type: message
25 callback_id: apex-reactions-get
26 description: Retrieve reactions for a message with the ReactionsGet api
27 slash_commands:
28 - command: /apex-create-record
29 url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
30 description: Create a record with Apex
31 should_escape: false
32 - command: /apex-view-record
33 url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
34 description: View a record with Apex
35 should_escape: false
36 - command: /apex-find-opp-by-account-id
37 url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
38 description: view opportunities by accountId
39 should_escape: false
40oauth_config:
41 redirect_urls:
42 - https://auth.slack-apps.salesforce.com/slack_oauth_callback/<your-app-ID>
43 scopes:
44 user:
45 - channels:read
46 bot:
47 - chat:write
48 - commands
49 - channels:history
50 - groups:history
51 - im:history
52settings:
53 event_subscriptions:
54 bot_events:
55 - app_home_opened
56 - app_uninstalled
57 - channel_rename
58 - group_rename
59 - reaction_added
60 - reaction_removed
61 - tokens_revoked
62 request_url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
63 interactivity:
64 is_enabled: true
65 message_menu_options_url: https://slack-apps.salesforce.com:9443/a/<your-app-Id>
66 request_url: https://slack-apps.salesforce.com:9443/a/<your-app-ID>
67 org_deploy_enabled: false
68 socket_mode_enabled: false
69 token_rotation_enabled: falseThe app_uninstalled and tokens_revoked events are required if you expect to disconnect your Slack app from an org and reconnect to a different org. See Org Connections.
Important
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.
Alternatively, update your app ID via the OAuth & Permissions page. Under Redirect URLs, replace <your-app-id> with the app ID on the Basic Information page.
Note
slackapp/ApexSlackApp.slackapp-meta.xml.ApexSlackApp.slackapp match the values in your app manifest on api.slack.com.To use a different name for your app besides “ApexSlackApp,” you can change the name of the .slackapp and .slackapp-meta.xml files, like myApp.slackapp and myApp.slackapp-meta.xml. Update any references to your app name in apex classes. For example in EventDispatcherAppHomeOpened, replace Slack.App.ApexSlackApp.get() with Slack.App.myApp.get().
Tip
Next, you can deploy your sample app to a scratch org using sf project deploy start. If you haven’t set up a scratch org, see Create a Scratch Org.
After you push your sample app to a scratch org, you can test the commands in your Slack workspace as discussed in this section.
For example, when you run /apex-create-record the first time, you get a prompt to connect your Salesforce org. Click the link and log in with your scratch org’s username and password. After you successfully connect your Salesforce org, run your command again.
To see your username, run sf org list. To generate a password, run sf org generate password --target-org <yourusername>@example.com.
Tip
The sample app demonstrates the following areas.
The ApexSlackApp.slackapp file configures these commands and the Apex actions they call.
1commands:
2 /apex-create-record:
3 action:
4 definition: apex__action__CommandDispatcherCreateRecord
5 title: Create Record Command
6 description: Create a record using a slack command.
7 /apex-view-record:
8 action:
9 definition: apex__action__CommandDispatcherViewRecord
10 title: View Record Command
11 description: View record details by objectApiName and recordId.
12# More shortcut definitions here/apex-create-record calls the CommandDispatcherCreateRecord Apex class to open a modal using the create_record view definition.
/apex-view-record calls the CommandDispatcherViewRecord Apex class to open a modal to view record details for Accounts, Contacts, or Opportunities using the corresponding view definitions: view_account, view_contact, or view_opportunity.
The CommandDispatcherViewRecord Apex class extends Slack.SlashCommandDispatcher to handle the slash command that’s registered with the Slack app.
For example, you can enter a command like /apex-view-record Account 00Bxx0000029bnsEAA and the Apex action returns a modal described by the view_account definition.
The ApexSlackApp.slackapp file configures a global shortcut and its Apex action.
1globalShortcuts:
2 apex-create-record-global:
3 action:
4 definition: apex__action__ShortcutDispatcherCreateRecord
5 title: Create Record Global Shortcut
6 description: Create a record using a slack global shortcut.The apex-create-record-global global shortcut calls the ShortcutDispatcherCreateRecord Apex class to open a modal using the create_record view definition.
The ShortcutDispatcherCreateRecord Apex class extends Slack.ShortcutDispatcher to handle the global and message shortcuts that are registered with the Slack app.
To test out the global shortcut, press the shortcuts button in the message composer or from within search in your Slack workspace. For more information, see Shortcuts.
The ApexSlackApp.slackapp file configures a message shortcut to call an Apex action.
1messageShortcuts:
2 apex-create-record-message:
3 action:
4 definition: apex__action__ShortcutDispatcherCreateRecord
5 title: Create Record Message Shortcut
6 description: Create a record using a slack message shortcut.The apex-create-record-message message shortcut calls the ShortcutDispatcherCreateRecord Apex class to open a modal using the create_record view definition.
The ShortcutDispatcherCreateRecord Apex class extends Slack.ShortcutDispatcher to handle the slash command registered with the Slack app.
To test out the message shortcut, go to the overflow menu on a message. If the shortcut isn’t available on the menu, click “More message shortcuts…” and find the shortcut by typing it in the search field in your Slack workspace.
The ApexSlackApp.slackapp file configures the app_home_opened event to call an Apex action.
1events:
2 app_home_opened:
3 action:
4 definition: apex__action__EventDispatcherAppHomeOpened
5 title: Example event handler for app_home_opened
6 description: event fires when a user opens the home tab.
7# More event definitions hereWhen a user clicks into the App Home tab, the EventDispatcherAppHomeOpened Apex class returns the app_home view definition.
The EventDispatcherAppHomeOpened Apex class extends Slack.EventDispatcher to handle the app_home_opened event.
The example app includes an implementation of the test harness. The ApexExampleEndToEndTestSuite test suite in force-app/main/default/testSuites contains tests for commands, shortcuts, and event handlers in the example app that are currently supported by the test harness.
To run the Apex test suite:
1sf apex run test --suite-names ApexExampleEndToEndTestSuiteTo run the Apex test suite with coverage:
1sf apex run test --suite-names ApexExampleEndToEndTestSuite --code-coverage --result-format humanSee the apex run test command.
Tip
Alternatively, run the test suite in the Developer Console after logging into the scratch org.
Beta Feature