package top.mhi.provider.usc.controller;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;/**
* @author
*
* @dateTime 2020年3月31日 下午1:56:00
*
* @describe 消息发送频率限制:
* 每个机器人每分钟最多发送20条。
* 消息发送太频繁会严重影响群成员的使用体验,大量发消息的场景 (譬如系统监控报警) 可以将这些信息进行整合,通过markdown消息以摘要的形式发送到群里。
*/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;public class TestController {
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 void main(String[] args)
throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, ApiException { Long timestamp = System.currentTimeMillis();
String secret = "";
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
DingTalkClient client = new DefaultDingTalkClient(
"https://oapi.dingtalk.com/robot/send?access_token=&sign="
+ sign + "×tamp=" + timestamp);
boolean isAtAll=true;
List<String> mobileList = new ArrayList<String>();
mobileList.add("13xxxxxxx51");
//发送文本示例
// String content="你好呀,我是属于你的钉小蜜";
// OapiRobotSendRequest request = sendMessageByText(content, mobileList, isAtAll);
//发送link类型示例
// String title="Hello Mr li";
// String content="你好呀,我是属于你的钉小蜜";
// String messageUrl="http://www.ruiwen.com/wenxue/loushiming/";
// String picUrl="http://pic.ruiwen.com/allimg/copyright/lanmu/lm150.jpg?x-oss-process=style/qr.ruiwen";
// OapiRobotSendRequest request = sendMessageByLink(title,content,messageUrl,picUrl);
//
//markdown类型
// String title="Mr li";
// String content=" 你好呀,我是属于你的钉小蜜";
// OapiRobotSendRequest request = sendMessageByMarkdown(title,content,isAtAll,mobileList);
//整体跳转ActionCard类型
String title="Hello Mr li";
String content="你好呀,我是属于你的钉小蜜";
String btnOrientation="1";
String hideAvatar="0";
String singleTitle="内容不错";
String singleURL="http://pic.ruiwen.com/allimg/copyright/lanmu/lm150.jpg?x-oss-process=style/qr.ruiwen";
OapiRobotSendRequest request = sendMessageByActionCardSingle(title,content,singleTitle,singleURL,btnOrientation,hideAvatar);
OapiRobotSendResponse response = client.execute(request); }
/**
* @description: 发送普通文本消息
* @param content 文本消息
* @param mobileList 指定@ 联系人
* @param isAtAll 是否@ 全部联系人
* @return: com.dingtalk.api.response.OapiRobotSendResponse
*/
public static OapiRobotSendRequest sendMessageByText(String content, List<String> mobileList, boolean isAtAll) {
if (StringUtils.isBlank(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(MSG_TYPE_TEXT);
request.setText(text);
return request;
}; /**
* @description: 发送link 类型消息
* @param title 消息标题
* @param text 消息内容
* @param messageUrl 点击消息后跳转的url
* @param picUrl 插入图片的url
* @return: com.dingtalk.api.response.OapiRobotSendResponse
*/
public static OapiRobotSendRequest sendMessageByLink(String title, String text, String messageUrl, String picUrl) {
if (StringUtils.isBlank(title)) {
return null;
}
if (StringUtils.isBlank(text)) {
return null;
}
if (StringUtils.isBlank(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(MSG_TYPE_LINK);
request.setLink(link);
return request;
} /**
* 发送markdown类型消息
*
* @param title 发送显示标题,
* @param content 发送显示内容,
* @param isAtAll 所有人时:true,否则为:false,
* @param atMobiles 被@人的手机号(在text内容里要有@手机号)
* @contentStyle 内容样式: 标题 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ######
* 六级标题
*
* 引用 > A man who stands for nothing will fall for anything.
*
* 文字加粗、斜体 **bold** *italic*
*
* 链接 [this is a link](http://name.com)
*
* 图片 ![](http://name.com/pic.jpg)
*
* 无序列表 - item1 - item2
*
* 有序列表 1. item1 2. item2
* @return
*
*
*/
public static OapiRobotSendRequest sendMessageByMarkdown(String title, String content, boolean isAtAll,
List<String> atMobiles) {
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(MSG_TYPE_MARKDOWN);
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle(title);
markdown.setText(content);
request.setMarkdown(markdown);
if (!CollectionUtils.isEmpty(atMobiles)) {
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(isAtAll ? "true" : "false");
at.setAtMobiles(atMobiles);
request.setAt(at);
}
return request;
} /**
* @description: 整体跳转ActionCard类型的消息发送
* @param title 消息标题, 会话消息会展示标题
* @param markdownText markdown格式的消息
* @param singleTitle 单个按钮的标题
* @param singleURL 单个按钮的跳转链接
* @param btnOrientation 是否横向排列(1 横向排列, 0 纵向排列)
* @param hideAvatar 是否隐藏发消息者头像(1 隐藏头像, 0 不隐藏)
* @return: com.dingtalk.api.response.OapiRobotSendResponse
*/
public static OapiRobotSendRequest sendMessageByActionCardSingle(String title, String markdownText,
String singleTitle, String singleURL, String btnOrientation, String hideAvatar) {
if (StringUtils.isBlank(title)) {
return null;
}
if (StringUtils.isBlank(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);
actionCard.setBtnOrientation(btnOrientation);
actionCard.setHideAvatar(hideAvatar);
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype(MSG_TYPE_ACTION_CARD);
request.setActionCard(actionCard);
return request;
} /**
* @description: 独立跳转ActionCard类型 消息发送
* @param title 标题
* @param markdownText 文本
* @param btns 按钮列表
* @param btnOrientation 是否横向排列(true 横向排列, false 纵向排列)
* @param hideAvatar 是否隐藏发消息者头像(true 隐藏头像, false 不隐藏)
* @return: com.dingtalk.api.response.OapiRobotSendResponse
*/
public static OapiRobotSendRequest sendMessageByActionCardMulti(String title, String markdownText,
List<OapiRobotSendRequest.Btns> btns, boolean btnOrientation, boolean hideAvatar) {
if (StringUtils.isBlank(title)) {
return null;
}
if (StringUtils.isBlank(markdownText)) {
return null;
}
if (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(MSG_TYPE_ACTION_CARD);
request.setActionCard(actionCard);
return request; }
/**
* @description: 发送FeedCard类型消息
* @param links
* @return: com.dingtalk.api.response.OapiRobotSendResponse
*/
public static OapiRobotSendRequest 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(MSG_TYPE_FEED_CARD);
request.setFeedCard(feedcard);
return request; }
}
以上需要引用jar包:commons-codec 与 taobao-sdk-java-auto_1479188381469
commons-codec 下载地址:https://mvnrepository.com/artifact/commons-codec/commons-codec
taobao-sdk-java-auto_1479188381469下载地址:https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02
参考技术文档 :https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq