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 { private static final Logger log = LoggerFactory.getLogger(DingTalkNotifier.class); private final AtomicReference 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 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; } }