前言
在消息通知这块,钉钉可谓是玩出了花,比如工作通知、群机器人通知,还有那万恶的Ding一下。钉钉的通知不仅花样多,而且大部分渠道都支持自定义,也即可以自定义设置发送时间、发送内容,并且还支持多种样式的消息如文本、卡片、Markdown等。
这篇文章我主要介绍一下常用的两类:钉钉群机器人通知和钉钉工作通知。
钉钉群机器人通知效果图
钉钉工作通知效果图
具体的区别和对比我就不写了,简单来说钉钉群机器人消息通知发的消息所有人都可以看到,钉钉工作通知只有选择到的人才可以看到,所以钉钉群机器人消息通知适合发公告通知所有人,钉钉工作通知适合发工作安排通知到具体人,不过具体怎么用还得看业务的需要。
钉钉群机器人通知实现
官方文档链接
自定义机器人接入
:https://open.dingtalk.com/document/robots/custom-robot-access
自定义机器人安全设置
:https://open.dingtalk.com/document/robots/customize-robot-security-settings
配置流程
1. 选择需要添加机器人的群聊,然后依次单击群设置 > 机器人。
2. 点击添加机器人,然后找到自定义机器人
3. 输入配置,安全设置需要注意一下
由于创建自定义机器人必须选择安全设置,这里注意不要乱输,如果明白安全设置是啥最好,如果不懂建议选择加签的方式,然后看一下说明文档,不然测试的时候有可能会发送不成功。
4. 点击完成,会生成Webhook
开发机器人
1. 保存好安全设置的token和Webhook
2. 导入maven旧版依赖
<!-- 使用旧版钉钉开放api -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
3. 编写测试类
package com.example.dingtalkmsg.service;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.request.OapiRobotSendRequest.Links;
import com.dingtalk.api.response.OapiRobotSendResponse;
import org.apache.tomcat.util.codec.binary.Base64;
public class DingTalkRobotSendService {
public static void main(String[] args) throws Exception {
//安全设置加签
Long timestamp = System.currentTimeMillis();
String secret = "xxx ";
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");
//构建消息发送Client
DingTalkClient client = new DefaultDingTalkClient(
"https://oapi.dingtalk"
+ ".com/robot/send?access_token=xxx"
+ "×tamp="
+ timestamp + "&sign=" + sign);
//创建发送请求体
OapiRobotSendRequest request = new OapiRobotSendRequest();
request.setMsgtype("text");
//文本消息
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
text.setContent("测试文本消息");
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("xxxxx"));
// isAtAll类型如果不为Boolean,请升级至最新SDK
//at.setIsAtAll(true);
//at.setAtUserIds(Arrays.asList("109929","32099"));
request.setMsgtype("link");
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setMessageUrl("https://www.dingtalk.com/");
link.setPicUrl("");
link.setTitle("时代的火车向前开");
link.setText("这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林");
request.setLink(link);
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle("杭州天气");
markdown.setText("#### 杭州天气 @156xxxx8827\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);
request.setMsgtype("feedCard");
OapiRobotSendRequest.Feedcard feedcard = new OapiRobotSendRequest.Feedcard();
OapiRobotSendRequest.Links links1 = new OapiRobotSendRequest.Links();
links1.setTitle("时代的火车向前开1");
links1.setMessageURL("https://www.dingtalk.com/");
links1.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
OapiRobotSendRequest.Links links2 = new OapiRobotSendRequest.Links();
links2.setTitle("时代的火车向前开2");
links2.setMessageURL("https://www.dingtalk.com/");
links2.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
List<Links> links = new ArrayList<>();
links.add(links1);
links.add(links2);
feedcard.setLinks(links);
request.setFeedCard(feedcard);
OapiRobotSendResponse response = client.execute(request);
System.out.println(response);
}
}
总体来说,钉钉机器人推送消息的开发没有那些弯弯绕绕的设置,基本上能看懂文档就可以开发出来,也不需要什么特殊的环境支持。
钉钉工作通知实现
官方文档链接
第三方企业应用开发
:https://open.dingtalk.com/document/isvapp/send-job-notification
配置流程
1. 创建一个第三方企业应用
我本来以为企业内应用和第三方企业应用一样,创建应用之后都有一个工作通知。创建应用后发现,确实都有一个,但是区别非常大。企业内应用通知就是机器人群消息,而第三方企业应用则是模板消息。
2. 依次点击应用功能—>消息推送—>新增
3. 选择Markdown模板进行创建—>发送测试消息
4. 保存好消息模板后点击提交审核,审核一般半天左右会通过
开发工作通知
1. 导入maven旧版依赖
<!-- 使用旧版钉钉开放api -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
2. 编写测试类
package com.example.dingtalkmsg.service;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
public class DingTalkWorkNoticeSendService {
public static void main(String[] args) throws Exception {
//获取小程序的accessToken
DefaultDingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
req.setAuthCorpid("dingxxxx");
//suiteTicket获取比较麻烦,后续我会单独写文章说明
OapiServiceGetCorpTokenResponse execute = client1.execute(req, "xxx", "xxx", "xxx");
//构建工作通知推送Client
DingTalkClient client2 = new DefaultDingTalkClient(
"https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setAgentId(836390886L);
request.setUseridList("user123");
request.setToAllUser(false);
//构建消息模板
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("text");
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
msg.getText().setContent("test123");
request.setMsg(msg);
msg.setMsgtype("image");
msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
msg.getImage().setMediaId("@lADOdvRYes0CbM0CbA");
request.setMsg(msg);
msg.setMsgtype("file");
msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
msg.getFile().setMediaId("@lADOdvRYes0CbM0CbA");
request.setMsg(msg);
msg.setMsgtype("link");
msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
msg.getLink().setTitle("test");
msg.getLink().setText("test");
msg.getLink().setMessageUrl("test");
msg.getLink().setPicUrl("test");
request.setMsg(msg);
msg.setMsgtype("markdown");
msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
msg.getMarkdown().setText("##### text");
msg.getMarkdown().setTitle("### Title");
request.setMsg(msg);
msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
msg.getOa().getHead().setText("head");
msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
msg.getOa().getBody().setContent("xxx");
msg.setMsgtype("oa");
request.setMsg(msg);
msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
msg.getActionCard().setTitle("xxx123411111");
msg.getActionCard().setMarkdown("### 测试123111");
msg.getActionCard().setSingleTitle("测试测试");
msg.getActionCard().setSingleUrl("https://www.dingtalk.com");
msg.setMsgtype("action_card");
request.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response rsp = client2.execute(request, execute.getAccessToken());
System.out.println(rsp.getBody());
}
}
标签:入门,dingtalk,通知,机器人,request,import,msg,new,com From: https://www.cnblogs.com/wlovet/p/17304045.html其实工作通知开发的难度也不高,但是由于这是个三方应用,所有三方应用的接口都需要一个accessToken。而这个accessToken也不是简单的拿ak/sk去获取就行了,钉钉那边还需要一个suiteTicket,这个suiteTicket才是开发的最大难点。
suiteTicket不是调用接口去获取的,而是钉钉主动推送过来的,所以我们还需要写一个回调接口供钉钉侧调用,这个回调接口会在后续文章中补充。