springboot整合企微webhook機器人發(fā)送消息提醒
一、獲取企業(yè)微信群機器人 Webhook 地址
業(yè)務需要在企業(yè)微信推送告警監(jiān)控或者定時提醒業(yè)務,就可以使用企業(yè)微信自帶的機器人工具Webhook reboot作為消息的發(fā)起者!
打開手機端企業(yè)微信App,打開一個內(nèi)部群聊,點擊右上角圖標進入到群聊設置,來到群機器人頁面添加群機器人,設置群機器人昵稱點擊添加,機器人添加完成后出現(xiàn)的頁面,請點擊 Webhook 地址后的復制按鈕;注意一般只有群主才有對應的權(quán)限哦。
二、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并且發(fā)送消息
@Autowired private MessageService messageService;
1.發(fā)送普通文本消息
WeWorkWebhookMessage wessage = WeWorkWebhookMessage.buildText("hello"); messageService.send(weWorkWebhookMessage);
2.發(fā)送圖片(本地或者網(wǎng)絡圖片均可發(fā)送)
// 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.發(fā)送圖文卡片消息(本地或者網(wǎng)絡圖片均可發(fā)送)
// 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.發(fā)送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動態(tài)線程池框架告警集成了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機器人發(fā)送消息提醒的詳細內(nèi)容,更多關于springboot webhook發(fā)送消息的資料請關注腳本之家其它相關文章!
相關文章
Clojure?與Java對比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個單獨類的優(yōu)點
這篇文章主要介紹了Clojure?與Java對比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個單獨類的優(yōu)點,在Clojure中,我們一次又一次地使用相同的數(shù)據(jù)結(jié)構(gòu),并在其上運行許多函,更多相關介紹需要的朋友可以參考一下下面文章內(nèi)容2022-06-06