Skip to content

Dinger 3.0 统一管理消息体使用示例

Jaemon edited this page May 18, 2021 · 2 revisions

引入Dinger maven依赖

<dependency>
    <groupId>com.github.answerail</groupId>
    <artifactId>dinger-spring-boot-starter</artifactId>
    <version>${dinger.version}</version>
</dependency>

dinger.version版本号取值: Github OR Gitee

最新版: Maven Central

 

启动类引入DingerScan注解

@SpringBootApplication
// 标识Dinger层扫描路径
@DingerScan(basePackages = "com.jaemon.dt.dinger")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

DingerScan的basePackages属性值注意更改为实际项目中定义的dinger接口层包路径

 

定义Dinger消息体

Dinger(叮鸽)有两种方式进行Dinger消息体定义,分别是注解方式XML配置方式,两种方式任选一种使用即可。

方式1-注解方式

yaml配置

spring:
  dinger:
    project-id: ${spring.application.name}
#    default-dinger: dingtalk
    dingers:
      # 使用钉钉机器人, 请根据自己机器人配置信息进行修改
      dingtalk:
        tokenId: 87dbeb7bc28894c3ycyl3d12457228ad309966275b5f427cd85f9025ebb520cf
        secret: AEQ74a9039ai01f2ljm017b90ycye9asg6335f97c658ff37ff371ec8120581c7f09
      # 使用企业微信机器人, 请根据自己机器人配置信息进行修改
#      wetalk:
#        tokenId: 32865206-7082-46l5-8j39-2m7ycy6d868

tokenId取值

  • dingtalk:取自webhook地址中access_token的值。
  • wetalk: 取自webhook地址中key的值。

Dinger接口类

package com.jaemon.dt.dinger;

public interface UserDinger {
    // `@`群里指定成员
    @DingerText(value = "恭喜用户${userName}登录成功!", phones = "13520200906")
    DingerResponse success(String userName);

    @DingerMarkdown(
            value = "#### 用户登录通知\n - 用户Id: ${userId}\n - 用户名: ${userName}",
            title = "用户登录反馈"
    )
    DingerResponse failed(long userId, String userName);

    // `@`群里全部成员
    @DingerText(value = "恭喜用户${loginName}登录成功!", atAll = true)
    DingerResponse login(@Parameter("loginName") String userName);


    // wetalk不支持markdown格式的`@`功能
    @DingerMarkdown(
            value = "#### 注销登录 @13520200906 @13520200908 \n - 用户Id: ${uId}\n - 用户名: ${loginName}",
            title = "用户登录反馈",
            phones = {"13520200906", "13520200908"}
    )
    DingerResponse logout(@Parameter("uId") long userId, @Parameter("loginName") String userName);
}

 

方式2-XML配置

yaml配置

spring:
  dinger:
    project-id: ${spring.application.name}
#    default-dinger: dingtalk
    dingers:
      # 使用钉钉机器人, 请根据自己机器人配置信息进行修改
      dingtalk:
        tokenId: 87dbeb7bc28894c3ycyl3d12457228ad309966275b5f427cd85f9025ebb520cf
        secret: AEQ74a9039ai01f2ljm017b90ycye9asg6335f97c658ff37ff371ec8120581c7f09
	  # 使用企业微信机器人, 请根据自己机器人配置信息进行修改        
#      wetalk:
#        tokenId: 32865206-7082-46l5-8j39-2m7ycy6d868
	# 使用xml配置方式时, 必填        
    dinger-locations: classpath*:dinger/*.xml

tokenId取值

  • dingtalk:取自webhook地址中access_token的值。
  • wetalk: 取自webhook地址中key的值。

Dinger接口类

package com.jaemon.dt.dinger;

public interface UserDinger {
    DingerResponse success(String userName);

    DingerResponse failed(long userId, String userName);

    DingerResponse login(@Parameter("loginName") String userName);

    DingerResponse logout(@Parameter("uId") long userId, @Parameter("loginName") String userName);
}

Dinger消息XML定义

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE dinger SYSTEM "dinger.dtd">
<!DOCTYPE dinger PUBLIC "-//AnswerAIL//DTD Dinger 2.0//EN" "dinger.dtd">

<dinger namespace="com.jaemon.dt.dinger.UserDinger">

    <message id="success" type="TEXT">
        <body>
            <content>
                恭喜用户${userName}登录成功!
            </content>

            <!-- `@`群里指定成员 -->
            <phones>
                <phone value="13520200906"/>
            </phones>
        </body>
    </message>

    <message id="failed" type="MARKDOWN">
        <body>
            <content title="用户登录反馈">
                #### 用户登录通知
                - 用户Id: ${userId}
                - 用户名: ${userName}
            </content>
        </body>
    </message>

    <message id="login" type="TEXT">
        <body>
            <content>
                恭喜用户${loginName}登录成功!
            </content>

            <!-- `@`群里全部成员 -->
            <phones atAll="true" />
        </body>
    </message>
	
    <!-- wetalk不支持markdown格式的`@`功能 -->
    <message id="logout" type="MARKDOWN">
        <body>
            <content title="用户登录反馈">
                #### 注销登录 @13520200906 @13520200908
                - 用户Id: ${uId}
                - 用户名: ${loginName}
            </content>

            <!-- `@`群里指定成员 -->
            <phones>
                <phone value="13520200906"/>
                <phone value="13520200908"/>
            </phones>
        </body>
    </message>
</dinger>

路径位置: resources/dinger/UserDinger.xml

 

使用Dinger

package com.jaemon.dt.controller;
    
@RestController
@Slf4j
public class DemoController {
    @Autowired
    private UserDinger userDinger;

    @GetMapping("/success")
    public Response success() {

        return Response.success(
                userDinger.success("Jaemon")
        );
    }

    @GetMapping("/failed")
    public Response failed() {

        return Response.success(
                userDinger.failed(1, "AnswerAIL")
        );
    }

    @GetMapping("/login")
    public Response login() {

        return Response.success(
                userDinger.login("Jaemon")
        );
    }
    @GetMapping("/logout")
    public Response logout() {

        return Response.success(
                userDinger.logout(1, "AnswerAIL")
        );
    }

}
Clone this wiki locally