一.获取自定义机器人webhook
参考官网文档一步步申请webhook
配置一个WebHook地址 https://oapi.dingtalk.com/robot/send?access_token=**********
二.引入钉钉jar
服务端API SDK下载 taobao-sdk-java-auto_1479188381469-20191225.jar
在resources下创建lib 导入taobao-sdk-java-auto_1479188381469-20191225.jar包
pom.xml 修改
<dependency>
<groupId>com.dingtalk</groupId>
<artifactId>com.dingtalk</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/taobao-sdk-java-auto_1479188381469-20191225.jar</systemPath>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
三.接口调用
@RequestMapping("/alarm")
@RestController
@Slf4j
public class AlarmController {
private String secret = "密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串";
private String webhook = "完成安全设置后,复制出机器人的Webhook地址";
@RequestMapping(value = "/pushData", method = RequestMethod.POST)
public void alarm(@RequestBody List<AlarmMessageDto> alarmMessageList) {
log.info("alarmMessage:{}", alarmMessageList.toString());
alarmMessageList.forEach(info -> {
try {
Long timestamp = System.currentTimeMillis();
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = "×tamp=" + timestamp + "&sign=" + URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
DingTalkClient client = new DefaultDingTalkClient(webhook + sign);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("业务告警:\n" + info.getAlarmMessage());
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("所有人"));
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
log.info("execute:{}" + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
评论区