yangys
2024-04-05 84dea9976c29ac938fa018b8566c71461b056418
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.qianwen.core.notify.provider.dingtalk;
 
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoRequest;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.log.exception.BizServiceException;
import com.qianwen.core.notify.DefaultNotifyType;
import com.qianwen.core.notify.NotifyType;
import com.qianwen.core.notify.Provider;
import com.qianwen.core.notify.notifier.AbstractNotifier;
import com.qianwen.core.notify.notifier.NotifierProperties;
import com.qianwen.core.notify.template.Template;
import com.qianwen.core.notify.template.TemplateManager;
import com.qianwen.core.tool.api.BizMessage;
import com.qianwen.core.tool.metadata.Values;
import com.qianwen.core.tool.utils.BeanUtil;
import com.qianwen.core.tool.utils.StringUtil;
 
/* loaded from: blade-starter-notify-9.3.0.0-SNAPSHOT.jar:org/springblade/core/notify/provider/dingtalk/DingTalkNotifier.class */
public class DingTalkNotifier extends AbstractNotifier<DingTalkMessageTemplate> {
    private static final Logger log = LoggerFactory.getLogger(DingTalkNotifier.class);
    private final AtomicReference<String> accessToken;
    private final DingTalkClient tokenClient;
    private final DingTalkClient messageClient;
    private long refreshTokenTime;
    private final long tokenTimeOut;
    private static final String tokenApi = "https://oapi.dingtalk.com/gettoken";
    private static final String notify = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
    private final DingTalkProperties properties;
    private final String notifierId;
    private NotifierProperties notifierProperties;
 
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public String getNotifierId() {
        return this.notifierId;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public NotifierProperties getNotifierProperties() {
        return this.notifierProperties;
    }
 
    public DingTalkNotifier(NotifierProperties properties, TemplateManager templateManager) {
        super(templateManager);
        this.accessToken = new AtomicReference<>();
        this.tokenTimeOut = Duration.ofSeconds(7000L).toMillis();
        this.notifierProperties = properties;
        DingTalkProperties dingTalkProperties = (DingTalkProperties) BeanUtil.copy(properties.getConfiguration(), DingTalkProperties.class);
        this.properties = dingTalkProperties;
        this.notifierId = properties.getId();
        this.tokenClient = new DefaultDingTalkClient(tokenApi);
        this.messageClient = new DefaultDingTalkClient(notify);
        this.notifierProperties = properties;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    @Nonnull
    public NotifyType getType() {
        return DefaultNotifyType.dingTalk;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    @Nonnull
    public Provider getProvider() {
        return DingTalkProvider.dingTalkMessage;
    }
 
    @Nonnull
    public void send(@Nonnull DingTalkMessageTemplate template, String traceId, @Nonnull Values context, List<String> notifiedParty) {
        String access_token = getToken();
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(template.getAgentId());
        request.setUseridList(StringUtil.join(notifiedParty));
        request.setToAllUser(Boolean.valueOf(false));
        request.setMsg(template.createMessage(context));
        BizMessage bizMessage = new BizMessage();
        try {
          OapiMessageCorpconversationAsyncsendV2Response response = (OapiMessageCorpconversationAsyncsendV2Response)this.messageClient.execute((TaobaoRequest)request, access_token);
          if (response.isSuccess()) {
            log.info("钉钉通知发送成功");
          } else {
            bizMessage.setMessage(response.getErrmsg());
            bizMessage.setDetail(response.getSubMessage());
            bizMessage.setCode(response.getErrorCode());
          } 
        } catch (ApiException e) {
          bizMessage.setMessage(e.getMessage());
          bizMessage.setDetail(e.getErrMsg());
          bizMessage.setCode(e.getErrCode());
        } 
        if (bizMessage != null) {
          log.info("钉钉通知发送失败:" + bizMessage.getMessage());
          throw new BizServiceException(bizMessage);
        } 
    }
 
    private String getToken() {
        if (System.currentTimeMillis() - this.refreshTokenTime > this.tokenTimeOut || this.accessToken.get() == null) {
            return requestToken();
        }
        return this.accessToken.get();
    }
 
    private String requestToken() {
        BizMessage bizMessage = new BizMessage();
        try {
          OapiGettokenRequest request = new OapiGettokenRequest();
          request.setAppkey(this.properties.getAppKey());
          request.setAppsecret(this.properties.getAppSecret());
          request.setHttpMethod("GET");
          OapiGettokenResponse response = (OapiGettokenResponse)this.tokenClient.execute((TaobaoRequest)request);
          if (response.isSuccess()) {
            log.info("钉钉通知获取token成功");
            return response.getAccessToken();
          }else {
              bizMessage.setMessage(response.getErrmsg());
              bizMessage.setDetail(response.getSubMessage());
              bizMessage.setCode(response.getErrorCode());
              throw new BizServiceException(bizMessage);
          }
        } catch (ApiException e) {
            
          bizMessage.setMessage(e.getMessage());
          bizMessage.setDetail(e.getErrMsg());
          bizMessage.setCode(e.getErrCode());
          log.info("钉钉通知获取token失败:" , e);
          throw new BizServiceException(bizMessage);
        } 
        
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    @Nonnull
    public void close() {
        this.accessToken.set(null);
        this.refreshTokenTime = 0L;
    }
}