Skip to content

Docs for DingTalk 3.x

Jaemon edited this page Dec 12, 2020 · 8 revisions

DingTalk 3.x使用文档

由于钉钉机器人对发送消息的频率做了限制-每个机器人每分钟最多发送20条,为了解决这种限制可能导致消息发送失败的问题,因此发布3.x版本,对dinger消息体进行配置多机器人信息,并且根据指定切换算法对发送机器人进行切换处理。

 

开发环境说明

  • JDK1.8
  • springboot版本: 2.3.4.RELEASE

 

3.x 新增注解说明

注解 功能描述 适用说明
@EnableMultiDinger 启动多钉钉机器人配置 SpringBoot启动类Application.java中定义使用
@MultiHandler 为Dinger层指定对应的多机器人处理器 XXXDinger.java接口层(接口类上)

说明: 当@EnableMultiDinger配置了全局处理器,则局部处理器默认无效

全局处理器: @EnableMultiDinger(GlobalDingerConfigHandler.class)

局部处理器:@EnableMultiDinger

 

系统默认四种AlgorithmHandler算法

public class CustomAlgorithmHandler implements AlgorithmHandler {
    @Override
    public DingerConfig handler(List<DingerConfig> dingerConfigs, DingerConfig defaultDingerConfig) {
        // 多机器人使用逻辑代码...
        return dingerConfigs.get(1);
    }
}

 


maven 依赖

 <dependency>
     <groupId>com.github.answerail</groupId>
     <artifactId>dingtalk-spring-boot-starter</artifactId>
     <version>3.1.0-RELEASE</version>
</dependency>

 

全局配置多钉钉机器人

yml配置

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;
    }
}

 

Dinger接口层

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");
    }
}

  

 

Dinger级别配置钉钉多机器人

yml配置

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

 

UserDinger对应的多机器人处理器

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;
    }
}

 

Dinger接口层

// 标识对应的处理器
@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");
    }
}

 


补充说明

自定义InitializingBean中使用多钉钉机器人

@Component
@DependsOn(AlgorithmHandler.MULTI_DINGER_PRIORITY_EXECUTE)
public class AppInit implements InitializingBean {

}
  • 此功能在3.1.0-RELEASE及以上版本支持
Clone this wiki locally