依赖
<!-- https://mvnrepository.com/artifact/com.aliyun/alibaba-dingtalk-service-sdk -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
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 org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Component;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Component
public class SendToDingTalkUtils {
public static final String url = "机器人的Webhook";
public static final String SECRET = "加签SECRET";
//获取DingTalkClient对象
public DingTalkClient getDingTalkClient() throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
Long timestamp = System.currentTimeMillis();
String secret = SECRET;
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
//sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
return new DefaultDingTalkClient(url + "&sign=" + sign + "×tamp=" + timestamp);
}
//组装参数
public String getContext(){
return "当前公司购买服务即将到期";
}
//推送钉钉消息
//@Scheduled(cron = "0/10 * * * * ?")
public void pushToDIngTakl() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, ApiException {
System.out.println("执行定时任务");
OapiRobotSendRequest req = new OapiRobotSendRequest();
//定义文本内容
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent(getContext());
//定义 @ 对象
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(true);
//at.setAtUserIds(Arrays.asList(USER_ID));
//设置消息类型
req.setMsgtype("text");
req.setText(text);
//req.setAt(at);
OapiRobotSendResponse rsp = getDingTalkClient().execute(req);
}
}
创建定时任务,每隔五秒执行一次
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@EnableScheduling
@Component
public class Task {
@Autowired
private SendToDingTalkUtils sendToDingTalkUtils;
@Scheduled(cron = "0/5 * * * * ?")
public void task() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, ApiException {
sendToDingTalkUtils.pushToDIngTakl();
}
}