visit
In our last post, we familiarized ourselves with the starter kit and set up our development environment to build a basic Slack app. In this post, we’ll continue building on that Slack app, focusing on how the Slack Starter Kit makes it easy to develop two essential components of every Slack app:
If you’ve ever developed a front-end web app using JavaScript or CSS, you’re probably familiar with the basic tenets of event handling. Events in this context are defined as the actions a user takes on your website. For example, a mouse pointer clicking on a link emits an onclick
event; a pointer linking over a piece of text emits onhover
; a keyboard keystroke emits onkeypressed
; and so on.
When your app receives the event from Slack, you can do anything you want: respond to the user, pop open a dialog to ask for more information, or simply store some information away in your database. There’s only one important point to remember: . This is to let Slack know that your app received its payload and is working on a response. You can do that work in the background—perhaps on a different thread, taking as long as you need—but only after you let Slack know everything is 200 OK
.
Let’s take a look at how event handling works in practice. Head on over to your App Manifest, and scroll down to the bottom of the page, near the settings
key. It should look like this:
settings:
event_subscriptions:
request_url: //<your-app-name>.herokuapp.com/slack/events
bot_events:
- app_home_opened
interactivity:
is_enabled: true
request_url: //<your-app-name>.herokuapp.com/slack/events
This section identifies where Slack should send its payload once an event occurs. The bot_events
key already defines one event to listen to, , which is triggered once your app is opened in Slack.
Now, open up the local copy of your Starter Kit in your IDE; navigate to apps/slack-salesforce-starter-app/listeners/events/app-home-opened.js
. As we saw in the last post, the Starter Kit has an opinionated directory structure to resolve any ambiguities as to where modifications should be made. In this case, the events folder is responsible for defining all of our event responses, just like the shortcuts folder defined our slack command in the previous article. Search for the first occurrence of the following line:
client.views.publish
As the method name implies, this function uses a Slack SDK to call . The main view for this event is created by a function called authorization_success_screen
, which can be found in apps/slack-salesforce-starter-app/user-interface/app-home/auth-success.js
.
let event_ts = event.event_ts;
Change both authorization_success_screen
calls to include this variable as a new argument:
view: authorization_success_screen(
currentuser.username,
event_ts
)
Finally, open up auth-success.js
, change the method signature to include event_ts, and modify the displayed string to include this information:
'use strict';
const { HomeTab, Blocks } = require('slack-block-builder');
const authorization_success_screen = (username, event_ts) => {
// preceding code remains unchanged
Blocks.Section({
text: `It's ${event_ts}, and you are successfully authenticated to Salesforce as ${username}.`
})
);
// continued code remains unchanged
$ git add .
$ git commit -m "Add event timestamp"
$ git push heroku main
Responding to app_home_opened
can be a useful event to listen to whenever your app needs a centralized location to fetch data. Because of this, our next step will be to fetch data from Salesforce and present it in our Slack UI.
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Save"
},
"style": "primary"
}
sfdx force:org:open
Contact First Name, Contact Last Name, Contact Description
Arden, Isabela, Lorem ipsum dolor sit amet
Rowina, Arti, Proin a est sit amet quam varius efficitur.
Aislin, Oedipus, Praesent et euismod sem
Sindri, Filbert, Proin facilisis sit amet libero vulputate sodales
Cyril, Gratien, Nulla at massa eu turpis venenatis egestas
Next, we’re going to make changes similar to the ones we made before when adding the event timestamp. A variable called conn
is available to use, which serves as a connection to Salesforce. We can use it to query for Contact data, which is what we’re interested in displaying in Slack.
Navigate to apps/slack-salesforce-starter-app/user-interface/app-home/auth-success.js
. We’ll await the resolving of the promise in this function, and we’ll include the existing conn
variable as a new argument:
view: await authorization_success_screen(
currentuser.username,
event_ts,
conn
)
And once again, open up auth-success.js
and change the method signature to include conn
. You also need to declare the function as async:
'use strict';
const { HomeTab, Blocks } = require('slack-block-builder');
const authorization_success_screen = async (username, event_ts, conn) => {
// continued code remains unchanged for now
Since we already have a connection to Salesforce, fetching the data is as easy as issuing an SQL query. Place this code right before the place where the homeTab
variable is defined:
const result = await conn.query(
`SELECT Name, Description FROM Contact`
);
let records = result.records;
let fields = records.map((record) => {
return `*${record.Name}*: ${record.Description}`;
});
Blocks.Section({
text: `It's ${event_ts}, and you are successfully authenticated to Salesforce as ${username}.`
}),
Blocks.Header({ text: 'Contacts' }),
Blocks.Divider(),
Blocks.Section({
text: fields.join('\n')
})
We’ve built a Slack app that fetches data from Salesforce and presents that data in a UI using a framework called Block Kit. In our next post, we’ll send data from Slack back to Salesforce!