com.squareup.okhttp3
okhttp
3.4.1
com.alibaba
fastjson
1.2.32
provided
钉钉公开API
https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx
public class DingdingUtils {
private static OkHttpClient client = new OkHttpClient();
/**
* 发送钉钉消息
* @param jsonString 消息内容
* @param webhook 钉钉自定义机器人webhook
* @return
*/
public static boolean sendToDingding(String jsonString, String webhook) {
try{
String type = "application/json; charset=utf-8";
RequestBody body = RequestBody.create(MediaType.parse(type), jsonString);
Request.Builder builder = new Request.Builder().url(webhook);
builder.addHeader("Content-Type", type).post(body);
Request request = builder.build();
Response response = client.newCall(request).execute();
String string = response.body().string();
System.out.println(String.format("send ding message:%s", string));
JSONObject res = JSONObject.parseObject(string);
return res.getIntValue("errcode") == 0;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
}
@RestController
@RequestMapping(value = "/dingding")
public class DingdingController {
// 发送钉钉消息
// example
//{
// "content": "content @xxx",
// "atMobiles": ["xxx"],
// "isAtAll": false
//}
@PostMapping(value = "/sendText")
public boolean sendTextMessage(@RequestBody TextEntity text) {
return DingdingUtils.sendToDingding(text.getJSONObjectString(), "https://oapi.dingtalk.com/robot/send?access_token=xxx");
}
@PostMapping(value = "/sendLink")
// example
//{
// "title": "时代的火车向前开",
// "content": "这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?",
// "picUrl": "https://avatar.csdn.net/5/F/0/3_zww1984774346.jpg",
// "messageUrl": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
//}
public boolean sendLinkMessage(@RequestBody LinkEntity linkEntity) {
return DingdingUtils.sendToDingding(linkEntity.getJSONObjectString(), "https://oapi.dingtalk.com/robot/send?access_token=xxx");
}
@PostMapping(value = "/sendMarkdown")
//{
// "title":"杭州天气",
// "content": "#### 杭州天气 @xxx\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",
// "atMobiles": ["xx"],
// "isAtAll": false
//}
public boolean sendMarkdownMessage(@RequestBody MarkdownEntity markdownEntity) {
return DingdingUtils.sendToDingding(markdownEntity.getJSONObjectString(), "https://oapi.dingtalk.com/robot/send?access_token=xxx");
}
@PostMapping(value = "/sendActionCard")
// example
//{
// "title": "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
// "content": "![screenshot](https://gw.alipayobjects.com/zos/skylark-tools/public/files/84111bbeba74743d2771ed4f062d1f25.png)\n ### 乔布斯 20 年前想打造的苹果咖啡厅\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
// "hideAvatar": "0",
// "btnOrientation": "0",
// "singleTitle" : "阅读全文",
// "singleURL" : "https://www.dingtalk.com/"
//}
public boolean sendActionCardMessage(@RequestBody ActionCardEntity actionCardEntity) {
return DingdingUtils.sendToDingding(actionCardEntity.getJSONObjectString(), "https://oapi.dingtalk.com/robot/send?access_token=xxx");
}
@PostMapping(value = "/sendFeedCard")
// example
//{
// "links": [
// {
// "title": "时代的火车向前开",
// "messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
// "picURL": "https://img-blog.csdnimg.cn/20181129152228878.png?imageView2/5/w/120/h/120"
// },
// {
// "title": "时代的火车向前开2",
// "messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
// "picURL": "https://img-blog.csdnimg.cn/20181129152228878.png?imageView2/5/w/120/h/120"
// }
// ]
// }
public boolean sendFeedCardMessage(@RequestBody FeedCardEntity feedCardEntity) {
return DingdingUtils.sendToDingding(feedCardEntity.getJSONObjectString(), "https://oapi.dingtalk.com/robot/send?access_token=xxx");
}
}
参数格式如下
{
"msgtype": "text",
"text": {
"content": "我就是我, 是不一样的烟火@156xxxx8827"
},
"at": {
"atMobiles": [
"156xxxx8827",
"189xxxx8325"
],
"isAtAll": false
}
}
TextEntity实现
public class TextEntity {
private String msgType;
// 显示内容
private String content;
// 是否at所有人
private Boolean isAtAll;
// 被@人的手机号(在content里添加@人的手机号)
private List atMobiles;
public String getMsgType() {
return "text";
}
.....此处省略setter、getter
public String getJSONObjectString() {
// text类型
JSONObject content = new JSONObject();
content.put("content", this.getContent());
// at some body
JSONObject atMobile = new JSONObject();
if(this.getAtMobiles().size() > 0){
List mobiles = new ArrayList();
for (int i=0;i0){
atMobile.put("atMobiles", mobiles);
}
atMobile.put("isAtAll", this.getIsAtAll());
}
JSONObject json = new JSONObject();
json.put("msgtype", this.getMsgType());
json.put("text", content);
json.put("at", atMobile);
return json.toJSONString();
}
}
{
"msgtype": "link",
"link": {
"text": "这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。
而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?",
"title": "时代的火车向前开",
"picUrl": "",
"messageUrl": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
}
}
LinkEntity实现
public class LinkEntity {
private String msgType;
// 显示标题
private String title;
// 显示内容
private String content;
// icon url
private String picUrl;
// 内容对链接
private String messageUrl;
public String getMsgType() {
return "link";
}
.....此处省略setter、getter
public String getJSONObjectString() {
// text类型
JSONObject linkContent = new JSONObject();
linkContent.put("title", this.getTitle());
linkContent.put("text", this.getContent());
linkContent.put("picUrl", this.getPicUrl());
linkContent.put("messageUrl", this.getMessageUrl());
JSONObject json = new JSONObject();
json.put("msgtype", this.getMsgType());
json.put("link", linkContent);
return json.toJSONString();
}
}
{
"msgtype": "markdown",
"markdown": {
"title":"杭州天气",
"text": "#### 杭州天气 @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"
},
"at": {
"atMobiles": [
"156xxxx8827",
"189xxxx8325"
],
"isAtAll": false
}
}
MarkdownEntity 实现
public class MarkdownEntity {
private String msgType;
// 显示标题
private String title;
// 显示内容
private String content;
// 是否at所有人
private Boolean isAtAll;
// 被@人的手机号(在content里添加@人的手机号)
private List atMobiles;
public String getMsgType() {
return "markdown";
}
.....此处省略setter、getter
public String getJSONObjectString() {
// markdown类型
JSONObject markdownContent = new JSONObject();
markdownContent.put("title", this.getTitle());
markdownContent.put("text", this.getContent());
// at some body
JSONObject atMobile = new JSONObject();
if(this.getAtMobiles().size() > 0){
List mobiles = new ArrayList();
for (int i=0;i0){
atMobile.put("atMobiles", mobiles);
}
atMobile.put("isAtAll", this.getIsAtAll());
}
JSONObject json = new JSONObject();
json.put("msgtype", this.getMsgType());
json.put("markdown", markdownContent);
json.put("at", atMobile);
return json.toJSONString();
}
}
{
"actionCard": {
"title": "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
"text": "![screenshot](serverapi2/@lADOpwk3K80C0M0FoA)
### 乔布斯 20 年前想打造的苹果咖啡厅
Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
"hideAvatar": "0",
"btnOrientation": "0",
"singleTitle" : "阅读全文",
"singleURL" : "https://www.dingtalk.com/"
},
"msgtype": "actionCard"
}
ActionCardEntity实现
public class ActionCardEntity {
private String msgType;
// 显示标题
private String title;
// 显示内容 markdown格式的消息
private String content;
// 0-正常发消息者头像,1-隐藏发消息者头像
private String hideAvatar;
// 0-按钮竖直排列,1-按钮横向排列
private String btnOrientation;
// 单个按钮的方案。(设置此项和singleURL后btns无效)
private String singleTitle;
// 点击singleTitle按钮触发的URL
private String singleURL;
private List btns;
public String getMsgType() {
return "actionCard";
}
.....此处省略setter、getter
public String getJSONObjectString() {
// text类型
JSONObject actionCardContent = new JSONObject();
actionCardContent.put("title", this.getTitle());
actionCardContent.put("text", this.getContent());
actionCardContent.put("hideAvatar", this.getHideAvatar());
actionCardContent.put("btnOrientation", this.getBtnOrientation());
if(!StringUtils.isEmpty(this.getSingleTitle()) && !StringUtils.isEmpty(this.getSingleURL())){
actionCardContent.put("singleTitle", this.getSingleTitle());
actionCardContent.put("singleURL", this.getSingleURL());
}else{
List btns = new ArrayList();
for (int i=0;i0){
actionCardContent.put("btns", btns);
}
}
JSONObject json = new JSONObject();
json.put("msgtype", this.getMsgType());
json.put("actionCard", actionCardContent);
return json.toJSONString();
}
}
{
"feedCard": {
"links": [
{
"title": "时代的火车向前开",
"messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
"picURL": "https://www.dingtalk.com/"
},
{
"title": "时代的火车向前开2",
"messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
"picURL": "https://www.dingtalk.com/"
}
]
},
"msgtype": "feedCard"
}
FeedCardEntity实现
ublic class FeedCardEntity {
private String msgType;
// links
private List links;
public String getMsgType() {
return "feedCard";
}
.....此处省略setter、getter
public String getJSONObjectString() {
// text类型
JSONObject feedCardContent = new JSONObject();
List links = new ArrayList();
for (int i=0;i0){
feedCardContent.put("links", links);
}
JSONObject json = new JSONObject();
json.put("msgtype", this.getMsgType());
json.put("feedCard", feedCardContent);
return json.toJSONString();
}
}