Skip to content

Dinger 2.2 统一管理消息体

Jaemon edited this page Mar 2, 2021 · 2 revisions

一、使用XML标签方式统一管理消息体

1. 配置文件配置

1.1 钉钉机器人配置方式

spring:
  dinger:
    project-id: ${spring.application.name}
    dinger-locations: classpath*:dinger/*.xml
    dingers:
      # 使用钉钉机器人, 请根据自己机器人配置信息进行修改
      dingtalk:
        tokenId: 87dbeb7bc28894c3ycyl3d12457228ad309966275b5f427cd85f9025ebb520cf
        secret: AEQ74a9039ai01f2ljm017b90ycye9asg6335f97c658ff37ff371ec8120581c7f09

1.2 企业微信机器人配置方式

spring:
  dinger:
    project-id: ${spring.application.name}
    dinger-locations: classpath*:dinger/*.xml
    dingers:
      # 使用企业微信机器人, 请根据自己机器人配置信息进行修改
      wetalk:
        token-id: 32865206-7082-46l5-8j39-2m7ycy6d868

2. 启动类引入DingerScan注解

@SpringBootApplication
@DingerScan(basePackages = "com.jaemon.demo.dinger")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

3. 定义Dinger层

package com.jaemon.demo.dinger;

public interface UserDinger {
    DingerResponse success(@Parameter("loginName") String userName);

    DingerResponse failed(long userId, String userName);
}

4. UserDinger.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.demo.dinger.UserDinger">

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

            <!-- @所有人 -->
            <phones atAll="true" />
        </body>
    </message>


    <message id="failed" type="MARKDOWN">
        <body>
            <!-- 注意:
                    1. 当发送消息类型为markdown时, title 属性必填
                    2. 如果需要@指定成员,markdown消息内容中必须带@成员, eg: @13520201226
            -->
            <content title="用户登录反馈">
                #### 用户登录通知 @13520201226
                - 用户Id: ${userId}
                - 用户名: ${userName}
            </content>
			
            <phones>
                <phone value="13520201226"/>
            </phones>
        </body>
    </message>
</dinger>

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

5. 使用Dinger

@Component
@Slf4j
public class AppInit implements InitializingBean {
    @Autowired
    private UserDinger userDinger;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public void afterPropertiesSet() throws Exception {
        DingerResponse success = userDinger.success("Jaemon");
        log.info(objectMapper.writeValueAsString(success));

        DingerResponse failed = userDinger.failed(1, "AnswerAIL");
        log.info(objectMapper.writeValueAsString(failed));
    }
}

 

二、使用注解方式统一管理消息体

1. 配置文件配置

1.1 钉钉机器人配置方式

spring:
  dinger:
    project-id: ${spring.application.name}
    dingers:
      # 使用钉钉机器人, 请根据自己机器人配置信息进行修改
      dingtalk:
        tokenId: 87dbeb7bc28894c3ycyl3d12457228ad309966275b5f427cd85f9025ebb520cf
        secret: AEQ74a9039ai01f2ljm017b90ycye9asg6335f97c658ff37ff371ec8120581c7f09

1.2 企业微信机器人配置方式

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

2. 启动类引入DingerScan注解

@SpringBootApplication
@DingerScan(basePackages = "com.jaemon.demo.dinger")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

3. 定义Dinger层

package com.jaemon.demo.dinger;

public interface UserDinger {

    @DingerText(value = "恭喜用户${userName}登录成功!")
    DingerResponse success(String userName);

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

}

4. 使用Dinger

@Component
public class AppInit implements InitializingBean {
    @Autowired
    private UserDinger userDinger;
    @Override
    public void afterPropertiesSet() throws Exception {
        userDinger.success("Jaemon");
        userDinger.failed(1, "Jaemon");
    }
}
Clone this wiki locally