How to send messages to Slack from Marketing Cloud

As we all know, Salesforce Marketing Cloud has many builders and studios to send messages as emails, SMS, and push notifications using Email Studio and Mobile Studio. Also, WhatsApp messages can be sent to subscribers. Recently, with increased popularity, clients are asking for message delivery to the Slack channel. Salesforce has acquired Slack, and there are multiple options to integrate Slack with SalesCloud and service through multiple apps on the Salesforce app exchange, like Salesforce for Slack, Slack Integration by Zapier, and Tray.io. But the challenge is to integrate Salesforce Marketing Cloud with the Slack channel; there are no out-of-the-box connectors or activities in Journey Builders, and there is also nothing specified in the Salesforce road map to enable Slack-SFMC integration.

To solve this problem – when client team insisted on slack communications, we explored the APIs on the slack side to send messages from the marketing cloud. The feasible options available to try were

  1. Sending messages using incoming webhooks
  2. calling Slack API methods from SSJS

https://api.slack.com/methods/chat.postMessage

https://api.slack.com/scopes/chat:write

The public API methods published in Slack were the appropriate method for our requirement because we wanted to send Slack messages to individual Slack IDs and not to the common channel. For the same reason, we also did not use the webhooks option.

After finalizing the approach and API method published by Slack, – next steps were to design the solution on the SFMC side to efficiently send the communications, considering rate limits, network errors, and the retry mechanism. The requirement was to send the communication to the Slack channel along with the user’s email and text message. Here we have used marketing cloud journey builder to send the other two communications and used the target subscriber list in a separate Slack_Comms data extension with columns: address (slackid), messageToBeSent, status (not processed, success, error), etc.

And then developed a SSJS file to make the callout to the Slack API with the required inputs, capture the response, and update the response back to the Slack Data extension to capture the message delivery status. And also considering the rate limiting on the Slack side-enabled retry option three times for the messages that failed for any reason.

Below are the sample SSJ script samples to make the callouts to the Slack API from the marketing cloud.

Communication using Slack Webhooks: Sample SSJS

<script runat="server">
Platform.Load("Core","1");
try{
  var url = 'https://hooks.slack.com/services/T04MEEUHW13/B04MHDDEJ74/tfpVrXAMPHRaQb*******';
  var contentType = 'text/plain';
  var payload = '{"text":"Happy Friday! Please submit your time in workday EOD !!"}';
  var headerNames = [];
  var headerValues = [];
  var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);

  Write(result.StatusCode + '<br>');
  Write(result.Response);
} 
catch(error) {
Write('Message: '+ error);
}
</script>

Communication using Slack API calls : Sample SSJS

<script runat="server">
Platform.Load("Core", "1.1.1");
try{
var slackUrl = 'https://slack.com/api/chat.postMessage';
var slackToken = 'xoxb-4728504608037-4962083441910-******************';
var slackuserId = 'C04V68KG92L';
//var data = Platform.Function.LookupOrderedRows("Random Channel Messages", 4000, "Status desc", "Status", "New");
var myDE = DataExtension.Init("Random Channel Messages");
var data = myDE.Rows.Lookup(['Status'], ['New']);  

Write("Original Data: "+ Stringify(Platform.Function.ParseJSON(data[0]["message"])));

  
for (var i = 0; i < data.length; i++)
{
  
var epayload = '{"channel":"'+data[i]["channelid"]+'","blocks":'+ data[i]["message"] +'}';
  
  //Write("\nOne:" + Stringify(Platform.Function.ParseJSON(epayload)));
  //Write("\nTwo: " + epayload)
  

var request = new Script.Util.HttpRequest(slackUrl);
request.emptyContentHandling = 0;
request.retries = 2;
request.contentType = 'application/json;charset=utf-8';
request.setHeader('Authorization', 'Bearer ' + slackToken);
request.method = 'POST';
//request.postData = Platform.Function.Stringify(Platform.Function.ParseJSON(epayload));
request.postData = epayload;
  var response = request.send();
if (response.statusCode == 200) {
    Write('Message posted successfully to Slack!'+ response.content);
} else {
    Write('Error posting message to Slack: ' + response.content);
}
}
}
catch(error) {

//retry method();
Write('Message: '+ Platform.Function.ParseJSON(error));
}</script>

Key Considerations:

  1. while committing this solution, it is always nice to opt if there is any middleware in place because some middleware tools, like Workato, have slack and SFMC connectors available out of the box and are easy to deal with with rate limits, network errors etc. These tools can easily extract the subscriber ids and core message from the SFMC data extension, further format it and easily deliver it to Slack in an efficient manner.
  2. Message format is also one key thing because if requirement is to include personalization of the user data (to pull user attributes like date of birth, first name, joining date etc) need additional logic. And in some cases, special formatting requirements with font size and images make it complicated.
  3. Emojis are a manageable effort, but again, there is a limitation with attachment size.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top