|
|
|
@ -6,6 +6,8 @@ import com.dingtalk.api.request.OapiRobotSendRequest;
|
|
|
|
|
import com.dingtalk.api.response.OapiRobotSendResponse;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
@ -14,20 +16,26 @@ import java.net.URLEncoder;
|
|
|
|
|
/**
|
|
|
|
|
* 钉钉推送工具类
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class DingTalkPushUtil {
|
|
|
|
|
|
|
|
|
|
public static final String CUSTOM_ROBOT_TOKEN = "e65e730cba22e320e16926fd4ff19ce787fa2162d065792bb6562c6d4a4cf328";
|
|
|
|
|
public static final String SECRET = "SEC72e5fb1b4ce7f9fed55386040d599035c50f8d2a181ad66bd1277549f0716124";
|
|
|
|
|
public static final String KEY_WORD = "运维告警: ";
|
|
|
|
|
@Value("${cac.dingding.send}")
|
|
|
|
|
public boolean send;
|
|
|
|
|
@Value("${cac.dingding.token}")
|
|
|
|
|
public String token;
|
|
|
|
|
@Value("${cac.dingding.secret}")
|
|
|
|
|
public String secret;
|
|
|
|
|
public String KEY_WORD = "运维告警: ";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void pushText(String content) {
|
|
|
|
|
public void pushText(String content) {
|
|
|
|
|
if (send) {
|
|
|
|
|
try {
|
|
|
|
|
Long timestamp = System.currentTimeMillis();
|
|
|
|
|
String stringToSign = timestamp + "\n" + SECRET;
|
|
|
|
|
String stringToSign = timestamp + "\n" + secret;
|
|
|
|
|
Mac mac = Mac.getInstance("HmacSHA256");
|
|
|
|
|
mac.init(new SecretKeySpec(SECRET.getBytes("UTF-8"), "HmacSHA256"));
|
|
|
|
|
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
|
|
|
|
|
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
|
|
|
|
|
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
|
|
|
|
|
|
|
|
|
@ -43,11 +51,12 @@ public class DingTalkPushUtil {
|
|
|
|
|
//设置消息类型
|
|
|
|
|
req.setMsgtype("text");
|
|
|
|
|
req.setText(text);
|
|
|
|
|
OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
|
|
|
|
|
OapiRobotSendResponse rsp = client.execute(req, token);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("钉钉发送失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|