当前位置: 首页 > news >正文

SpringBoot整合钉钉实现消息推送

前言

钉钉作为一款企业级通讯工具,具有广泛的应用场景,包括但不限于团队协作、任务提醒、工作汇报等。

通过Spring Boot应用程序整合钉钉实现消息推送,我们可以实现以下功能:

  • 实时向指定用户或群组发送消息通知。
  • 自定义消息内容和格式,满足不同业务需求。
  • 监控和提醒功能的实现,提高工作效率和沟通效果。

在接下来的部分,我们将逐步介绍如何在Spring Boot中配置和调用钉钉消息推送API,以实现消息推送功能。

钉钉群安装机器人

电脑端登录钉钉,选择一个群聊进行如下的操作:

点击群设置

群设置——>点击机器人

机器人管理——>添加机器人

添加机器人

选择自定义

点击添加

添加机器人设置

注:复制生成的签名串,后续将会用到!

复制出来的结果如下:
SEC1cc02e7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

勾选协议,点击完成即可!

接下来节将会出现如下配置:

image

注:复制Webhook地址,后续将使用该地址向钉钉群推送消息!

地址如下:

https://oapi.dingtalk.com/robot/send?access_token=89742c23bdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

到此位置,经过以上步骤,钉钉群安装机器人完成了。

添加机器人完成

集成钉钉消息推送功能

引入依赖

在项目pom.xml文件中引入依赖,当前版本1.0.3.RELEASE

        <dependency><groupId>cn.snowheart</groupId><artifactId>spring-boot-dingtalk-robot-starter</artifactId><version>1.0.3.RELEASE</version></dependency>

配置WebHook地址

在application.yml配置文件中配置钉钉机器人的WebHook地址:

dingtalk:robot:prefix: https://oapi.dingtalk.com/robot/sendaccess-token: 89742c23bdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsecret:secret-enabled: truesecret-token: SEC1cc02e7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

功能测试

@RestController
@RequestMapping
@Slf4j
public class DingTalkController {@Autowiredprivate DingTalkRobotClient dingTalkRobotClient;/*** 测试发送文本消息*/@GetMapping("/sendMsg")public void sendMessage() {DingTalkResponse response = dingTalkRobotClient.sendMessage(new TextMessage("公众号:小小开发者!"));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());}}

启动项目,访问:http://127.0.0.1:8080/sendMsg 即可发送!

消息发送

其他相关测试

普通文本通知

  /*** 普通文本通知*/@GetMapping("/sendTextMessage")public void sendTextMessage() {DingTalkResponse response = dingTalkRobotClient.sendTextMessage(new TextMessage("业务处理异常:构建TextMessage对象发布!"));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendTextMessage("业务处理异常:构建普通字符串发布!");Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendTextMessage("业务处理异常:通知指定人!", new String[]{"17767145153"});Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendTextMessage("业务处理异常:通知群内所有人!", true);Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);}

访问:http://127.0.0.1:8080/sendTextMessage

普通文本通知

超链接文本

    /*** 超链接文本*/@GetMapping("/sendLinkMessage")public void sendLinkMessage() {DingTalkResponse response = dingTalkRobotClient.sendLinkMessage(new LinkMessage("业务处理异常:AAAAAA","BBBBBB","CCCCCC","DDDDDD"));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);
}

访问:http://127.0.0.1:8080/sendLinkMessage

超链接文本

Markdown文本推送

 private static final String markDownDemoText = "业务报警:标题" +"# 一级标题\n" +"## 二级标题\n" +"### 三级标题\n" +"#### 四级标题\n" +"##### 五级标题\n" +"###### 六级标题\n";/*** markdown文本推送*/@GetMapping("/sendMarkdownMessage")public void sendMarkdownMessage() {// 构建 markdown 对象用法DingTalkResponse response = dingTalkRobotClient.sendMarkdownMessage(new MarkdownMessage("业务处理异常:钉钉markdown消息支持的语法",markDownDemoText));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);// 不构建对象response = dingTalkRobotClient.sendMarkdownMessage("业务处理异常:钉钉markdown消息支持的语法",markDownDemoText);Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);// 发送给指定人response = dingTalkRobotClient.sendMarkdownMessage("业务处理异常:钉钉markdown消息支持的语法",markDownDemoText, new String[]{"19087690186"});Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);//发送给全体人response = dingTalkRobotClient.sendMarkdownMessage("业务处理异常:钉钉markdown消息支持的语法",markDownDemoText, true);Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);}

访问:http://127.0.0.1:8080/sendMarkdownMessage

markdown文本推送

ActionCard 业务推送

 /*** ActionCard业务推送*/@GetMapping("/sendActionCardMessage")public void sendActionCardMessage() {DingTalkResponse response = dingTalkRobotClient.sendActionCardMessage(new ActionCardMessage("业务报警:This is title", "![screenshot](@lADOpwk3K80C0M0FoA)\n" +"**Apple Store** 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划"));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendActionCardMessage("业务报警:This is title", "![screenshot](@lADOpwk3K80C0M0FoA)\n" +"**Apple Store** 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendActionCardMessage("业务报警:This is title", "![screenshot](@lADOpwk3K80C0M0FoA)\n" +"**Apple Store** 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",HideAvatarType.HIDE);Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendActionCardMessage("业务报警:This is title", "![screenshot](@lADOpwk3K80C0M0FoA)\n" +"**Apple Store** 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",ActionCardButton.defaultReadButton("https://www.dingtalk.com"));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendActionCardMessage("业务报警:This is title", "![screenshot](@lADOpwk3K80C0M0FoA)\n" +"**Apple Store** 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",HideAvatarType.HIDE,ActionCardButton.defaultReadButton("https://www.dingtalk.com"));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);}

访问:http://127.0.0.1:8080/sendActionCardMessage

ActionCard 业务推送

FeedCard 消息推送

    /*** FeedCard 消息推送* @throws InterruptedException*/@GetMapping("/sendFeedCardMessage")public void sendFeedCardMessage()  {ArrayList<FeedCardMessageItem> items = new ArrayList<>();items.add(new FeedCardMessageItem("业务处理异常:成为架构师的路上,看这一篇文章就足够了,因为……","http://mp.weixin.qq.com/s/CPUaB60pue0Zf3fUL9xqvw","https://mmbiz.qpic.cn/mmbiz_jpg/YriaiaJPb26VMtfgPvTsM9amH5hf3pmTbf40ia6OLE845icrDb0vt4AsMnTyva5mMMpwwxnkVR5UjCEI8ADvSic1qWQ/640"));items.add(new FeedCardMessageItem("业务处理异常:想成为一名Web开发者?你应该学习Node.js而不是PHP","http://mp.weixin.qq.com/s/x8dm9e7gwLXSEzxE6sQYow","https://mmbiz.qpic.cn/mmbiz_jpg/YriaiaJPb26VND0Q0hSBOoyVkr9cXQrFjWI7hOzax1IxIibqanXYD4W8nyeYX5iaicjgiaqia7ly94iawOsGwehbKGwGsA/640"));DingTalkResponse  response = dingTalkRobotClient.sendFeedCardMessage(new FeedCardMessage(items));Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);response = dingTalkRobotClient.sendFeedCardMessage(items);Assert.assertEquals(response.getErrcode().longValue(), 0L);log.info(response.toString());ThreadUtil.sleep(3000);}

访问:http://127.0.0.1:8080/sendFeedCardMessage

FeedCard 消息推送

总结

通过本文的介绍,我们深入了解了如何使用Spring Boot应用集成钉钉实现消息的推送,实现了实时消息通知和提醒功能;在实际应用中,钉钉消息推送功能可以帮助企业提高内部沟通效率,及时传达重要信息和通知,提升团队协作和工作效率。

希望本文对您了解和应用钉钉消息推送功能有所帮助,如果你觉得本文对你有帮助,请点赞分享,让更多人受益!

源码获取

本文代码获取方式:
后台回复【消息推送】即可获取!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 河海大学上岸经验分享!
  • 数据中心网络架构设计与优化
  • WPF 简单页面切换示例
  • SAP_ABAP_消息类型
  • 【Pytorch】一文向您详细介绍 torch.nn.DataParallel() 的作用和用法
  • Web前端高亮:深度解析高亮技术的四个方面、五个方面、六个方面与七个方面
  • 软考 系统架构设计师系列知识点之杂项集萃(33)
  • vue2中封装图片上传获取方法类(针对后端返回的数据不是图片链接,只是图片编号)
  • iCloud完全指南:释放Apple云服务的终极潜力
  • 实验八、地址解析协议《计算机网络》
  • Java学习 - MyBatis - 初识MyBatis
  • MySQL数据库---LIMIT、EXPLAIN详解
  • jmeter并发测试
  • C++中实现一个泄漏检测工具
  • 软考初级网络管理员_01_计算机系统基础知识(硬件)单选题
  • 网络传输文件的问题
  • golang中接口赋值与方法集
  • gops —— Go 程序诊断分析工具
  • Js基础——数据类型之Null和Undefined
  • Mocha测试初探
  • MySQL QA
  • Selenium实战教程系列(二)---元素定位
  • Spring Cloud中负载均衡器概览
  • SQLServer之索引简介
  • vue.js框架原理浅析
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 高度不固定时垂直居中
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 如何设计一个比特币钱包服务
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 入门级的git使用指北
  • 优秀架构师必须掌握的架构思维
  • 智能合约开发环境搭建及Hello World合约
  • 大数据全解:定义、价值及挑战
  • # dbt source dbt source freshness命令详解
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #if #elif #endif
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (003)SlickEdit Unity的补全
  • (3)nginx 配置(nginx.conf)
  • (52)只出现一次的数字III
  • (bean配置类的注解开发)学习Spring的第十三天
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (二十三)Flask之高频面试点
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (四)JPA - JQPL 实现增删改查
  • .NET CF命令行调试器MDbg入门(一)
  • .NET 表达式计算:Expression Evaluator
  • .NET4.0并行计算技术基础(1)
  • .NET连接数据库方式
  • .Net面试题4
  • .NET应用UI框架DevExpress XAF v24.1 - 可用性进一步增强
  • //解决validator验证插件多个name相同只验证第一的问题
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48
  • @PostConstruct 注解的方法用于资源的初始化