Skip to content

Salesforce Apex – Using Slack API archive/unarchive a Slack Channel

Before you start, first you have to get a User OAuth Token from Slack so that you can make authorized API calls to Slack from Salesforce.

Step 1: Create a Slack App

  1. Go to Slack API: Navigate to https://api.slack.com/apps.
  2. Create a New App: Click on “Create New App”.
    • From scratch: Choose this option.
    • App Name: Give your app a name.
    • Workspace: Select the Slack workspace you want to use.
    • Click “Create App”.

Step 2: Configure the App

  1. OAuth & Permissions:
    • Navigate to “OAuth & Permissions” in the left sidebar.
    • Scopes: Scroll down to “Scopes” and add the necessary scopes under “User Token Scopes”. For archiving/unarchiving a channel, you need:
      • channels:write, groups:write

Step 3: Install App to Workspace:

  1. Scroll up to “OAuth Tokens for your workspace”.
  2. Click “Install App to Workspace”.
  3. Follow the prompts to authorize the app for your workspace.
  4. After installation, you will receive a User OAuth access token.
  5. Copy the token and store it in a safe place within Salesforce such as named credentials, or Custom Settings, etc. So that you can use it in the apex class.

Apex Class for Archiving Slack Channel: This class uses a hard-coded access token which is a User OAuth Token from Slack.

public class Slack_ArchiveSlackChannel {
    
        public class Requests {            
            @InvocableVariable(label='Slack Channel Id' description='Id of the Slack Channel to archive.' required=true)
            public string channelId;
        }
        public class Response {
            @InvocableVariable(label='is Success?' description='True if channel is archived successfully.')
            public Boolean isSuccess;
    
            @invocableVariable(label='API Response' description='Slack API response.')
            public String apiResponse;
        }
    
        @InvocableMethod(label='Slack - Archive Slack Channel Apex Invocable' iconName='slds:standard:slack')
        public static list<Response> execute (List<Requests> requestList) {
            
            list<Response> RetrunResponse = new List<Response>();
            for(Requests req : requestList){
                
                String accessToken = 'xoxp-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXX-XXXXXXXf190bde5a92c4806952d62a0a';

                HttpRequest HttpReq = new HttpRequest();
                HttpReq.setEndpoint('https://slack.com/api/conversations.archive');
                HttpReq.setMethod('POST');
                HttpReq.setHeader('Content-Type', 'application/x-www-form-urlencoded');
                HttpReq.setHeader('Authorization', 'Bearer ' + accessToken);
                HttpReq.setBody('channel=' + req.channelId);

                Http http = new Http();

                string apiResponse = '';
                HttpResponse slackRes = http.send(HttpReq);
                apiResponse = slackRes.getBody();

                system.debug(slackRes.getBody());
                system.debug(slackRes.getStatusCode());

                //set default to false
                boolean isSuccess = false;
                Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(apiResponse);
                if (responseMap.containsKey('ok')) {
                    isSuccess = (Boolean) responseMap.get('ok');
                }

                Response resp = new Response();
                resp.isSuccess = isSuccess;
                resp.apiResponse = apiResponse;
                RetrunResponse.add(resp);

            }
    
            return RetrunResponse;
        }
    
}

Apex Class for Unarchiving Slack Channel:

public class Slack_UnarchiveSlackChannel {
    
        public class Requests {            
            @InvocableVariable(label='Slack Channel Id' description='Id of the Slack Channel to Unarchive.' required=true)
            public string channelId;
        }
        public class Response {
            @InvocableVariable(label='is Success?' description='True if channel is unarchived successfully.')
            public Boolean isSuccess;
    
            @invocableVariable(label='API Response' description='Slack API response.')
            public String apiResponse;
        }
    
        @InvocableMethod(label='Slack - Unarchive Slack Channel Apex Invocable' iconName='slds:standard:slack')
        public static list<Response> execute (List<Requests> requestList) {
            
            list<Response> RetrunResponse = new List<Response>();
            for(Requests req : requestList){
                
                String accessToken = 'xoxp-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXX-XXXXXXXf190bde5a92c4806952d62a0a';

                HttpRequest HttpReq = new HttpRequest();
                HttpReq.setEndpoint('https://slack.com/api/conversations.unarchive');
                HttpReq.setMethod('POST');
                HttpReq.setHeader('Content-Type', 'application/x-www-form-urlencoded');
                HttpReq.setHeader('Authorization', 'Bearer ' + accessToken);
                HttpReq.setBody('channel=' + req.channelId);

                Http http = new Http();

                string apiResponse = '';
                HttpResponse slackRes = http.send(HttpReq);
                apiResponse = slackRes.getBody();

                system.debug(slackRes.getBody());
                system.debug(slackRes.getStatusCode());

                //set default to false
                boolean isSuccess = false;
                Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(apiResponse);
                if (responseMap.containsKey('ok')) {
                    isSuccess = (Boolean) responseMap.get('ok');
                }

                Response resp = new Response();
                resp.isSuccess = isSuccess;
                resp.apiResponse = apiResponse;
                RetrunResponse.add(resp);

            }
    
            return RetrunResponse;
        }
    
}