public void getAccessToken() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
GetAccessTokenRequest accessTokenRequest = new GetAccessTokenRequest()
.setAppKey("din*********hgn")
.setAppSecret("9G_O************mBkhgGIO");
GetAccessTokenResponse accessToken = client.getAccessToken(accessTokenRequest);
System.out.println(JSON.toJSONString(accessToken.getBody()));
}
public void sendMessage() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(config);
SendMessageHeaders sendMessageHeaders = new SendMessageHeaders();
sendMessageHeaders.xAcsDingtalkAccessToken = "<your access token>";
SendMessageRequest sendMessageRequest = new SendMessageRequest()
.setSenderId("1107****2120")
.setReceiverId("1745****8777")
.setOpenConversationId("14da****2760")
.setMessageType("text")
.setMessage("{ \"text\": { \"content\":\"hello world\" } }");
try {
SendMessageResponse response = client.sendMessageWithOptions(sendMessageRequest, sendMessageHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(response.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
System.out.println(err.code);
System.out.println(err.message);
}
} 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.out.println(err.code);
System.out.println(err.message);
}
}
}
authCode
。public void sendDingMessage() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(config);
SendDingMessageHeaders sendDingMessageHeaders = new SendDingMessageHeaders();
sendDingMessageHeaders.xAcsDingtalkAccessToken = "<your access token>";
SendDingMessageRequest sendDingMessageRequest = new SendDingMessageRequest()
.setSenderId("1745****8777")
.setReceiverId("1107****2120")
.setOpenConversationId("14da****2760")
.setMessageType("text")
.setMessage("{\"text\": {\"content\": \"hello world\"} }")
.setCode("06f4****d1ec");
try {
SendDingMessageResponse response = client.sendDingMessageWithOptions(sendDingMessageRequest, sendDingMessageHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(response.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
System.out.println(err.code);
System.out.println(err.message);
}
} 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.out.println(err.code);
System.out.println(err.message);
}
}
}
public void sendRobotMessage() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(config);
SendRobotMessageHeaders sendRobotMessageHeaders = new SendRobotMessageHeaders();
sendRobotMessageHeaders.xAcsDingtalkAccessToken = "<your access token>";
SendRobotMessageRequest sendRobotMessageRequest = new SendRobotMessageRequest()
.setOpenConversationIds(java.util.Arrays.asList(
"1745****8777"
))
.setMsgType("text")
.setMsgContent("{ \"content\": \"我就是我, 是不一样的烟火\"}")
.setAtDingUserId("1107****2120")
.setAtAppUserId("1107****2120")
.setAtAll(true);
try {
SendRobotMessageResponse response = client.sendRobotMessageWithOptions(sendRobotMessageRequest, sendRobotMessageHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(response.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
System.out.println(err.code);
System.out.println(err.message);
}
} 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.out.println(err.code);
System.out.println(err.message);
}
}
}
public void queryUnReadMessage() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkim_1_0.Client client = new com.aliyun.dingtalkim_1_0.Client(config);
QueryUnReadMessageHeaders queryUnReadMessageHeaders = new QueryUnReadMessageHeaders();
queryUnReadMessageHeaders.xAcsDingtalkAccessToken = "<your access token>";
QueryUnReadMessageRequest queryUnReadMessageRequest = new QueryUnReadMessageRequest()
.setAppUserId("1107****2120")
.setOpenConversationIds(java.util.Arrays.asList(
"14da****2760"
));
try {
QueryUnReadMessageResponse response = client.queryUnReadMessageWithOptions(queryUnReadMessageRequest, queryUnReadMessageHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(response.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
System.out.println(err.code);
System.out.println(err.message);
}
} 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.out.println(err.code);
System.out.println(err.message);
}
}
}