Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

integration - Send Slack Notification to multiple channels with an Apex Class in Salesforce

I have an apex class allowing me to send slack notification to a channel. But I would like to be able to send slack notifications to multiple slack channels.

Does anyone has an answer or is able to point me the a documentation to achieve this? Thanks

here the code allowing the send slack notification to a single channel.

public without sharing class SlackNotificationSupport{
   
public class slackRequest { 
    @InvocableVariable(label='caseNumber')
    public String caseNumber;
    @InvocableVariable(label='status')
    public String status;
    @InvocableVariable(label='nickName')
    public String nickname;
    @InvocableVariable(label='queue')
    public String queue;
    @InvocableVariable(label='accountTier')
    public String accountTier;
    @InvocableVariable(label='accountName')
    public String accountName;
    @InvocableVariable(label='subject')
    public String subject;
    @InvocableVariable(label='businessUnit')
    public String businessUnit;
} 


public static String setChannelName(String queue) {

    String channelName;

    channelName = '#'+queue;
    channelName = channelName.toLowerCase('en');
    channelName = channelName.replaceAll('queue', 'bot');
    channelName = channelName.replaceAll('[_]', '-');
    return channelName;
}

@InvocableMethod(label='Publish to Slack')
public static void publishToSlack(List<slackRequest> requests) {

    String webhookURL = system.label.Param_Slack_Token;
    String msg;
    String channelName;

    for(slackRequest r:requests){

        if (r.queue == 'internal'){
            System.debug('### SlackNotificationSupport new internal case');
            channelName = '#'+Label.Slack_Internal_Case_Channel;
            msg = 'A new internal case has been created : *'+r.caseNumber+'* - By User : (*'+r.accountName+'*) - Subject : (*'+r.subject+'*)';                
        }
        else if (r.queue == 'caseconcern'){
            System.debug('### SlackNotificationSupport new case concern');
            channelName = '#'+Label.Slack_Case_Concern_Channel;
            
            msg = 'A new Case Concern has been created : *'+r.caseNumber+'* - By User : (*'+r.nickName+' '+r.accountName+'* From *'+r.accountTier+'*) - Category : (*'+r.subject+'*)';
            msg += '
Link to Case Concern : '+URL.getOrgDomainUrl().toExternalForm()+'/'+r.status;
            
        }
        // Team Leads
        else if (r.queue == 'Queue Team Leads') {
            channelName = setChannelName(r.queue);
            msg = 'A customer has opened a new case.
>>>*'+
                r.caseNumber+'* - '+r.subject;
                System.debug('### SlackNotificationSupport Queue Team Leads');
        } // New Tier 1 Ticker
        else if (r.accountTier == 'Tier 1' && r.accountName != null && r.queue != null) {
            System.debug('### SlackNotificationSupport New Tier 1');
            channelName = setChannelName(r.queue);
            msg = 'The customer '+r.accountTier+' - *'+r.accountName+'* has opened a new case.
>>>*'+
                r.caseNumber+'* - '+r.subject;
        }// Assigned ticket, status to Open - Internal notification for awaiting feedback cases
        else if (r.nickname != null && r.status != null && r.caseNumber != null) {
            System.debug('### SlackNotificationSupport  Status x to Open');
            channelName = '@'+r.nickname;
            if(r.queue == 'internal_notification')msg = 'Salesforce Internal Case number *'+r.caseNumber+'* related to : *'+r.subject+'* - Status changed to : *'+r.status+'*.';
            else msg = 'Case number *'+r.caseNumber+'* has become '+r.status+'.';
        }

        // Generate JSON for request
        try {
            if (r.queue != null || r.nickname != null) {
                System.debug('### SlackNotificationSupport Sending message');
                JSONGenerator gen = JSON.createGenerator(true);
                gen.writeStartObject(); //Inserts {
                gen.writeStringField('text', msg);
                gen.writeStringField('channel', channelName);
                gen.writeStringField('username', 'bot-support');
                gen.writeStringField('icon_emoji', ':smartplus:');
                gen.writeEndObject(); //Inserts }
                String body = gen.getAsString(); //Translates JSONGenerator to string to be passed to callout
                System.debug('### SlackNotificationSupport body: '+ body);
                System.enqueueJob(new qCallOut(webhookURL, 'POST', body)); // Send request
            }
            else {
                System.debug('### SlackNotificationSupport Queue = '+ r.queue);
                return;
            }
        } // try    
        catch (exception e){
            system.debug('### SlackNotificationSupport error:' + e);
        }
    } 
}


public class qCallOut implements System.Queueable, Database.AllowsCallouts {
     
    private final String url;
    private final String method;
    private final String body;
     
    public qCallOut(String url, String method, String body) {
        this.url = url;
        this.method = method;
        this.body = body;
    }
     
    public void execute(System.QueueableContext ctx) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod(method);
        req.setBody(body);
        Http http = new Http();
        // to pass when process builder is invoked by another test class
        if(!Test.isRunningTest()){  
          HttpResponse res = http.send(req);
        }
    }
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...