钉钉目前已经作为一个常见的办公工具,实现和钉钉的整合可带来更大的场景,
钉钉的集成主要有三大应用场景:
(1)以钉钉为入口直接对接H5和小程序应用软件【和微信公众号类似】
(2)利用钉钉的API实现和钉钉功能的互通,适用于基于钉钉办公的企业做一些二次开发的功能,如考勤、个人信息推送、请假、审批流、钉钉组织结构和用户等功能实现交互
(3)利用钉钉的群发功能,推送相关信息
本文重点介绍第三种情况
1.准备工作
(1)下载钉钉SDK【注意钉钉没有提供公用的pom,要么是放在nexus上,要么本地创建jar】
下载地址:
https://ding-doc.dingtalk.com/document#/resourcedownload
解压后找到jar
(2)在resources文件夹里创建lib文件夹,把jar文件
(3)修改pom文件
<!--外部钉钉第三方jar-->
<dependency>
<groupId>com.dingtalk.api</groupId>
<artifactId>dingtalk</artifactId>
<version>3.0.12</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/dingding.jar
</systemPath>
</dependency>
<!--
Apache开源组织提供的用于摘要运算、编码解码的包。常见的编码解码工具Base64、MD5、Hex、SHA1、DES等,
在钉钉做签名时用到
-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>sbdemo</finalName>
<plugins>
<!--spring boot maven插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--外部jar引用必须使用,否则maven打包不会引入-->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
(4)添加钉钉预警机器人
2.先建立群组 在手机端钉钉里,通讯录里-》我的群组-》发起群聊,选择公司组织架构里的人,起个群的名称 |
2.通过电脑版钉钉点击我的头像,选择机器人管理,点击自定义webhook, |
给机器人取个名字,选择加签的模式,记住加签的密钥,点击完成,进入机器人的页面,基础url,点击完成,则创建成功
|
5.编写代码封装发消息的对象
package com.jyj.soft.comm;
/**
* @class: com.jyj.soft.comm.RobotUtil
* @description: 钉钉机器人群发帮助类
* @author: jiangzengkui
* @company: 教育家
* @create: 2020-12-06 19:17
*/
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
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;
public class RobotUtil {
/**
* 机器人信息
*/
//-----------------机器人信息,可配置到yml文件里
public final static String robot_name_test="1";//机器人名-测试
//--地址
private final static String test_url="https://oapi.dingtalk.com/robot/send?access_token=ae4c9916b96e0b90bc7880fb5888873c0f8bd05eb3a8a2c931e4e5765ec0bced";
//签名密钥
private final static String test_secret ="SECc7c01a4e60cdb5b632d7f426401bfc1b84a2ac8cc3045a368d14071cd3b046f6";
/**
* 发送文本信息
* @param robotName 机器人名称
* @param cont
* @mobiles @相关人,对应钉钉手机号,所有人传递null
*/
public static void sendTextMsg(String robotName,String cont,String[] mobiles){
String url=getUrl(robotName);
DingTalkClient client = new DefaultDingTalkClient(url);
OapiRobotSendRequest request = new OapiRobotSendRequest();
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
request.setMsgtype("text"); //设置消息类型
text.setContent(cont);//设置内容
request.setText(text);//设置文本
setMsgtAt(request,mobiles);//设置接收人@
msgSend(client,request);//发送
}
/**
* 群发链接消息
* @param robotName 机器人名称
* @param linkUrl 点击链接
* @param linkPicUrl 显示图片
* @param title 标题
* @param cont 内容
*/
public static void sendLinkMsg(String robotName,String[] mobiles,String linkUrl,String linkPicUrl,String title,String cont){
String url=getUrl(robotName);
DingTalkClient client = new DefaultDingTalkClient(url);
OapiRobotSendRequest request = new OapiRobotSendRequest();
//相关内容
request.setMsgtype("link");
OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
link.setMessageUrl(linkUrl);
link.setPicUrl(linkPicUrl);
link.setTitle(title);
link.setText(cont);
request.setLink(link);
setMsgtAt(request,mobiles);//设置接收人@
//发送
msgSend(client,request);
}
/**
* 发布markdown内容
* @param robotName
* @param mobiles
* @param title
* @param markdownCont
*/
public static void sendMarkdownMsg(String robotName,String[] mobiles,String title,String markdownCont){
String url=getUrl(robotName);
DingTalkClient client = new DefaultDingTalkClient(url);
OapiRobotSendRequest request = new OapiRobotSendRequest();
//相关内容
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle(title);
markdown.setText(markdownCont);
request.setMarkdown(markdown);
//设置接收人@
setMsgtAt(request,mobiles);
//发送
msgSend(client,request);
}
/**
* 通用Markdown
* @param robotName
* @param mobiles
* @param title
* @param desc 详细描述不服
* @param pic_url 显示图片
* @param detail_url 点击链接
*/
public static void sendMarkdownMsgTemplate(String robotName,String[] mobiles,String title,String desc,String pic_url,String detail_url){
String url=getUrl(robotName);
DingTalkClient client = new DefaultDingTalkClient(url);
OapiRobotSendRequest request = new OapiRobotSendRequest();
//相关内容
pic_url=dropNull(pic_url);
detail_url=dropNull(detail_url);
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle(title);
String markdownCont="<font color=#FF0000 size=4 face=\"黑体\">"+title+"</font>\n\n "
+ "> "+desc+" \n\n";
if(pic_url.length()!=0){
markdownCont+="> ![screenshot]("+pic_url+")\n\n";
}
if(detail_url.length()!=0){
markdownCont+="> [详情请点击]("+detail_url+") \n\n";
}
markdown.setText(markdownCont);
request.setMarkdown(markdown);
//设置接收人@
setMsgtAt(request,mobiles);
//发送
msgSend(client,request);
}
/**
* 内容是多条List<String> string 标题·内容
* @param robotName
* @param mobiles
* @param title
* @param desc
* @param pic_url
* @param detail_url
*/
public static void sendMarkdownMsgListTemplate(String robotName,String[] mobiles,String title,List<String>contList,String pic_url,String detail_url){
String url=getUrl(robotName);
DingTalkClient client = new DefaultDingTalkClient(url);
OapiRobotSendRequest request = new OapiRobotSendRequest();
//相关内容
pic_url=dropNull(pic_url);
detail_url=dropNull(detail_url);
request.setMsgtype("markdown");
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
markdown.setTitle(title);
String markdownCont="<font color=#FF0000 size=4 face=\"黑体\">"+title+"</font>\n\n ";
String subTitle="";
String subCont="";
String[] sz=null;
for(String desc:contList){
sz=desc.split("`");
markdownCont+="> "+"**"+sz[0]+":**"+sz[1]+" \n\n";
}
if(pic_url.length()!=0){
markdownCont+="> ![screenshot]("+pic_url+")\n\n";
}
if(detail_url.length()!=0){
markdownCont+="> [详情请点击]("+detail_url+") \n\n";
}
markdown.setText(markdownCont);
request.setMarkdown(markdown);
//设置接收人@
setMsgtAt(request,mobiles);
//发送
msgSend(client,request);
}
/**
* 发送request
* @param client
* @param request
*/
private static void msgSend(DingTalkClient client,OapiRobotSendRequest request){
try {
OapiRobotSendResponse response = client.execute(request);
System.out.println("消息群发成功");
} catch (ApiException e) {
System.out.println("消息群发失败");
// TODO Auto-generated catch block
e.printStackTrace();
throw new java.lang.RuntimeException(e.getErrMsg());
}
}
/**
* 得到测试机器人的链接
* @return
* @throws Exception
*/
private static String getUrl(String robotName){
String url="";
Long timestamp=RobotUtil.getTimeStamp();
if(robot_name_test.equals(robotName)){
url=test_url
+"×tamp="+timestamp
+"&sign="+RobotUtil.getSign(timestamp, test_secret);
}
return url;
}
/**
* 获得签名
* @param timestamp
* @param appSecret
* @return
* @throws Exception
*/
private static String getSign(Long timestamp,String appSecret) {
String sign="";
try{
String stringToSign = timestamp + "\n" + appSecret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(appSecret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
}
catch(Exception e){
e.printStackTrace();
throw new java.lang.RuntimeException(e.getMessage());
}
return sign;
}
/**
* 设置消息@人
* @param request
* @param mobiles 联系人对应的dingding手机号,如果null或者空,则At所有人
*/
private static void setMsgtAt(OapiRobotSendRequest request,String[] mobiles){
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
if(mobiles==null || mobiles.length==0){
at.setIsAtAll(true);
}
else{
at.setAtMobiles(Arrays.asList(mobiles));
}
request.setAt(at);
}
private static Long getTimeStamp(){
return System.currentTimeMillis();
}
private static String dropNull(String val){
if(val==null)val="";
return val.trim();
}
public static void main(String[] args) throws Exception {
// RobotUtil.sendTextMsg(RobotUtil.robot_name_test, "人生大起大落实在太刺激了", new String[]{"13688006635"});
/* String linkUrl="http://jth.gz2vip.idcfengye.com/pm/pmadmin/index.do";
String linkPicUrl=null;
String title="烦烦烦";
String cont="这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林";
//RobotUtil.sendLinkMsg(RobotUtil.robot_name_test,new String[]{"13688006635"}, linkUrl, linkPicUrl, title, cont);
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);
*/
String markDowonCont="<font color=#FF0000 size=4 face=\"黑体\">杭州天气 </font>\n\n " +
"> **9度**,西北风1级,空气良89,相对温度73%9度,西北风1级,空气良89,相对温度73%9度,西北风1级,空气良89,相对温度73%9度,西北风1级,空气良89,相对温度73%9度,西北风1级,空气良89,相对温度73%9度,西北风1级,空气良89,相对温度73%9度,西北风1级,空气良89,相对温度73%\n\n" +
"> ![screenshot](http://jth.gz2vip.idcfengye.com/pm/resource/admin/images/logo_3.jpg)\n" +
"> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"
+" ##### **机器**: 192.167.3 \n\n";
// RobotUtil.sendMarkdownMsg(RobotUtil.robot_name_test,new String[]{"13688006635"}, "maorkdowon内容", markDowonCont);
String title="十万个为什么";
String desc="不要问我十万个为什么不要问我十万个为什么不要问我十万个为什么不要问我十万个为什么不要问我十万个为什么不要问我十万个为什么不要问我十万个为什么不要问我十万个为什么";
String pic_url="http://jth.gz2vip.idcfengye.com/pm/resource/admin/images/logo_3.jpg";
String detail_url="http://www.sina.com.cn";
sendMarkdownMsgTemplate(RobotUtil.robot_name_test, new String[]{"13688006635"}, title, desc, pic_url, detail_url);
}
}
参考:
1.钉钉机器人API介绍
https://ding-doc.dingtalk.com/document#/org-dev-guide/qf2nxq
2.markdown基础
https://www.jianshu.com/p/191d1e21f7ed/