-
Notifications
You must be signed in to change notification settings - Fork 77
Docs for DingTalk 3.x
Jaemon edited this page Dec 12, 2020
·
8 revisions
由于钉钉机器人对发送消息的频率做了限制-每个机器人每分钟最多发送20条,为了解决这种限制可能导致消息发送失败的问题,因此发布3.x版本,对dinger消息体进行配置多机器人信息,并且根据指定切换算法对发送机器人进行切换处理。
- JDK1.8
- springboot版本: 2.3.4.RELEASE
注解 | 功能描述 | 适用说明 |
---|---|---|
@EnableMultiDinger | 启动多钉钉机器人配置 | SpringBoot启动类Application.java中定义使用 |
@MultiHandler | 为Dinger层指定对应的多机器人处理器 | XXXDinger.java接口层(接口类上) |
说明: 当@EnableMultiDinger配置了全局处理器,则局部处理器默认无效
全局处理器: @EnableMultiDinger(GlobalDingerConfigHandler.class)
局部处理器:@EnableMultiDinger
-
DefaultHandler: 返回默认使用的机器人账号
-
DingerHandler: 钉钉机器人发送消息频率限制算法-每个机器人每分钟最多发送20条
- 每分钟发送次数可通过启动参数进行更改
-Dmulti.dinger.minute.limit.count=10
- eg:
java -Dmulti.dinger.minute.limit.count=10 -jar service.jar
- 每分钟发送次数可通过启动参数进行更改
-
RandomHandler: 随机选择算法
-
RoundRobinHandler: 轮询算法
-
自定义算法
public class CustomAlgorithmHandler implements AlgorithmHandler {
@Override
public DingerConfig handler(List<DingerConfig> dingerConfigs, DingerConfig defaultDingerConfig) {
// 多机器人使用逻辑代码...
return dingerConfigs.get(1);
}
}
<dependency>
<groupId>com.github.answerail</groupId>
<artifactId>dingtalk-spring-boot-starter</artifactId>
<version>3.1.0-RELEASE</version>
</dependency>
spring:
dingtalk:
token-id: c60d4824e0ba4a30544e81212256789331d68b0085ed1a5b2279715741355fbc
secret: APC3eb471b2761851d6ddd1abcndf2d97be21447d8818f1231c5ed61234as52d1w0
project-id: ${spring.application.name}
配置默认机器人信息
@SpringBootApplication
// 标识Dinger层扫描路径
@DingerScan(basePackages = "com.jaemon.dt.dinger")
// 启动多钉钉机器人并配置全局处理器
@EnableMultiDinger(GlobalDingerConfigHandler.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动全局多钉钉机器人:
@EnableMultiDinger(GlobalDingerConfigHandler.class)
public class GlobalDingerConfigHandler implements DingerConfigHandler {
@Override
public List<DingerConfig> dingerConfigs() {
ArrayList<DingerConfig> dingerConfigs = Lists.newArrayList();
dingerConfigs.add(new DingerConfig("tokenId1", "secret1"));
dingerConfigs.add(new DingerConfig("tokenId2", "secret2"));
// ...
return dingerConfigs;
}
@Override
public Class<? extends AlgorithmHandler> algorithmHandler() {
// 采用轮询算法
return RoundRobinHandler.class;
}
}
public interface UserDinger {
@DingerText(value = "恭喜用户${userName}注册成功!", phones = {"13520200906"})
DingTalkResult userRegister(String userName);
@DingerMarkdown(
value = "#### 用户注销通知 @13520200906\n - 用户Id: ${userId}\n - 用户名: ${userName}",
title = "用户注销反馈",
phones = {"13520200906"}
)
DingTalkResult userLogout(@Parameter("userId") Long id, String userName);
}
@Component
public class AppInit implements InitializingBean {
@Autowired
private UserDinger userDinger;
@Override
public void afterPropertiesSet() throws Exception {
userDinger.userRegister("AnswerAIL");
userDinger.userLogout(1L, "Jaemon");
}
}
spring:
dingtalk:
token-id: c60d4824e0ba4a30544e81212256789331d68b0085ed1a5b2279715741355fbc
secret: APC3eb471b2761851d6ddd1abcndf2d97be21447d8818f1231c5ed61234as52d1w0
project-id: ${spring.application.name}
配置默认机器人信息
@SpringBootApplication
// 标识Dinger层扫描路径
@DingerScan(basePackages = "com.jaemon.dt.dinger")
// 启动多钉钉机器人
@EnableMultiDinger
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动多钉钉机器人功能:
@EnableMultiDinger
public class UserDingerConfigHandler implements DingerConfigHandler {
@Override
public List<DingerConfig> dingerConfigs() {
ArrayList<DingerConfig> dingerConfigs = Lists.newArrayList();
dingerConfigs.add(new DingerConfig("tokenId1", "secret1"));
dingerConfigs.add(new DingerConfig("tokenId2", "secret2"));
dingerConfigs.add(new DingerConfig("tokenId3", "secret3"));
return dingerConfigs;
}
@Override
public Class<? extends AlgorithmHandler> algorithmHandler() {
// 采用钉钉发送频率限制算法
return DingerHandler.class;
}
}
// 标识对应的处理器
@MultiHandler(UserDingerConfigHandler.class)
public interface UserDinger {
@DingerText(value = "恭喜用户${userName}注册成功!", phones = {"13520200906"})
DingTalkResult userRegister(String userName);
@DingerMarkdown(
value = "#### 用户注销通知 @13520200906\n - 用户Id: ${userId}\n - 用户名: ${userName}",
title = "用户注销反馈",
phones = {"13520200906"}
)
DingTalkResult userLogout(@Parameter("userId") Long id, String userName);
}
指定多钉钉机器人处理器:
@MultiHandler(UserDingerConfigHandler.class)
@Component
public class AppInit implements InitializingBean {
@Autowired
private UserDinger userDinger;
@Override
public void afterPropertiesSet() throws Exception {
userDinger.userRegister("AnswerAIL");
userDinger.userLogout(1L, "Jaemon");
}
}
@Component
@DependsOn(AlgorithmHandler.MULTI_DINGER_PRIORITY_EXECUTE)
public class AppInit implements InitializingBean {
}
- 此功能在3.1.0-RELEASE及以上版本支持