Recently, the project needs to be able to push @someone’s message in the Dingpin group. I checked the support of Dingpin’s Open API and recorded it.

  1. Group Settings -> Smart Group Assistant -> Add robots



There are many types of robots, and our customized push needs Webhook access.

After the robot is created, you can see the Webhook address in the robot Settings screen. It should be noted that the security Settings are mandatory. If you fill in the user-defined keyword, the message containing the user-defined keyword will be pushed.



Code 2.

Interface document address:In this

The message format is as follows:

{"msgtype": "markdown", "markdown": {"title":" Hangzhou weather ", "text": "#### Hangzhou weather @150XXXXXXXX \n > 9 degrees, northwest wind level 1, good air 89, relative temperature 73%\n >! [screenshot] (https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \ n > # # # # # # 10 p.m [weather] (https://www.dingtalk.com) \ n "}, "at" : {" atMobiles ":" 150 XXXXXXXX "], "atUserIds:" [] "user123", "isAtAll" : false } }Copy the code

Code:

Private static void sendDingTalkAtMessage(String webhook, String atMobiles, String message) { try { DingTalkClient client = new DefaultDingTalkClient(webhook); OapiRobotSendRequest request = new OapiRobotSendRequest(); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); If (stringutils.isnotBlank (atMobiles)) {at.setAtmobiles (arrays.aslist (Atmobiles.split (","))); String atMsg = Arrays.asList(atMobiles.split(",")).stream().map(mobile -> "@" + mobile).collect(Collectors.joining()); message += atMsg; } else {// @owner at.setisatall ("true"); } request.setAt(at); request.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); Markdown. SetTitle (" test "); markdown.setText(message); logger.info("message:{}", message); request.setMarkdown(markdown); client.execute(request); } catch (ApiException e) { logger.error("sendDingTalkAtMessage error:", e); }}Copy the code
  1. The effect

The nail group implements @ type message push, and the final effect is shown in the figure:



The message type can be markdown. The effect of markdown is as follows: