Skip to content

Instantly share code, notes, and snippets.

@nabondance
Last active April 18, 2025 11:02
Show Gist options
  • Select an option

  • Save nabondance/0183cfba420ac2977b8f383429a4eb16 to your computer and use it in GitHub Desktop.

Select an option

Save nabondance/0183cfba420ac2977b8f383429a4eb16 to your computer and use it in GitHub Desktop.
Think2 Blog: Example Apex class to send a message to a Slack channel or direct message (DM) using the Slack API
// Example Apex class to send a message to a Slack channel using the Slack API
// Read the entire blog at: https://www.think2.ai/
// Note that there is not a lot of error handling in this code. In a production environment, you should add more error handling and logging.
// This code is for demonstration purposes only and should not be used in a production environment without proper testing and error handling.
// This code is designed to be used as an InvocableMethod in Salesforce, which allows it to be called from Flow or AgentForce.
public with sharing class SendSlackToChannel {
private static final String SLACK_API_URL = 'https://slack.com/api/chat.postMessage';
private static final String HARDCODED_BOT_TOKEN = 'xoxb-your-token-here';
// Input class for the InvocableMethod
public class Input {
@InvocableVariable(
label='Channel ID',
description='Slack Channel ID (e.g. C01XXXXXXX)'
)
public String channelId;
@InvocableVariable(label='Message Text', description='Text of the message to send to the Slack channel')
public String messageText;
}
// Output class for the InvocableMethod
public class Output {
@InvocableVariable(label='Output Message', description='Result message from Slack API')
public String message;
public Output(String message) {
this.message = message;
}
}
@InvocableMethod(
label='Send Slack Message to Channel',
description='Sends a simple message to a Slack channel using the Slack API'
)
public static List<Output> postMessage(List<Input> requests) {
List<Output> outputs = new List<Output>();
for (Input request : requests) {
// Prepare the payload
String payload = '{"channel":"' + request.channelId + '", "text":"' + request.messageText + '"}';
String token = getBotToken();
String responseMessage = slackApiCallout(SLACK_API_URL, 'POST', token, payload);
outputs.add(new Output(responseMessage));
}
return outputs;
}
private static String getBotToken() {
// This is a hardcoded token for demonstration purposes. In a real-world scenario, you should use a more secure method to store and retrieve tokens.
// For example, you could use Custom Metadata Types.
return HARDCODED_BOT_TOKEN;
}
private static String slackApiCallout(String endpoint, String method, String token, String payload) {
if (String.isBlank(token)) {
return 'No token provided';
}
if (String.isBlank(endpoint)) {
return 'No endpoint provided';
}
try {
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod(method);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + token);
if (!String.isBlank(payload)) {
req.setBody(payload);
}
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
System.debug('Slack API callout successful: ' + res.getBody());
return 'Slack API callout successful';
} else {
System.debug('Slack API callout failed: ' + res.getBody());
return 'Slack API callout failed: ' + res.getBody();
}
} catch (Exception e) {
System.debug('HTTP response error: ' + e.getMessage());
return 'HTTP response error: ' + e.getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment