微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Salesforce 中的 Apex 类将 Slack 通知发送到多个渠道

如何解决使用 Salesforce 中的 Apex 类将 Slack 通知发送到多个渠道

我有一个允许我向频道发送松弛通知的顶点类。但我希望能够向多个 slack 频道发送 slack 通知

有没有人有答案或能够向我指出实现这一目标的文档? 谢谢

这里是允许向单个通道发送 slack 通知代码

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 += '\nLink 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.\n>>>*'+
                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.\n>>>*'+
                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);
        }
    }
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。