All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.taotao.boot.dingtalk.model.DingerRobot Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.dingtalk.model;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.dingtalk.entity.DingerRequest;
import com.taotao.boot.dingtalk.entity.DingerResponse;
import com.taotao.boot.dingtalk.entity.MsgType;
import com.taotao.boot.dingtalk.enums.DingerResponseCodeEnum;
import com.taotao.boot.dingtalk.enums.DingerType;
import com.taotao.boot.dingtalk.enums.MediaTypeEnum;
import com.taotao.boot.dingtalk.enums.MessageSubType;
import com.taotao.boot.dingtalk.exception.AsyncCallException;
import com.taotao.boot.dingtalk.exception.SendMsgException;
import com.taotao.boot.dingtalk.properties.DingtalkProperties;
import com.taotao.boot.dingtalk.support.CustomMessage;
import com.taotao.boot.dingtalk.support.SignBase;
import com.taotao.boot.dingtalk.utils.DingerUtils;
import org.springframework.beans.BeanUtils;
import java.util.HashMap;
import java.util.Map;
/**
* DingTalk Robot
*
* @author shuigedeng
* @version 2022.07
* @since 2022-07-06 15:23:19
*/
public class DingerRobot extends AbstractDingerSender {
public DingerRobot(DingtalkProperties dingtalkProperties, DingerManagerBuilder dingTalkManagerBuilder) {
super(dingtalkProperties, dingTalkManagerBuilder);
}
@Override
public DingerResponse send(MessageSubType messageSubType, DingerRequest request) {
return send(dingtalkProperties.getDefaultDinger(), messageSubType, request);
}
@Override
public DingerResponse send(DingerType dingerType, MessageSubType messageSubType, DingerRequest request) {
if (!messageSubType.isSupport()) {
return DingerResponse.failed(DingerResponseCodeEnum.MESSAGE_TYPE_UNSUPPORTED);
}
CustomMessage customMessage = customMessage(messageSubType);
String msgContent = customMessage.message(dingtalkProperties.getProjectId(), request);
request.setContent(msgContent);
MsgType msgType = messageSubType.msgType(dingerType, request);
return send(msgType);
}
/**
* @param message 消息内容
* @param T
* @return 响应内容 {@link DingerResponse}
*/
protected DingerResponse send(T message) {
DingerType dingerType = message.getDingerType();
String dkid = dingTalkManagerBuilder.getDingerIdGenerator().dingerId();
Map dingers = dingtalkProperties.getDingers();
if (!(dingtalkProperties.getEnabled() && dingers.containsKey(dingerType))) {
return DingerResponse.failed(dkid, DingerResponseCodeEnum.DINGER_DISABLED);
}
DingerConfig localDinger = getLocalDinger();
// dinger is null? use global configuration and check whether dinger send
boolean dingerConfig = localDinger != null;
try {
DingtalkProperties.Dinger dinger;
if (dingerConfig) {
dinger = new DingtalkProperties.Dinger();
BeanUtils.copyProperties(localDinger, dinger);
dinger.setAsync(localDinger.getAsyncExecute());
dinger.setRobotUrl(dingers.get(dingerType).getRobotUrl());
} else {
dinger = dingers.get(dingerType);
}
StringBuilder webhook = new StringBuilder();
webhook.append(dinger.getRobotUrl()).append("=").append(dinger.getTokenId());
LogUtils.info(
"dingerId={} send message and use dinger={}, tokenId={}.", dkid, dingerType, dinger.getTokenId());
// 处理签名问题(只支持DingTalk)
if (dingerType == DingerType.DINGTALK && DingerUtils.isNotEmpty((dinger.getSecret()))) {
SignBase sign = dingTalkManagerBuilder
.getDingerSignAlgorithm()
.sign(dinger.getSecret().trim());
webhook.append(sign.transfer());
}
Map headers = new HashMap<>();
headers.put("Content-Type", MediaTypeEnum.JSON.type());
// 异步处理, 直接返回标识id
if (dinger.isAsync()) {
dingTalkManagerBuilder.getDingTalkExecutor().execute(() -> {
try {
String result =
dingTalkManagerBuilder.getDingerHttpClient().post(webhook.toString(), headers, message);
dingTalkManagerBuilder.getDingerAsyncCallback().execute(dkid, result);
} catch (Exception e) {
exceptionCallback(dkid, message, new AsyncCallException(e));
}
});
return DingerResponse.success(dkid, dkid);
}
String response = dingTalkManagerBuilder.getDingerHttpClient().post(webhook.toString(), headers, message);
LogUtils.info(response);
return DingerResponse.success(dkid, response);
} catch (Exception e) {
LogUtils.error(e);
exceptionCallback(dkid, message, new SendMsgException(e));
return DingerResponse.failed(dkid, DingerResponseCodeEnum.SEND_MESSAGE_FAILED);
}
}
}