您现在的位置是:首页 >技术杂谈 >通知神器——java调用钉钉群自定义机器人----Jay网站首页技术杂谈
通知神器——java调用钉钉群自定义机器人----Jay
简介通知神器——java调用钉钉群自定义机器人----Jay
其中webhook非常重要,下文详述。点击设置说明可以看相关使用文档,文档链接见本文末尾
创建群自定义机器人
其中webhook非常重要,下文详述。点击设置说明可以看相关使用文档,文档链接见本文末尾
使用HTTP POST请求发送消息
直接向上文所述的webhook链接发送POST请求已达到自定义机器人向群内发消息的功能
按照文档中所属的参数格式直接向链接发送请求即可,我使用Postman发送请求如下:
效果如下:
发送其他格式(如:链接消息、markdown、ActionCard)的消息效果如下:
确定你需要哪种消息了吗?如果需要使用,通过这种方式来实现,组织参数还是挺麻烦的,不过我们可以使用sdk来简化操作
使用sdk调用自定义机器人
public static void main(String[] args) throws ApiException {
final String serverUrl =
"https://oapi.dingtalk.com/robot/send?access_token=*******************************************************";
DingTalkClient client = new DefaultDingTalkClient(serverUrl);
OapiRobotSendRequest request = new OapiRobotSendRequest();
At at = new At();
at.setIsAtAll("true");
request.setAt(at);
// 若上一步isAtAll没有设置true,则根据此处设置的手机号来@指定人
List<String> mobiles = new ArrayList<>();
mobiles.add("166********");
at.setAtMobiles(mobiles);
// 以下是设置各种消息格式的方法
sentText(request);
// sendLink(request);
// sendMarkdown(request);
// sendActionCard(request);
// sendFeedCard(request);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getErrmsg());
}
text文本消息
public static void sentText(OapiRobotSendRequest request) {
Text text = new Text();
text.setContent("sdk demo");
request.setMsgtype("text");
request.setText(text);
}
link链接消息
public static void sendLink(OapiRobotSendRequest request) {
Link link = new Link();
link.setTitle("好消息!好消息!");
link.setText("本群与百度成功达成合作关系,今后大家有什么不懂的可以直接百度搜索,不用再群里提问浪费时间啦!");
link.setMessageUrl("https://www.baidu.com");
link.setPicUrl("http://www.baidu.com/img/bd_logo1.png");
request.setMsgtype("link");
request.setLink(link);
}
markdown消息
public static void sendMarkdown(OapiRobotSendRequest request) {
Markdown markdown = new Markdown();
markdown.setTitle("好消息!好消息!");
markdown.setText(
"#### 杭州天气 @156xxxx8827
> 9度,西北风1级,空气良89,相对温度73%
"
+ "> ![screenshot](https://img2018.cnblogs.com/blog/1775867/201908/1775867-20190823214506246-1471056011.png)
"
+ "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/)
");
request.setMsgtype("markdown");
request.setMarkdown(markdown);
}
ActionCard消息
public static void sendActionCard(OapiRobotSendRequest request) {
Actioncard actioncard = new Actioncard();
actioncard.setTitle("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
actioncard.setText(
"![screenshot](@lADOpwk3K80C0M0FoA)
"
+ " ### 乔布斯 20 年前想打造的苹果咖啡厅 Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
actioncard.setHideAvatar("0");
actioncard.setBtnOrientation("1");
// actioncard.setSingleTitle("阅读全文");
// actioncard.setSingleURL("https://www.baidu.com/");
List<Btns> btns = new ArrayList<>();
Btns btn0 = new Btns();
btn0.setTitle("内容不错");
btn0.setActionURL("https://www.qq.com/");
btns.add(btn0);
Btns btn1 = new Btns();
btn1.setTitle("不感兴趣");
btn1.setActionURL("https://www.baidu.com/");
btns.add(btn1);
actioncard.setBtns(btns);
request.setMsgtype("actionCard");
request.setActionCard(actioncard);
}
FeedCard消息
类似公众号头条、次条消息
public static void sendFeedCard(OapiRobotSendRequest request) {
Feedcard feedcard = new Feedcard();
List<Links> linksList = new ArrayList<>();
Links links0 = new Links();
links0.setTitle("时代的火车向前开1");
links0.setMessageURL(
"https://www.dingtalk.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");
links0.setPicURL("https://www.dingtalk.com/");
linksList.add(links0);
Links links1 = new Links();
links1.setTitle("时代的火车向前开2");
links1.setMessageURL(
"https://www.dingtalk.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");
links1.setPicURL("https://www.dingtalk.com/");
linksList.add(links1);
feedcard.setLinks(linksList);
request.setMsgtype("feedCard");
request.setFeedCard(feedcard);
}
demo~~
package com.macro.mall.controller;
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 java.util.Arrays;
//————————————————
public class DingDIngTest {
public static void main(String[] args) throws ApiException {
// 根据机器人的token获取DingTalkClient
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=???");
// 创建请求对象
OapiRobotSendRequest request = new OapiRobotSendRequest();
// 文本消息
request.setMsgtype("text");
request.setText("1123");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
// 注意内容中要包含你设置的关键词!
text.setContent("你好:song");
request.setText(text);
// 添加联系方式对象
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("46549618"));
at.setIsAtAll(true);// @All
request.setAt(at);
// 发送
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.isSuccess());
}
}
demo~~
<dependency> <groupId>com.ganshane.specs</groupId> <artifactId>dingtalk-sdk</artifactId> <version>20210115</version> </dependency> <dependency> <groupId>com.carrotsearch.thirdparty</groupId> <artifactId>simple-xml-safe</artifactId> <version>2.7.1</version> </dependency>
package com.macro.mall.controller;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.macro.mall.common.exception.ApiException;
import lombok.SneakyThrows;
import java.util.ArrayList;
import java.util.List;
public class DingDing {
@SneakyThrows
public static void main(String[] args) throws ApiException {
final String serverUrl =
"https://oapi.dingtalk.com/robot/send?access_token=???";
DingTalkClient client = new DefaultDingTalkClient(serverUrl);
OapiRobotSendRequest request = new OapiRobotSendRequest();
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setIsAtAll(true);
request.setAt(at);
// 若上一步isAtAll没有设置true,则根据此处设置的手机号来@指定人
List<String> mobiles = new ArrayList<>();
at.setAtMobiles(mobiles);
// 以下是设置各种消息格式的方法
sentText(request);
sendLink(request);
sendMarkdown(request);
sendActionCard(request);
sendFeedCard(request);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response.getErrmsg());
}
public static void sentText(OapiRobotSendRequest request) {
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("你好: 123");
request.setMsgtype("text");
request.setText(text);
}
public static void sendLink(OapiRobotSendRequest request) {
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setTitle("好消息!好消息!");
link.setText("你好:本群与百度成功达成合作关系,今后大家有什么不懂的可以直接百度搜索,不用再群里提问浪费时间啦!");
link.setMessageUrl("https://www.baidu.com");
link.setPicUrl("http://www.baidu.com/img/bd_logo1.png");
request.setMsgtype("link");
request.setLink(link);
}
public static void sendMarkdown(OapiRobotSendRequest request) {
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle("好消息!好消息!");
markdown.setText(
"你好: #### 杭州天气 @156xxxx8827
> 9度,西北风1级,空气良89,相对温度73%
"
+ "> ![screenshot](https://img2018.cnblogs.com/blog/1775867/201908/1775867-20190823214506246-1471056011.png)
"
+ "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/)
");
request.setMsgtype("markdown");
request.setMarkdown(markdown);
}
public static void sendActionCard(OapiRobotSendRequest request) {
OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
actioncard.setTitle("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
actioncard.setText(
"你好: ![screenshot](@lADOpwk3K80C0M0FoA)
"
+ " ### 乔布斯 20 年前想打造的苹果咖啡厅 Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划");
actioncard.setHideAvatar("0");
actioncard.setBtnOrientation("1");
// actioncard.setSingleTitle("阅读全文");
// actioncard.setSingleURL("https://www.baidu.com/");
List<OapiRobotSendRequest.Btns> btns = new ArrayList<>();
OapiRobotSendRequest.Btns btn0 = new OapiRobotSendRequest.Btns();
btn0.setTitle("内容不错");
btn0.setActionURL("https://www.qq.com/");
btns.add(btn0);
OapiRobotSendRequest.Btns btn1 = new OapiRobotSendRequest.Btns();
btn1.setTitle("不感兴趣");
btn1.setActionURL("https://www.baidu.com/");
btns.add(btn1);
actioncard.setBtns(btns);
request.setMsgtype("actionCard");
request.setActionCard(actioncard);
}
public static void sendFeedCard(OapiRobotSendRequest request) {
OapiRobotSendRequest.Feedcard feedcard = new OapiRobotSendRequest.Feedcard();
List<OapiRobotSendRequest.Links> linksList = new ArrayList<>();
OapiRobotSendRequest.Links links0 = new OapiRobotSendRequest.Links();
links0.setTitle("你好:时代的火车向前开1");
links0.setMessageURL(
"https://www.dingtalk.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");
links0.setPicURL("https://www.dingtalk.com/");
linksList.add(links0);
OapiRobotSendRequest.Links links1 = new OapiRobotSendRequest.Links();
links1.setTitle("时代的火车向前开2");
links1.setMessageURL(
"https://www.dingtalk.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");
links1.setPicURL("https://www.dingtalk.com/");
linksList.add(links1);
feedcard.setLinks(linksList);
request.setMsgtype("feedCard");
request.setFeedCard(feedcard);
}
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。