iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot整合企微webhook机器人发送消息提醒
  • 535
分享到

springboot整合企微webhook机器人发送消息提醒

springbootwebhook发送消息springbootwebhook企微机器人 2022-12-19 12:12:33 535人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、获取企业微信群机器人 WEBhook 地址二、Webhook支持消息类型三、Webhook使用配置1.添加Maven依赖2.配置webhook地址api3.注入Message

一、获取企业微信群机器人 Webhook 地址

业务需要在企业微信推送告警监控或者定时提醒业务,就可以使用企业微信自带的机器人工具Webhook reboot作为消息的发起者!

打开手机端企业微信App,打开一个内部群聊,点击右上角图标进入到群聊设置,来到群机器人页面添加群机器人,设置群机器人昵称点击添加,机器人添加完成后出现的页面,请点击 Webhook 地址后的复制按钮;注意一般只有群主才有对应的权限哦。

二、Webhook支持消息类型

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

三、Webhook使用配置

1.添加maven依赖

    <dependency>
        <groupId>io.GitHub.swalikh</groupId>
        <artifactId>wework-wehook-starter</artifactId>
        <version>1.0.0</version>
    </dependency>
    

2.配置webhook地址api

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 wessage = WeWorkWebhookMessage.buildText("hello");
messageService.send(weWorkWebhookMessage);

2.发送图片(本地或者网络图片均可发送)

// networkImage 和 localImage 均可,格式可支持jpg&png
String networkImageUrl = "https://xxxxx/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://xxxx/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);

四、dynamic-tp动态线程池框架告警集成了webhook机器人

yml配置:

public void send(NotifyPlatfORM platform, String text) {
    String serverUrl = WechatNotifyConst.WECHAT_WEH_HOOK + platform.getUrlKey();
    MarkdownReq markdownReq = new MarkdownReq();
    markdownReq.setMsgtype("markdown");
    MarkdownReq.Markdown markdown = new MarkdownReq.Markdown();
    markdown.setContent(text);
    markdownReq.setMarkdown(markdown);

    try {
        HttpResponse response = HttpRequest.post(serverUrl).body(JSONUtil.tojsonStr(markdownReq)).execute();
        if (Objects.nonNull(response)) {
            log.info("DynamicTp notify, wechat send success, response: {}, request:{}",
                    response.body(), JSONUtil.toJsonStr(markdownReq));
        }
    } catch (Exception e) {
        log.error("DynamicTp notify, wechat send failed...", e);
    }
}

以上就是SpringBoot整合企微webhook机器人发送消息提醒的详细内容,更多关于springboot webhook发送消息的资料请关注编程网其它相关文章!

--结束END--

本文标题: springboot整合企微webhook机器人发送消息提醒

本文链接: https://www.lsjlt.com/news/175230.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作