Skip to content

Instantly share code, notes, and snippets.

@abeyer
Created February 25, 2016 22:50
Show Gist options
  • Save abeyer/15b1752b3009a61c84d9 to your computer and use it in GitHub Desktop.
Save abeyer/15b1752b3009a61c84d9 to your computer and use it in GitHub Desktop.
Post to slack webhooks from salesforce process builder
public with sharing class SlackMessage {
public class Params {
@InvocableVariable(label='Message' required=true)
public String msg;
}
@InvocableMethod(
label='Send a message to Slack'
description='Send the Message argument as text to a Slack webhook endpoint.'
)
public static void sendSlackMessage(List<Params> ps){
Params p = ps[0];
Map<String, String> jsonMessage = new Map<String,String>();
// Set the message text.
jsonMessage.put('text', p.msg);
// Set slack sending user info:
jsonMessage.put('username', 'salesforce-bot');
jsonMessage.put('icon_emoji', ':envelope:');
//jsonMessage.put('channel', '#channel-name'); // to send to a channel other than the default
//jsonMessage.put('channel', '@user'); // to message a specific user
slackMessageCallout(JSON.serialize(jsonMessage));
}
@future(callout=true)
private static void slackMessageCallout(String body) {
HttpRequest req = new HttpRequest();
req.setBody(body);
// TODO: Slack will give you this endpoint url when you setup a webhook for your team
req.setEndpoint('https://hooks.slack.com/services/...');
req.setMethod('POST');
Http http = new Http();
HTTPResponse res = http.send(req);
if (res.getStatusCode() >= 400) {
System.debug(res.getBody());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment