Giter Club home page Giter Club logo

ding-talk-spring-boot's Introduction

ding-talk-spring-boot

基于spring boot 3.x的starter组件,集成了钉钉机器人发送消息通知,支持多机器人,开箱即用

基本配置

1、先把项目 clone 到本地
git clone [email protected]:terminux/ding-talk-spring-boot.git
2、进入到项目根目录下执行(需要JDK17+)
mvn -pl '!ding-talk-spring-boot-samples' -DskipTests clean install
3、给项目添加依赖
<dependency>
    <groupId>com.ugrong.framework</groupId>
    <artifactId>ding-talk-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
4、在项目中配置一下钉钉机器人
com:
  ugrong:
    dingtalk:
      robots:
        default: # 自定义的机器人ID
          access-token: 'your accessToken' # 机器人accessToken
          enable-sign: true # 安全策略是否启用了加签
          secret: 'your secret' # 用于验签的secret,若没有启用加签则不需要设置

开始使用

1、在项目中注入 DingTalkRobotService 服务,例如:
@Resource
private DingTalkRobotService dingTalkRobotService;
2、使用 DingTalkRobotService 中提供的方法进行消息发送
  • 发送文本
dingTalkRobotService.sendText("我就是我,是不一样的烟火");
  • 发送文本且@所有人
dingTalkRobotService.sendText("我就是我,是不一样的烟火", new DingTalkAt().atAll());
  • 发送文本且@指定手机号的用户
dingTalkRobotService.sendText("我就是我,是不一样的烟火", new DingTalkAt().atMobiles("12345678910", "10987654321"));
  • 发送文本且@指定用户id的用户
dingTalkRobotService.sendText("我就是我,是不一样的烟火", new DingTalkAt().atUserIds("userId1", "userId2"));
  • 发送markdown
DingTalkMarkdown markdown = new DingTalkMarkdown("杭州天气", "#### 杭州天气 @150XXXXXXXX \n> 9度,西北风1级,空气良89,相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingalk.com) \n");
dingTalkRobotService.send(markdown);
  • 发送markdown且@所有人
DingTalkMarkdown markdown = new DingTalkMarkdown("杭州天气", "#### 杭州天气 @150XXXXXXXX \n> 9度,西北风1级,空气良89,相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingalk.com) \n");
dingTalkRobotService.send(markdown, new DingTalkAt().atAll());
  • 发送markdown且@指定手机号的用户
DingTalkMarkdown markdown = new DingTalkMarkdown("杭州天气", "#### 杭州天气 @150XXXXXXXX \n> 9度,西北风1级,空气良89,相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingalk.com) \n");
dingTalkRobotService.send(markdown, new DingTalkAt().atMobiles("12345678910", "10987654321"));
  • 发送markdown且@指定用户id的用户
DingTalkMarkdown markdown = new DingTalkMarkdown("杭州天气", "#### 杭州天气 @150XXXXXXXX \n> 9度,西北风1级,空气良89,相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingalk.com) \n");
dingTalkRobotService.send(markdown, new DingTalkAt().atUserIds("userId1", "userId2"));
  • 发送链接
DingTalkLink link = new DingTalkLink();
link.setTitle("这是一个Link消息");
link.setText("这是Link消息");
link.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
link.setMessageUrl("https://open.dingtalk.com/document/");

dingTalkRobotService.send(link);
  • 发送整体跳转的ActionCard
DingTalkSingleActionCard actionCard = new DingTalkSingleActionCard();
actionCard.setTitle("打造一间咖啡厅");
actionCard.setText("![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
actionCard.setSingleTitle("阅读全文");
actionCard.setSingleURL("https://www.dingtalk.com/");

dingTalkRobotService.send(actionCard);
  • 发送独立跳转的ActionCard,底部按钮竖直排列
DingTalkActionCard actionCard = new DingTalkActionCard();
actionCard.setTitle("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
actionCard.setText("![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n\n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");

List<Button> buttons = List.of(new Button("内容不错", "https://www.dingtalk.com/"),
new Button("不感兴趣", "https://www.dingtalk.com/"));
actionCard.setBtns(buttons);
actionCard.setBtnLayout(BtnLayoutEnum.VERTICAL);

dingTalkRobotService.send(actionCard);

若底部按钮需要横向排列,则将 btnLayout 设置为 HORIZONTAL 即可,例如:

actionCard.setBtnLayout(BtnLayoutEnum.HORIZONTAL);
  • 发送FeedCard
Link link1 = new Link();
link1.setTitle("时代的火车向前开1");
link1.setMessageURL("https://www.dingtalk.com/");
link1.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");

Link link2 = new Link();
link2.setTitle("时代的火车向前开2");
link2.setMessageURL("https://www.dingtalk.com/");
link2.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");

DingTalkFeedCard feedCard = new DingTalkFeedCard(List.of(link1, link2));
dingTalkRobotService.send(feedCard);
3、多机器人场景
  • 在配置文件中配置多个机器人,例如:
com:
  ugrong:
    dingtalk:
      robots:
        robot1:
          access-token: 'accessToken1'
          enable-sign: true
          secret: 'secret1'
        robot2:
          access-token: 'accessToken2'
          enable-sign: true
          secret: 'secret2'
// 根据自定义的机器人ID将消息发送给指定机器人且@所有人,适用于多机器人场景

dingTalkRobotService.send("robot2", new DingTalkText("测试文本"), new DingTalkAt().atAll());

注意:若使用之前的方法,它们没有指定机器人ID,所以会使用配置的第一个机器人,即 robot1


相关单元测试可以在 DingTalkRobotTests 类中查看

ding-talk-spring-boot's People

Contributors

terminux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

javanfeng doccker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.