使用教程:实现服务群发送消息
预期效果
消息发送


流程简介
1.
2.
3.
4.
步骤一:创建企业内部应用
步骤二:获取AppKey和AppSecret

步骤三:添加接口权限

步骤四:获取应用访问凭证accessToken
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()));
}
**步骤五:**调用服务端服务群相关API
1.
public void groupsCreate() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkservice_group_1_0.Client client = new com.aliyun.dingtalkservice_group_1_0.Client(config);
CreateGroupHeaders createGroupHeaders = new CreateGroupHeaders();
createGroupHeaders.xAcsDingtalkAccessToken = "accessToken";
CreateGroupRequest createGroupRequest = new CreateGroupRequest()
.setGroupBizId("serviceGroup202211101001")
.setOpenTeamId("xGDx**ZXlkiE")
.setOpenGroupSetId("3DPxe***VkwiE")
.setGroupName("场景服务群")
.setOwnerStaffId("manager7675")
.setMemberStaffIds(java.util.Arrays.asList(
"manager7675","01472825524039877041"
))
.setGroupTagNames(java.util.Arrays.asList(
"tag"
));
try {
CreateGroupResponse groupWithOptions = client.createGroupWithOptions(createGroupRequest, createGroupHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(groupWithOptions.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 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)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.code);
System.out.println(err.message);
}
}
}
2.
public void serviceGroupMember() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkservice_group_1_0.Client client = new com.aliyun.dingtalkservice_group_1_0.Client(config);
com.aliyun.dingtalkservice_group_1_0.models.AddMemberToServiceGroupHeaders addMemberToServiceGroupHeaders = new com.aliyun.dingtalkservice_group_1_0.models.AddMemberToServiceGroupHeaders();
addMemberToServiceGroupHeaders.xAcsDingtalkAccessToken = "accessToken";
com.aliyun.dingtalkservice_group_1_0.models.AddMemberToServiceGroupRequest addMemberToServiceGroupRequest = new com.aliyun.dingtalkservice_group_1_0.models.AddMemberToServiceGroupRequest()
.setOpenTeamId("xGDx**ZXlkiE")
.setOpenConversationId("cidn0Wqg****znMOiEmpcJCpQ==")
.setUserIds(java.util.Arrays.asList(
"01296106445126923197"
));
try {
AddMemberToServiceGroupResponse addMemberToServiceGroupResponse = client.addMemberToServiceGroupWithOptions(addMemberToServiceGroupRequest, addMemberToServiceGroupHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(addMemberToServiceGroupResponse.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 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)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.code);
System.out.println(err.message);
}
}
}
3.
public void messagesSend() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkservice_group_1_0.Client client = new com.aliyun.dingtalkservice_group_1_0.Client(config);
SendServiceGroupMessageHeaders sendServiceGroupMessageHeaders = new SendServiceGroupMessageHeaders();
sendServiceGroupMessageHeaders.xAcsDingtalkAccessToken = "accessToken";
SendServiceGroupMessageRequest sendServiceGroupMessageRequest = new SendServiceGroupMessageRequest()
.setTargetOpenConversationId("cidn0Wqg****znMOiEmpcJCpQ==")
.setTitle("服务提醒")
.setAtUnionIds(Arrays.asList("E9CS6Xu5*****VOO905eN7QiEiE"))
.setReceiverUnionIds(Arrays.asList("E9CS6Xu5*****VOO905eN7QiEiE"))
.setContent("#### 杭州天气 \n> 9度,西北风1级,空气良89,相对温度73%\n> \n> ###### 10点20分发布 [天气](https://www.dingtalk.com) \n")
.setMessageType("MARKDOWN");
try {
SendServiceGroupMessageResponse sendServiceGroupMessageResponse = client.sendServiceGroupMessageWithOptions(sendServiceGroupMessageRequest, sendServiceGroupMessageHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(sendServiceGroupMessageResponse.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 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)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.code);
System.out.println(err.message);
}
}
}
public void tasksSend() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkservice_group_1_0.Client client = new com.aliyun.dingtalkservice_group_1_0.Client(config);
SendMsgByTaskHeaders sendMsgByTaskHeaders = new SendMsgByTaskHeaders();
sendMsgByTaskHeaders.xAcsDingtalkAccessToken = "accessToken";
SendMsgByTaskRequest.SendMsgByTaskRequestMessageContentBtns messageContentBtns0 = new SendMsgByTaskRequest.SendMsgByTaskRequestMessageContentBtns()
.setActionURL("https://www.dingtalk.com")
.setTitle("标题1");
SendMsgByTaskRequest.SendMsgByTaskRequestMessageContentBtns messageContentBtns1 = new SendMsgByTaskRequest.SendMsgByTaskRequestMessageContentBtns()
.setActionURL("https://www.dingtalk.com")
.setTitle("标题2");
SendMsgByTaskRequest.SendMsgByTaskRequestMessageContent messageContent = new SendMsgByTaskRequest.SendMsgByTaskRequestMessageContent()
.setAtAll(false)
.setAtActiveUser(false)
.setTitle("群发消息测试")
.setMessageType("ACTIONCARD")
.setContent("群发任务")
.setImages(Arrays.asList("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"))
.setBtns(Arrays.asList(messageContentBtns0,messageContentBtns1))
.setTop(false)
.setRemind(false);
SendMsgByTaskRequest.SendMsgByTaskRequestQueryGroup queryGroup = new SendMsgByTaskRequest.SendMsgByTaskRequestQueryGroup()
.setQueryType("AIMED")
.setOpenConversationIds(Arrays.asList("cidn0Wqg****znMOiEmpcJCpQ==","cidBHpU+/eD0****TQnKJLw=="))
.setGroupTagNames(Arrays.asList("tag"))
.setOpenGroupSetId("xGDx**ZXlkiE");
SendMsgByTaskRequest.SendMsgByTaskRequestSendConfig sendConfig = new SendMsgByTaskRequest.SendMsgByTaskRequestSendConfig()
.setSendType("INSTANT")
.setNeedUrlTrack(false);
SendMsgByTaskRequest sendMsgByTaskRequest = new SendMsgByTaskRequest()
.setOpenTeamId("xGDx**ZXlkiE")
.setTaskName("群发任务测试")
.setMessageContent(messageContent)
.setQueryGroup(queryGroup)
.setSendConfig(sendConfig);
try {
SendMsgByTaskResponse sendMsgByTaskResponse = client.sendMsgByTaskWithOptions(sendMsgByTaskRequest, sendMsgByTaskHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(sendMsgByTaskResponse.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 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)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.code);
System.out.println(err.message);
}
}
}
4.
public void queryActiveUsers() throws Exception {
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkservice_group_1_0.Client client = new com.aliyun.dingtalkservice_group_1_0.Client(config);
QueryActiveUsersHeaders queryActiveUsersHeaders = new QueryActiveUsersHeaders();
queryActiveUsersHeaders.xAcsDingtalkAccessToken = "accessToken";
QueryActiveUsersRequest queryActiveUsersRequest = new QueryActiveUsersRequest()
.setOpenTeamId("xGDx**ZXlkiE")
.setOpenConversationId("cidn0Wqg****znMOiEmpcJCpQ==");
try {
QueryActiveUsersResponse queryActiveUsersResponse = client.queryActiveUsersWithOptions(queryActiveUsersRequest, queryActiveUsersHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(queryActiveUsersResponse.getBody()));
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 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)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
System.out.println(err.code);
System.out.println(err.message);
}
}
}
修改于 2024-01-23 02:50:54