Table of contents
reference
Gadgets - an official tutorial that takes you to play with SpringBoot DingTalk robot
- custom robot access
1. Scene introduction
There are many systems within the enterprise that support the company's core business processes, such as CRM systems, transaction systems, monitoring and alarm systems, and so on. Through DingTalk's custom robot, these system events can be synchronized to DingTalk's chat group.
Note
Currently, the robot does not support the response mechanism. This mechanism refers to when members in the group chat with @bot, DingTalk calls back the specified service address, that is, the Outgoing robot.
Call frequency limit
Since sending too frequent messages will seriously affect the user experience of group members, the DingTalk open platform imposes the following restrictions on the frequency of message sending by custom robots:
Each robot can send up to 20 messages to the group per minute. If more than 20 messages are sent, the flow will be limited for 10 minutes.
Note that if you have a scenario where you send a large number of messages (such as system monitoring and alarms), you can integrate the information and send it to the group in the form of a summary through markdown messages.
2. Create a Ding group
First of all, create a group chat in the DingTalk software. This is relatively simple, so I won’t say much. The main thing to note is that there are at least three people in a group chat, but how can one person create a group? It's also very simple, just pull 2 people in at the beginning, and then kick them out after the creation is successful, so that 1 person can play happily with the robot in a group~
3. Add a custom robot
Add a robot
Add - choose to sign, save this secret
and save this webhook address
4. Coding
1. Import package
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
2. Encapsulate public methods
private static final String SECRET = "SEC2c885aef51xxxxxx4703fc94de28c6b";
private static final String URL = "https://oapi.dingtalk.com/robot/send?access_token=0e82c5326dd33b4xxxxx026ed53eb47b47";
/**
* 组装签名url
* @return url
*/
public static String getURL()throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
Long timestamp = System.currentTimeMillis();
String stringToSign = timestamp + "\n" + SECRET;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
String signResult = "×tamp=" + timestamp + "&sign=" + sign;
// 得到拼接后的 URL
return URL + signResult;
}
/**
* 获取客户端
* @return
*/
public static DingTalkClient getClient()throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException{
return new DefaultDingTalkClient(getURL());
}
3. Send a text message
How to set at.setIsAtAll(true); the message will @everyone
public static void sendText(String content) throws ApiException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
DingTalkClient client = getClient();
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent(content);
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
// isAtAll类型如果不为Boolean,请升级至最新SDK
at.setIsAtAll(false);
at.setAtMobiles(Arrays.asList("1392xxxxx","155xxxx"));
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
log.info("success:{}, code:{}, errorCode:{}, errorMsg:{}",response.isSuccess(),response.getCode(),response.getErrcode(),response.getErrmsg());
}
4. Send Link message
How to set at.setIsAtAll(true); the message will @everyone
@SneakyThrows
public static void sendLink(){
DingTalkClient client = getClient();
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("link");
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setMessageUrl("https://www.dingtalk.com/");
link.setPicUrl("https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png");
link.setTitle("时代的火车向前开");
link.setText("这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林");
request.setLink(link);
OapiRobotSendResponse response = client.execute(request);
}
5. Send MarkDown message
@SneakyThrows
public static void sendMarkDown(){
DingTalkClient client = getClient();
OapiRobotSendRequest request = new OapiRobotSendRequest();
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
request.setMsgtype("markdown");
markdown.setTitle("杭州天气");
markdown.setText("#### 杭州天气 \n" +
"> 9度,西北风1级,空气良89,相对温度73%\n\n" +
"> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
"> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
request.setMarkdown(markdown);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("1392xxxxx","155xxxx"));
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
System.out.printf("success:%s, code:%s, errorCode:%s, errorMsg:%s%n",response.isSuccess(),response.getCode(),response.getErrcode(),response.getErrmsg());
}
6. Send ActionCard message
@SneakyThrows
public static void sendActionCard(){
DingTalkClient client = getClient();
OapiRobotSendRequest request = new OapiRobotSendRequest();
OapiRobotSendRequest.Actioncard card = new OapiRobotSendRequest.Actioncard();
request.setMsgtype("actionCard");
card.setTitle("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身");
card.setText("![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \n" +
" ### 乔布斯 20 年前想打造的苹果咖啡厅 \n" +
" Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计");
// 0:按钮竖直排列, 1:按钮横向排列
card.setBtnOrientation("0");
card.setSingleTitle("阅读全文");
card.setSingleURL("https://www.dingtalk.com/");
request.setActionCard(card);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("1392xxxxx","155xxxx"));
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
}
7. Send FeedCard message
@SneakyThrows
@SneakyThrows
public static void sendFreeCard(){
DingTalkClient client = getClient();
OapiRobotSendRequest request = new OapiRobotSendRequest();
OapiRobotSendRequest.Feedcard card = new OapiRobotSendRequest.Feedcard();
request.setMsgtype("feedCard");
List<OapiRobotSendRequest.Links> data = new ArrayList<>();
OapiRobotSendRequest.Links links1 = new OapiRobotSendRequest.Links();
OapiRobotSendRequest.Links links2 = new OapiRobotSendRequest.Links();
OapiRobotSendRequest.Link link1 = new OapiRobotSendRequest.Link();
link1.setText("标题1");
link1.setTitle("时代的火车向前开1");
link1.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
link1.setMessageUrl("https://www.dingtalk.com/");
links1.setTitle("时代的火车向前开1");
links1.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
links1.setMessageURL("https://www.dingtalk.com/");
data.add(links1);
links2.setTitle("时代的火车向前开2");
links2.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
links2.setMessageURL("https://www.dingtalk.com/");
data.add(links2);
card.setLinks(data);
request.setFeedCard(card);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("1392xxxxx","155xxxx"));
request.setAt(at);
OapiRobotSendResponse response = client.execute(request);
}
8. Send interactive messages
Refer to the official website documentation: ActionCard