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!
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.
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:
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!
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):
You also need to create a BOT user:
Create a custom setting to hold the following Configuration Details:
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.
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.