import com.alibaba.fastjson2.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
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;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Date;
/**
* @ClassName DingDingUtils
* @Descripition
* @Author
* @Date 2024/9/20 18:00
*/
@Slf4j
@Component
public class DingDingUtils {
/**
*
* 推送钉钉消息
*
* @param content
*/
public static void sendDingTalkMsg(String content) {
try {
// 钉钉发送消息路径
String sendMessageUrl = "https://oapi.dingtalk.com/robot/send?access_token=自定义token";
// 秘钥
String secret = "自定义secret";
//获取系统时间戳
long timestamp = Instant.now().toEpochMilli();
//拼接
String stringToSign = timestamp + "\n" + secret;
//使用HmacSHA256算法计算签名
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
//进行Base64 encode 得到最后的sign,可以拼接进url里
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
//钉钉机器人地址(配置机器人的webhook)
String dingUrl = sendMessageUrl + "×tamp=" + timestamp + "&sign=" + sign;
DingTalkClient client = new DefaultDingTalkClient(dingUrl);
OapiRobotSendRequest sendRequest = new OapiRobotSendRequest();
// 钉钉自定义机器人支持的消息类型包括text类型、link类型、markdown类型、actioncard类型和feedcard类型
sendRequest.setMsgtype("markdown");
StringBuilder sb = new StringBuilder();
// 标题:通过#的数量来表示不同级别的标题,例如# 标题1、## 标题2等。
sb.append("#### 【自定义标题】: ").append("****").append("\n\n");
// 引用块:使用>符号开头,例如> 这是引用的文本块`。
sb.append("> ##### 告警提示: ").append("【自定义信息文本】").append("\n\n");
sb.append("> ##### 时间: ").append(DateUtil.parseDateToStr(new Date(), "yyyy-MM-dd HH:mm:ss")).append("\n\n");
sb.append("> ##### 内容: ").append(content);
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setText(sb.toString());
markdown.setTitle("告警");
sendRequest.setMarkdown(markdown);
OapiRobotSendResponse response = client.execute(sendRequest);
log.info("DingDingUtils.sendDingTalkMsg [钉钉消息发送结果] response: [{}]", JSON.toJSONString(response));
if (!response.isSuccess()) {
log.error("DingDingUtils.sendDingTalkMsg [钉钉消息发送失败!] response: [{}]", JSON.toJSONString(response));
}
} catch (NoSuchAlgorithmException e) {
log.error("DingDingUtils.sendDingTalkMsg [NoSuchAlgorithmException] getMessage : {}", e.getMessage(), e);
} catch (InvalidKeyException e) {
log.error("DingDingUtils.sendDingTalkMsg [InvalidKeyException] getMessage : {}", e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
log.error("DingDingUtils.sendDingTalkMsg [UnsupportedEncodingException] getMessage : {}", e.getMessage(), e);
} catch (ApiException e) {
log.error("DingDingUtils.sendDingTalkMsg [ApiException] getMessage : {}", e.getMessage(), e);
}
}
public static void main(String[] args) {
sendDingTalkMsg("测试发消息");
}
}
备注:钉钉发送消息路径 和 秘钥
-
创建机器人
示例如图: