- 實現在Java項目運行中出現異常信息及時推送到釘釘羣,或推送其他通知類信息;
主要工具包:
package pers.niaonao.dingtalkrobot.util;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest ;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
/**
* @className: RobotHelperUtil
* @description: 機器人工具類
* 每個機器人每分鐘最多發送20條
* 限制6 個機器人/羣
* @author: niaonao
* @date: 2019/7/6
**/
@Slf4j
public class RobotHelperUtil {
/**
* 釘釘羣設置 webhook, 支持重置
*/
private static final String ACCESS_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=36ba0cc82d41d3c6aaef7d2c09c9f14de0727069edbc498b5c9d88edb72db227";
/**
* 消息類型
*/
private static final String MSG_TYPE_TEXT = "text";
private static final String MSG_TYPE_LINK = "link";
private static final String MSG_TYPE_MARKDOWN = "markdown";
private static final String MSG_TYPE_ACTION_CARD = "actionCard";
private static final String MSG_TYPE_FEED_CARD = "feedCard";
/**
* 客戶端實例
*/
public static DingTalkClient client = new DefaultDingTalkClient(ACCESS_TOKEN);
/**
* @description: 官方演示示例
* title 是消息列表下透出的標題
* text 是進入羣后看到的消息內容
*
* @author: niaonao
* @date: 2019/7/6
*/
public static void sdkDemoJava() {
DingTalkClient client = RobotHelperUtil.client;
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("測試文本消息");
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("13261303345"));
request.setAt(at);
request.setMsgtype("link");
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setMessageUrl("https://www.dingtalk.com/");
link.setPicUrl("");
link.setTitle("時代的火車向前開");
link.setText("這個即將發佈的新版本,創始人陳航(花名“無招”)稱它爲“紅樹林”。\n" +
"而在此之前,每當面臨重大升級,產品經理們都會取一個應景的代號,這一次,爲什麼是“紅樹林");
request.setLink(link);
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle("杭州天氣");
markdown.setText("#### 杭州天氣 @156xxxx8827\n" +
"> 9度,西北風1級,空氣良89,相對溫度73%\n\n" +
"> ![screenshot](https://gw.alipayobjects.com/zos/skylark-tools/public/files/84111bbeba74743d2771ed4f062d1f25.png)\n" +
"> ###### 10點20分發布 [天氣](http://www.thinkpage.cn/) \n");
request.setMarkdown(markdown);
try {
client.execute(request);
} catch (ApiException e) {
log.error("[ApiException]: 消息發送演示示例, 異常捕獲{}", e.getMessage());
}
}
/**
* @description: 發送普通文本消息
* @param content 文本消息
* @param mobileList 指定@ 聯繫人
* @param isAtAll 是否@ 全部聯繫人
* @return: com.dingtalk.api.response.OapiRobotSendResponse
* @author: niaonao
* @date: 2019/7/6
*/
public static OapiRobotSendResponse sendMessageByText(String content, List<String> mobileList, boolean isAtAll) {
if (StringUtils.isEmpty(content)) {
return null;
}
//參數 參數類型 必須 說明
//msgtype String 是 消息類型,此時固定爲:text
//content String 是 消息內容
//atMobiles Array 否 被@人的手機號(在content裏添加@人的手機號)
//isAtAll bool 否 @所有人時:true,否則爲:false
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent(content);
OapiRobotSendRequest request = new OapiRobotSendRequest();
if (!CollectionUtils.isEmpty(mobileList)) {
// 發送消息並@ 以下手機號聯繫人
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(mobileList);
at.setIsAtAll(isAtAll ? "true" : "false");
request.setAt(at);
}
request.setMsgtype(RobotHelperUtil.MSG_TYPE_TEXT);
request.setText(text);
OapiRobotSendResponse response = new OapiRobotSendResponse();
try {
response = RobotHelperUtil.client.execute(request);
} catch (ApiException e) {
log.error("[發送普通文本消息]: 發送消息失敗, 異常捕獲{}", e.getMessage());
}
return response;
}
/**
* @description: 發送link 類型消息
* @param title 消息標題
* @param text 消息內容
* @param messageUrl 點擊消息後跳轉的url
* @param picUrl 插入圖片的url
* @return: com.dingtalk.api.response.OapiRobotSendResponse
* @author: niaonao
* @date: 2019/7/6
*/
public static OapiRobotSendResponse sendMessageByLink(String title, String text, String messageUrl, String picUrl) {
if (!DataValidUtil.checkNotEmpty(title, text, messageUrl)) {
return null;
}
//參數 參數類型 必須 說明
//msgtype String 是 消息類型,此時固定爲:link
//title String 是 消息標題
//text String 是 消息內容。如果太長只會部分展示
//messageUrl String 是 點擊消息跳轉的URL
//picUrl String 否 圖片URL
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setTitle(title);
link.setText(text);
link.setMessageUrl(messageUrl);
link.setPicUrl(picUrl);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(RobotHelperUtil.MSG_TYPE_LINK);
request.setLink(link);
OapiRobotSendResponse response = new OapiRobotSendResponse();
try {
response = RobotHelperUtil.client.execute(request);
} catch (ApiException e) {
log.error("[發送link 類型消息]: 發送消息失敗, 異常捕獲{}", e.getMessage());
}
return response;
}
/**
* @description: 發送Markdown 編輯格式的消息
* @param title 標題
* @param markdownText 支持markdown 編輯格式的文本信息
* @param mobileList 消息@ 聯繫人
* @param isAtAll 是否@ 全部
* @return: com.dingtalk.api.response.OapiRobotSendResponse
* @author: niaonao
* @date: 2019/7/6
*/
public static OapiRobotSendResponse sendMessageByMarkdown(String title, String markdownText, List<String> mobileList, boolean isAtAll) {
if (!DataValidUtil.checkNotEmpty(title, markdownText)) {
return null;
}
//參數 類型 必選 說明
//msgtype String 是 此消息類型爲固定markdown
//title String 是 首屏會話透出的展示內容
//text String 是 markdown格式的消息
//atMobiles Array 否 被@人的手機號(在text內容裏要有@手機號)
//isAtAll bool 否 @所有人時:true,否則爲:false
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle(title);
markdown.setText(markdownText);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(RobotHelperUtil.MSG_TYPE_MARKDOWN);
request.setMarkdown(markdown);
if (!CollectionUtils.isEmpty(mobileList)) {
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(isAtAll ? "true" : "false");
at.setAtMobiles(mobileList);
request.setAt(at);
}
OapiRobotSendResponse response = new OapiRobotSendResponse();
try {
response = RobotHelperUtil.client.execute(request);
} catch (ApiException e) {
log.error("[發送link 類型消息]: 發送消息失敗, 異常捕獲{}", e.getMessage());
}
return response;
}
/**
* @description: 整體跳轉ActionCard類型的消息發送
* @param title 消息標題, 會話消息會展示標題
* @param markdownText markdown格式的消息
* @param singleTitle 單個按鈕的標題
* @param singleURL 單個按鈕的跳轉鏈接
* @param btnOrientation 是否橫向排列(true 橫向排列, false 縱向排列)
* @param hideAvatar 是否隱藏發消息者頭像(true 隱藏頭像, false 不隱藏)
* @return: com.dingtalk.api.response.OapiRobotSendResponse
* @author: niaonao
* @date: 2019/7/6
*/
public static OapiRobotSendResponse sendMessageByActionCardSingle(String title, String markdownText, String singleTitle, String singleURL, boolean btnOrientation, boolean hideAvatar) {
if (!DataValidUtil.checkNotEmpty(title, markdownText)) {
return null;
}
//參數 類型 必選 說明
// msgtype string true 此消息類型爲固定actionCard
// title string true 首屏會話透出的展示內容
// text string true markdown格式的消息
// singleTitle string true 單個按鈕的方案。(設置此項和singleURL後btns無效)
// singleURL string true 點擊singleTitle按鈕觸發的URL
// btnOrientation string false 0-按鈕豎直排列,1-按鈕橫向排列
// hideAvatar string false 0-正常發消息者頭像,1-隱藏發消息者頭像
OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
actionCard.setTitle(title);
actionCard.setText(markdownText);
actionCard.setSingleTitle(singleTitle);
actionCard.setSingleURL(singleURL);
// 此處默認爲0
actionCard.setBtnOrientation(btnOrientation ? "1" : "0");
// 此處默認爲0
actionCard.setHideAvatar(hideAvatar ? "1" : "0");
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(RobotHelperUtil.MSG_TYPE_ACTION_CARD);
request.setActionCard(actionCard);
OapiRobotSendResponse response = new OapiRobotSendResponse();
try {
response = RobotHelperUtil.client.execute(request);
} catch (ApiException e) {
log.error("[發送ActionCard 類型消息]: 整體跳轉ActionCard類型的發送消息失敗, 異常捕獲{}", e.getMessage());
}
return response;
}
/**
* @description: 獨立跳轉ActionCard類型 消息發送
* @param title 標題
* @param markdownText 文本
* @param btns 按鈕列表
* @param btnOrientation 是否橫向排列(true 橫向排列, false 縱向排列)
* @param hideAvatar 是否隱藏發消息者頭像(true 隱藏頭像, false 不隱藏)
* @return: com.dingtalk.api.response.OapiRobotSendResponse
* @author: niaonao
* @date: 2019/7/6
*/
public static OapiRobotSendResponse sendMessageByActionCardMulti(String title, String markdownText, List<OapiRobotSendRequest.Btns> btns, boolean btnOrientation, boolean hideAvatar) {
if (!DataValidUtil.checkNotEmpty(title, markdownText) || CollectionUtils.isEmpty(btns)) {
return null;
}
//參數 類型 必選 說明
//msgtype string true 此消息類型爲固定actionCard
//title string true 首屏會話透出的展示內容
//text string true markdown格式的消息
//btns array true 按鈕的信息:title-按鈕方案,actionURL-點擊按鈕觸發的URL
//btnOrientation string false 0-按鈕豎直排列,1-按鈕橫向排列
//hideAvatar string false 0-正常發消息者頭像,1-隱藏發消息者頭像
OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
actionCard.setTitle(title);
actionCard.setText(markdownText);
// 此處默認爲0
actionCard.setBtnOrientation(btnOrientation ? "1" : "0");
// 此處默認爲0
actionCard.setHideAvatar(hideAvatar ? "1" : "0");
actionCard.setBtns(btns);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(RobotHelperUtil.MSG_TYPE_ACTION_CARD);
request.setActionCard(actionCard);
OapiRobotSendResponse response = new OapiRobotSendResponse();
try {
response = RobotHelperUtil.client.execute(request);
} catch (ApiException e) {
log.error("[發送ActionCard 類型消息]: 獨立跳轉ActionCard類型發送消息失敗, 異常捕獲{}", e.getMessage());
}
return response;
}
/**
* @description: 發送FeedCard類型消息
* @param links
* @return: com.dingtalk.api.response.OapiRobotSendResponse
* @author: niaonao
* @date: 2019/7/6
*/
public static OapiRobotSendResponse sendMessageByFeedCard(List<OapiRobotSendRequest.Links> links) {
if (CollectionUtils.isEmpty(links)) {
return null;
}
//msgtype string true 此消息類型爲固定feedCard
//title string true 單條信息文本
//messageURL string true 點擊單條信息到跳轉鏈接
//picURL string true 單條信息後面圖片的URL
OapiRobotSendRequest.Feedcard feedcard = new OapiRobotSendRequest.Feedcard();
feedcard.setLinks(links);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(RobotHelperUtil.MSG_TYPE_FEED_CARD);
request.setFeedCard(feedcard);
OapiRobotSendResponse response = new OapiRobotSendResponse();
try {
response = RobotHelperUtil.client.execute(request);
} catch (ApiException e) {
log.error("[發送ActionCard 類型消息]: 獨立跳轉ActionCard類型發送消息失敗, 異常捕獲{}", e.getMessage());
}
return response;
}
/*public static void main(String args[]) {
sdkDemoJava();
}*/
}
- 需要下載釘釘sdk
- ACCESS_TOKEN 生成方法:
- 添加機器人後會自動生成ACCESS_TOKEN
OK,這就完了,可以去測試了,就是這麼簡單。