文章目录
常见问题异常:“code”:“invalidParameter.robotCode.notExsit”,“message”:“机器人不存在”
原因:没有发布应用
{
"code":"invalidParameter.robotCode.notExsit",
"requestid":"76DABAC0-8ED5-7622-B692-B9E083CB09E9",
"message":"机器人不存在"
}
1、创建机器人应用
https://open-dev.dingtalk.com/fe/app#/corp/robot
2、开通机器人接口权限
3、一定要发布(否则无法调试调接口)
4、编写工具类
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dingtalk</artifactId>
<version>1.1.88</version>
</dependency>
DingTalkUtil
package com.liugx.dingtalk;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
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;
/**
* java 实现钉钉机器人单人私聊推送钉钉消息
* -添加依赖
* -获取应用凭证https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.3f99722fFLkPuA#/?devType=org&api=oauth2_1.0%23GetAccessToken
* -机器人发送消息
*
* @author liugx
* @version 1.0
* @date 2022/1/13 20:42
*/
public class DingTalkUtil {
private static String ACCESS_TOKEN = "";
private static String APP_KEY = "你的";
private static String APP_SECRET = "你的";
/**
* 初始化--权限Client
* @return Client
* @throws Exception
*/
public static com.aliyun.dingtalkoauth2_1_0.Client createAuthClient() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
return new com.aliyun.dingtalkoauth2_1_0.Client(config);
}
/**
* 初始化--机器人Client
*
* @return Client
* @throws Exception
*/
public static com.aliyun.dingtalkrobot_1_0.Client createClient() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
return new com.aliyun.dingtalkrobot_1_0.Client(config);
}
/**
* 获取并设置最新accessToken,每两小时失效
*/
public static void setAccessToken() throws Exception {
com.aliyun.dingtalkoauth2_1_0.Client client = createAuthClient();
GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
.setAppKey(APP_KEY)
.setAppSecret(APP_SECRET);;
try {
GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
//设置access_token
ACCESS_TOKEN = accessToken.getBody().accessToken;
} catch (TeaException err) {
System.out.println(err.message);
} catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
System.out.println(err.message);
}
}
/**
* 发送钉钉消息推送
*/
private void sendDingPusher() throws Exception {
setAccessToken();
com.aliyun.dingtalkrobot_1_0.Client client = createClient();
BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders();
batchSendOTOHeaders.xAcsDingtalkAccessToken = ACCESS_TOKEN;
BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest()
.setRobotCode(APP_KEY)
.setUserIds(java.util.Arrays.asList(
"01421230693825153972","manager6206"
))
.setMsgKey("sampleMarkdown")
.setMsgParam("{\"text\": \"hello text\",\"title\": \"hello title\"}");
try {
client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
} catch (TeaException err) {
if (err.code.equals("InvalidAuthentication")){
// access_token 过期或者不合法
setAccessToken();
sendDingPusher();
}
} catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
}
}
public static void main(String[] args) throws Exception {
new DingTalkUtil().sendDingPusher();
}
}