Get Slack Messages Directly from Salesforce

Do you want to integrate your Salesforce with Slack so users can get notified when there is important activity that requires their attention? Slack makes this incredibly easy, sexy, and it is well documented… for the most part. If you need to a go-to guide for this use case, read on!

Salesforce Integration with Slack

First of all—big shout out to Christophe Coenraets who has written a series of blog articles that were instrumental to my solution. His blog articles got me 95% of the way there, enabling me to send messages to a designated channel through the use of Slack’s Webhooks URLs. No need for an OAuth dance or another multi-step integration.

Business Requirements

I needed to send a Direct Message to a particular Slack user when a Salesforce Case was assigned to them. For my use case, there was no user involvement; it was all being done from within a trigger (with the use of the @future tag). That said, how do you handle authentication when the process is automated?

As detailed in Christophe’s blog articles, Slack provides a Webhook URL that is unique to your instance. We can send RESTful Web Service API calls to Slack, and the message will be sent to all users subscribed to the channel designated when the Webhook was created. Easy stuff.

When we want to send a message to a direct channel, it requires an access token, and getting an access token requires OAuth. Slack only supports web-based OAuth (a.k.a. a user provides a username and password in a browser), which didn’t meet our requirements. That left me with three possibilities:

  • Create a Slack user specifically for our integration and save the username/password in our Salesforce org in a Custom Setting or a Custom Object. This is not ideal due to security concerns, but furthermore, it would be complex because it requires back-and-forth between the user (Salesforce trigger) and channel. No thanks—next idea?
  • Go back to the client and tell them that their requirement is not feasible. (I’m not a big fan of this.)
  • Keep digging, keep digging—back to google…

Business Solution

I kept googling and it paid off! I continually saw a reference to “access token” as one of the JSON properties to send to the Slack API, but I couldn’t figure out where I could get said access token. Finally, the light bulb went off!

It’s actually not difficult at all… I was just missing the first crucial step— creating a BOT users that will automatically create an access token for you to use!

Configuration Steps – Slack

You need to create a designated channel in Slack for incoming messages, as well as a Webhook (endpoint specified in Slack for 3rd party access/integration):

  • Go to Slack web interface
  • Select Apps & Integration. From there—select Manage link in upper right corner
  • Select Custom Integrations from the left-hand Manage menu
  • Select Incoming Webhooks and then select ‘Add Configuration’ green button
  • Select Channel created in step #1 and click ‘Add Incoming Webhooks integration’
  • On the next page, take note of the Webhook URL. This is the value for the Webhook Endpoint field in the Slack Configuration Config Settings

You also need to create a BOT user:

  • Go to Slack web interface
  • Select Apps & Integration. From there—select Manage link in upper right corner
  • Select Custom Integrations from the left-hand Manage menu
  • Select Bots
  • Select ‘Add Configuration’ green button on the left
  • Specify a username for your Bot
  • Click the ‘Add bot integration’ green button
  • On next page, take note of API Token. This will be required when creating the Salesforce Configuration.

Configuration Steps – Salesforce

  • In your Salesforce instance, go to Setup->Security Controls->Remote Site Settings
  • Click ‘New Remote Site’ button
  • Name your Remote Site a name of your choosing
  • Set Remote Site URL = https://slack.com
  • Make sure Active checkbox is selected.
  • Click ‘Save’

Create a custom setting to hold the following Configuration Details:

  • Webhook Endpoint = Webhook created in Step #2 of Configuration Steps – Slack
  • WebAPI Endpoint = “https://slack.com/api”
  • BOT Access Token = API Token created in Step #3 ofConfiguration Steps – Slack

You also need to connect Salesforce users with Slack users. The simplest way to achieve this is to add a custom text field to the standard User object in Salesforce. The value of the field should be the user’s Slack UserId.

Implementation Steps

Once you have the Configuration for the integration complete, it is time to get your hands dirty and write the code. Here is the sequence of steps that needs to happen to post a direct message to a user:

NOTE: For all Slack API calls, pass in the BOT access token as a parameter

1. Open Connection between Salesforce and Slack
2. Once the connection is established, get the list of Users.(PATH = ‘/users.list’)
3. Parse return list of Users to find the one that matches our Salesforce users’ Slack username field
4. Open the Direct Channel Connection (PATH = ‘/im.open’)
5. Parse the response from Open Connection for the Channel Id
6. Send direct message (PATH=’/chat.postMessage’) ensuring to set Channel Id parameter. Voila—You did it!!

Find out how we can help with more of your Salesforce integrations with our custom development support.