集成钉钉机器人消息通知
集成消息发送呢,还是比较常见的,比如说,异常的时候,发送钉钉通知,艾特到某个人,可以点击查看详情;还可以集成调度,在某个时间段,执行特定的操作,比如每天早上8点发送今日热点新闻,9点提醒你上班打卡啊,11点半提醒你点外卖啊。。。等等等,只要你能想到的固定场景,都可以让机器人帮你执行了。
集成步骤
添加机器人
打开群设置 -> 添加智能群助手 -> 添加机器人 -> 添加自定义机器人
设置安全方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T4NcYGD3-1626853019018)(https://s1.ax1x.com/2020/10/26/BnTOQx.png)]
java sdk 集成
引入maven
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>
<repositories>
<repository>
<id>sonatype-nexus-staging</id>
<name>Sonatype Nexus Staging</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
请求封装
目前只封装了两种,大家可根据实际需求,自行添加
/**
* @author Hou_fx
* @date 2020.10.23 16:26
* @description
*/
public enum RobotType {
/** Robot类型 */
TEXT("text"),
LINK("link"),
;
RobotType(String value){
this.value = value;
}
private String value;
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
/**
* @author Hou_fx
* @date 2020.10.23 16:26
* @description
*/
public class RobotConfig {
public static final String CHARSET_NAME = "UTF-8";
public static final String ALGORITHM = "HmacSHA256";
public static String SECRET = "安全设置里面的秘钥复制过来";
public static String SERVER_URL = "安全设置里面的webhook复制过来";
}
/**
* @author Hou_fx
* @date 2020.10.23 16:25
* @description
*/
public class RobotNotice extends RobotConfig {
public static RobotStruct getInstance(RobotType robotType) {
return new RobotStruct(robotType);
}
public static class RobotStruct {
protected OapiRobotSendRequest request;
private RobotStruct(RobotType robotType) {
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(robotType.getValue());
this.request = request;
}
private DingTalkClient init() throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
Long timestamp = System.currentTimeMillis();
StringBuilder serverUrl = new StringBuilder(SERVER_URL);
serverUrl.append("&").append("timestamp=").append(timestamp);
serverUrl.append("&").append("sign=").append(sign(timestamp));
return new DefaultDingTalkClient(serverUrl.toString());
}
private String sign(Long timestamp) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
String stringToSign = timestamp + "\n" + SECRET;
Mac mac = Mac.getInstance(ALGORITHM);
mac.init(new SecretKeySpec(SECRET.getBytes(CHARSET_NAME), ALGORITHM));
byte[] signData = mac.doFinal(stringToSign.getBytes(CHARSET_NAME));
return URLEncoder.encode(new String(Base64.encodeBase64(signData)), CHARSET_NAME);
}
public RobotStruct setSendContent(String content){
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent(content);
request.setText(text);
return this;
}
public RobotStruct setSendContent(OapiRobotSendRequest.Link link) {
request.setLink(link);
return this;
}
public RobotStruct setAt(List<String> phone) {
if (phone.size() > 0) {
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(phone);
request.setAt(at);
}
return this;
}
public Optional<OapiRobotSendResponse> execute(){
try {
return Optional.ofNullable(this.init().execute(request));
} catch (ApiException | NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return Optional.empty();
}
}
}
好了,下面测试下
/**
* @author Hou_fx
* @date 2020.10.23 19:41
* @description
*/
public class NoticeTest {
public static void main(String[] args) {
RobotNotice.getInstance(RobotType.TEXT).setSendContent("测试文本消息").setAt(Arrays.asList("17788889999")).execute();
}
}
按照上面的步骤来的话,测试应该已经通过了。
优化点
通过看钉钉官方文档,可以看到,每个请求的实例,都需要几个步骤:设置发送类型初始化请求、设置发送内容、设置艾特的人、最后再执行。那么我们可以把上述代码改为工厂模式,根据不同的发送类型,构建不同的实例。
OK,先这样吧,等需求搞完了有时间再优化下(如果我还记得)