首先在pom.xml文件中添加依赖,也可以到钉钉官网下载
com.aliyun
dingtalk
1.1.88
具体工具类代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.aliyun.dingtalkrobot_1_0.Client;
import com.taobao.api.ApiException;
public class DingUtil {
//企业凭证,两小时一更新
private static String enterpriseToken = "6082435a58393675b4ff83d6630f53cc";
//应用凭证
private static String appkey = "XXXXXXXXXXXXXXXXXX";
private static String appsecret = "XXXXXXXXXXXXXXXXX";
//获取token,每两小时失效
private static void gettoken() {
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey(appkey);
req.setAppsecret(appsecret);
req.setHttpMethod("GET");
OapiGettokenResponse rsp = client.execute(req);
System.out.println("token:" + rsp.getBody());
JSonObject json = JSON.parseObject(rsp.getBody());
enterpriseToken = json.getString("access_token");
} catch (ApiException e) {
e.printStackTrace();
}
}
public static Client createClient() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
return new Client(config);
}
public static void createDingNotify(String dingUserId,String content) throws Exception{
com.aliyun.dingtalkrobot_1_0.Client client = createClient();
BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders();
batchSendOTOHeaders.xAcsDingtalkAccessToken = enterpriseToken;
BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest()
.setRobotCode(appkey)//机器人appkey
.setUserIds(java.util.Arrays.asList(
dingUserId
))
.setMsgKey("officialTextMsg")
.setMsgParam("{ "content": ""+content+"" }");
try {
client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
}
catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
System.err.println(err.code+":"+err.message);
// err 中含有 code 和 message 属性,可帮助开发定位问题
//企业凭证enterpriseToken不合法导致出错时获取新企业凭证并重试
if(err.code.equals("InvalidAuthentication")){
gettoken();
createDingNotify(dingUserId,content);
}
}
}
catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
System.err.println(err.code+":"+err.message);
// err 中含有 code 和 message 属性,可帮助开发定位问题
}
}
}
public static void main(String[] args_) throws Exception {
createDingNotify("[钉钉用户id]","[发送的消息内容]");
}
}
需要替换应用凭证 appkey 和 appsecret ,在钉钉后台创建应用后钉钉会给出,注意必须使用机器人类型的应用的凭证



