[简单便捷]springboot整合企业微信webhook快速发送消息提醒

springboot整合企业微信webhook快速发送消息提醒(wework webhook robot)

a java SDK for wework webhook robot

介绍(introduction)

本人前段时间做后台提醒功能,需要用到企业微信webhook机器人发送各种消息。很多参数拼接搞得焦头烂额。干脆写了一个springboot快速启动包。。配置好webhook地址之后就可以快速方便发送消息,摒弃了各种参数的拼接。
现在已经支持:

  • 文本消息
  • 图片消息
  • 文本卡片消息
  • 图文消息(批量)
  • markdown消息

使用方法(quick start)

1.添加maven依赖(import maven dependency)
        <dependency>
            <groupId>io.github.swalikh</groupId>
            <artifactId>wework-wehook-starter</artifactId>
            <version>1.0.0</version>
        </dependency>
2.配置webhook地址(add webhook api)

可以配置一个或者多个,默认以第一个生效。或者手动修改webhook地址

spring:
  message:
    wechat-webhooks: 
    	- https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx
    	- https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx

3.需要时注入MessageService并且发送消息
        @Autowired
        private MessageService messageService;
  • 1.发送普通文本消息
        WeWorkWebhookMessage weWorkWebhookMessage = WeWorkWebhookMessage.buildText("hello");
        messageService.send(weWorkWebhookMessage);
  • 2.发送图片(本地或者网络图片均可发送)
        // networkImage 和 localImage 均可,格式可支持jpg&png
        String networkImageUrl = "https://img-home.csdnimg.cn/images/20210918100245.png";
        String localImageFilePath = "/home/image/cat.png";
        WeWorkWebhookMessage imageMessage = 
                WeWorkWebhookMessage.buildImageMessage(networkImageUrl);
        messageService.send(imageMessage);

在这里插入图片描述

  • 3.发送图文卡片消息(本地或者网络图片均可发送)
        // networkImage 和 localImage 均可,格式可支持jpg&png
        String networkImageUrl = "https://img-home.csdnimg.cn/images/20210918100245.png";
        Article article = new Article()
                .setTitle("这是卡片的标题")
                .setUrl("http://www.google.com/这是点击的链接地址")
                .setPicurl(networkImageUrl)
                .setDescription("这是描述文字");
        WeWorkWebhookMessage articleMessage =
                WeWorkWebhookMessage.buildNewsMessage(article);
        messageService.send(articleMessage);

在这里插入图片描述

  • 4.发送markdown消息
        MarkdownBuffer markdownBuffer = new MarkdownBuffer();
        markdownBuffer
                .h2("H2").nextLine()
                .h3("H3").nextLine()
                .quote("quote").quoteEnd()
                .green("greenText").nextLine()
                .orange("orangeText").nextLine()
                .gray("grayText").nextLine()
                .code("single line code").nextLine()
                .link("link title","line URL").nextLine();

        WeWorkWebhookMessage markDownMessage =
                WeWorkWebhookMessage.buildMarkDownMessage(markdownBuffer);
        messageService.send(markDownMessage);

在这里插入图片描述

End

I've pushed this project on github. You can checkout the source code if you want~
github项目地址 https://github.com/swalikh/wework-wehook-starter

2024-01-19
近期会迭代出2.0版本,支持多个webhook的配置,以及各个优化

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot是一个用于简化Spring应用程序开发的框架,它提供了快速启动和开发的能力。企业微信机器人是企业微信中的一个功能,它可以向指定的群聊或个人发送消息,用于自动化通知和交互。 要使用Spring Boot来实现企业微信机器人消息,首先需要在企业微信后台获取到机器人的Webhook地址,并在Spring Boot应用程序中编写发送消息的逻辑。可以使用Spring Boot的RestTemplate来发送HTTP请求将消息发送企业微信机器人的Webhook地址,同时也可以通过实现定时任务或事件监听来自动化消息发送。 在Spring Boot应用程序中可以编写一个Controller来接收需要发送消息内容,然后调用RestTemplate发送HTTP POST请求将消息内容发送企业微信机器人。另外,还可以编写定时任务或事件监听,当满足一定条件时自动发送消息给指定的群聊或个人,实现自动化通知功能。 除了发送文本消息外,还可以通过企业微信机器人的Webhook接口发送图文消息、链接消息、Markdown消息等不同类型的消息。在Spring Boot应用程序中可以根据需求来封装不同类型消息发送逻辑,以满足企业内部不同场景下的消息通知需求。 总的来说,通过Spring Boot实现企业微信机器人消息功能可以简化开发流程,提高开发效率,同时也可以满足企业内部自动化消息发送的需求,为企业内部的协作和通知提供便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值