Req-3117 新增API,发送钉钉群消息

This commit is contained in:
zhongpeng 2024-11-14 19:27:06 +08:00
parent e2f2429d97
commit 23188912d6
9 changed files with 692 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package cn.axzo.riven.client.feign;
import cn.axzo.framework.domain.web.result.ApiResult;
import cn.axzo.riven.client.req.DingDingSendRebootGroupMsgReq;
import cn.axzo.riven.client.req.DingDingSendRebootMsgReq;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
@ -13,10 +14,23 @@ import org.springframework.web.bind.annotation.RequestBody;
public interface DingDingMsgApi {
/**
* 发送钉钉消息
*
* 正对于单个人进行发送
* @param req
* @return
*/
@PostMapping("/api/dingDing/reboot/msg/send")
ApiResult<Void> sendRebootMsg(@RequestBody DingDingSendRebootMsgReq req);
/**
* 发送钉钉消息
* 给单个群发送
* 目前只能给一个APPKEY发送后续扩展支持多个
* 目前只能支持2个横着的按钮发送
* @param req
* @return
*/
@PostMapping("/api/dingDing/reboot/groupMsg/send")
ApiResult<Void> sendRebootGroupMsg(@RequestBody DingDingSendRebootGroupMsgReq req);
}

View File

@ -0,0 +1,36 @@
package cn.axzo.riven.client.model;
import cn.axzo.riven.client.common.enums.DingTalkMsgTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
*
* @author zhongpeng
* @since 2024-11-14 14:01
*/
@Data
@AllArgsConstructor
public class SampleActionCard6 implements ReplyMessage<SampleActionCard6> {
private final String title;
private final String text;
private final String buttonTitle1;
private final String buttonUrl1;
private final String buttonTitle2;
private final String buttonUrl2;
@Override
public DingTalkMsgTypeEnum msgType() {
return DingTalkMsgTypeEnum.sampleActionCard6;
}
@Override
public SampleActionCard6 messageBody() {
return this;
}
public static SampleActionCard6 from(String title, String text,String buttonTitle1, String buttonUrl1, String buttonTitle2, String buttonUrl2) {
return new SampleActionCard6(title, text, buttonTitle1, buttonUrl1, buttonTitle2, buttonUrl2);
}
}

View File

@ -0,0 +1,25 @@
package cn.axzo.riven.client.req;
import cn.axzo.riven.client.common.enums.DingTalkMsgTypeEnum;
import cn.axzo.riven.client.model.ReplyMessage;
import lombok.Data;
import java.util.List;
/**
* @author zhognpeng@axzo.cn
*/
@Data
public class DingDingSendRebootGroupMsgReq {
/**
* 消息类型
* 目前支持sampleActionCard6其他待补充
*/
private DingTalkMsgTypeEnum msgType;
/**
* 消息内容
* 目前支持SampleActionCard6其他待补充
* 其中的消息markdown格式的消息具体见https://open.dingtalk.com/document/orgapp/message-types-and-data-format#
*/
private ReplyMessage replyMessage;
}

View File

@ -0,0 +1,28 @@
package cn.axzo.riven.config;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
* 后续其他钉钉配置可以收口到这里
* @author zhongpeng@axzo.cn
*/
@Configuration
@RefreshScope
@Getter
public class DingDingRebootMainConfig {
@Value("${reboot.operation.appKey:dingx9pejjkh8whnaqkw}")
private String appKey;
@Value("${reboot.operation.appSecret:IViT8sL_VVZ03gRc3oIvbtzS0DsTKUI9pLgXSNh9FErHgzFR_97liY5g6SpmJpjk}")
private String appSecret;
@Value("${reboot.operation.agentId:3286362019}")
private Long agentId;
//机器人的ID
@Value("${reboot.operation.rebootCode:dingx9pejjkh8whnaqkw}")
private String rebootCode;
//群的ID可以升级为支持多个群
@Value("${reboot.operation.openConversationId:cidAy86bIrB513xUaCOmvD/Hg==}")
private String openConversationId;
}

View File

@ -2,9 +2,11 @@ package cn.axzo.riven.controller;
import cn.axzo.framework.domain.web.result.ApiResult;
import cn.axzo.riven.client.feign.DingDingMsgApi;
import cn.axzo.riven.client.req.DingDingSendRebootGroupMsgReq;
import cn.axzo.riven.client.req.DingDingSendRebootMsgReq;
import cn.axzo.riven.service.DingDingRebootService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
/**
@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequiredArgsConstructor
@Component
public class DingDingMsgController implements DingDingMsgApi {
private final DingDingRebootService dingDingRebootService;
@ -20,4 +23,10 @@ public class DingDingMsgController implements DingDingMsgApi {
dingDingRebootService.sendCorpRebbootMsg(req);
return ApiResult.ok();
}
@Override
public ApiResult<Void> sendRebootGroupMsg(DingDingSendRebootGroupMsgReq req) {
dingDingRebootService.sendCorpRebootGroupMsg(req);
return ApiResult.ok();
}
}

View File

@ -1,10 +1,12 @@
package cn.axzo.riven.manger;
import cn.axzo.framework.domain.web.code.BaseCode;
import cn.axzo.riven.client.req.DingDingSendRebootGroupMsgReq;
import cn.axzo.riven.common.constants.RedisKeyConstant;
import cn.axzo.riven.common.util.Throws;
import cn.azxo.framework.common.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketResponse;
import com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketResponseBody;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
@ -14,6 +16,7 @@ import com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenResponseBody;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest;
import com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
@ -143,4 +146,27 @@ public class DingDingNewSdkManger {
LogUtil.error(LogUtil.ErrorLevel.P1, LogUtil.ErrorType.ERROR_THIRD_SERVICE, "RPC钉钉系统异常,api={},", api, e);
}
}
public void sendGroupMessage(String accessToken, String rebootCode,String openConversationId, DingDingSendRebootGroupMsgReq req){
com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendHeaders orgGroupSendHeaders = new com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendHeaders();
orgGroupSendHeaders.xAcsDingtalkAccessToken = accessToken;
com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendRequest orgGroupSendRequest = new com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendRequest();
orgGroupSendRequest.setRobotCode(rebootCode);
orgGroupSendRequest.setOpenConversationId(openConversationId);
orgGroupSendRequest.setMsgKey(req.getMsgType().toString());
orgGroupSendRequest.setMsgParam(req.getReplyMessage().toJson());
try {
OrgGroupSendResponse response = dingTalkRobotClient.orgGroupSendWithOptions(orgGroupSendRequest, orgGroupSendHeaders, new com.aliyun.teautil.models.RuntimeOptions());
log.info("sendGroupMessage, response={}", JSON.toJSONString(response));
} catch (Exception e) {
log.error("sendGroupMessage error", e);
throw Throws.bizDefaultException();
}
}
}

View File

@ -2,11 +2,13 @@ package cn.axzo.riven.service;
import cn.axzo.framework.data.mybatisplus.model.BaseEntity;
import cn.axzo.framework.domain.web.code.BaseCode;
import cn.axzo.riven.client.req.DingDingSendRebootGroupMsgReq;
import cn.axzo.riven.client.req.DingDingSendRebootMsgReq;
import cn.axzo.riven.common.constants.DingDingJumpConstant;
import cn.axzo.riven.common.util.Throws;
import cn.axzo.riven.config.DeliverConfig;
import cn.axzo.riven.config.DingDingRebootConfig;
import cn.axzo.riven.config.DingDingRebootMainConfig;
import cn.axzo.riven.gateway.OrganizationalNodeUserApiGateway;
import cn.axzo.riven.manger.DingDingNewSdkManger;
import cn.axzo.riven.repository.entity.ThirdPartyUser;
@ -38,6 +40,10 @@ public class DingDingRebootService {
private DingDingRebootConfig dingRebootConfig;
@Autowired
private DeliverConfig deliverConfig;
@Autowired
private DingDingRebootMainConfig dingDingRebootMainConfig;
@Autowired
private OrganizationalNodeUserApiGateway organizationalNodeUserApiGateway;
@ -46,12 +52,12 @@ public class DingDingRebootService {
// 非生产环境mock
String env = SpringUtil.getActiveProfile();
if(!"master".equals(env)) {
if (!"master".equals(env)) {
// 取交集
List<String> tmp = Arrays.asList(dingRebootConfig.getMockPersonIds().split(","));
List<Long> mockIds = tmp.stream().map(Long::valueOf).collect(Collectors.toList());
Collection<Long> intersection = org.apache.commons.collections4.CollectionUtils.intersection(mockIds, req.getPersonIds());
if(CollectionUtils.isEmpty(intersection)) {
if (CollectionUtils.isEmpty(intersection)) {
return;
}
req.setPersonIds(new ArrayList<>(intersection));
@ -88,4 +94,12 @@ public class DingDingRebootService {
dingDingNewSdkManger.robotoToMessagesBatchSend(accessToken.getAccessToken(), dingRebootConfig.getRebootCode(), dingUserIds.subList(i, i + sendLength), req.getMsgType(), JSONUtil.toJsonStr(req.getSampleActionCard()));
}
}
public void sendCorpRebootGroupMsg(DingDingSendRebootGroupMsgReq req) {
GetAccessTokenResponseBody accessToken = dingDingNewSdkManger.getAccessToken(dingDingRebootMainConfig.getAppKey(), dingDingRebootMainConfig.getAppSecret());
dingDingNewSdkManger.sendGroupMessage(accessToken.getAccessToken(), dingDingRebootMainConfig.getRebootCode(), dingDingRebootMainConfig.getOpenConversationId(), req);
}
}

View File

@ -0,0 +1,54 @@
package cn.axzo.riven.test;
import cn.axzo.basics.profiles.api.UserProfileServiceApi;
import cn.axzo.riven.Application;
import cn.axzo.riven.client.common.enums.DingTalkMsgTypeEnum;
import cn.axzo.riven.client.model.SampleActionCard6;
import cn.axzo.riven.client.req.DingDingSendRebootGroupMsgReq;
import cn.axzo.riven.config.FeignConfiguration;
import cn.axzo.riven.controller.DingDingMsgController;
import cn.axzo.riven.test.taizhou.AxzoUserProfileServiceApiTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
/**
* @author zhongpeng
* @date 2024/11/14 18:17
*/
@SpringBootTest(classes = Application.class)
public class DingDingMsgControllerTest {
@Resource
private DingDingMsgController dingDingMsgController;
@Test
public void test() {
DingDingSendRebootGroupMsgReq req = new DingDingSendRebootGroupMsgReq();
req.setMsgType(DingTalkMsgTypeEnum.sampleActionCard6);
String title = "标题";
String text = "内容 \n ## 标题1 你好";
String buttonTitle1 = "百度";
String buttonUrl1 = "http://www.baidu.com";
String buttonTitle2 = "MQ";
String buttonUrl2 = "https://rocketmq.apache.org/zh/docs/domainModel/02topic/";
SampleActionCard6 sampleActionCard6 = new SampleActionCard6(title,text,buttonTitle1,buttonUrl1,buttonTitle2,buttonUrl2);
req.setReplyMessage(sampleActionCard6);
dingDingMsgController.sendRebootGroupMsg(req);
}
}

View File

@ -0,0 +1,483 @@
package cn.axzo.riven.test;
import cn.axzo.riven.client.model.ReplyMessage;
import cn.axzo.riven.client.model.SampleActionCard6;
import cn.axzo.riven.client.req.DingDingActionCardDto;
import cn.axzo.riven.client.req.DingDingBtnJsonListDto;
import cn.axzo.riven.common.util.Throws;
import cn.axzo.riven.manger.DingDingNewSdkManger;
import cn.azxo.framework.common.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkim_1_0.Client;
import com.aliyun.dingtalkim_1_0.models.SendRobotMessageResponse;
import com.aliyun.dingtalkrobot_1_0.models.GetBotListInGroupResponse;
import com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendResponse;
import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.script.ReactiveScriptExecutor;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author zhongpeng
* @date 2024/11/14 14:58
*/
public class DingDingTest {
private static final String appKey = "dingx9pejjkh8whnaqkw";
private static final String appSecret = "IViT8sL_VVZ03gRc3oIvbtzS0DsTKUI9pLgXSNh9FErHgzFR_97liY5g6SpmJpjk";
private static final String agentId = "3286362019";
private static final String robotCode = "dingx9pejjkh8whnaqkw";
private static final String token = "8b154088e6f235fa8155daffca12b6cc";
private static final String openConversationId = "cidAy86bIrB513xUaCOmvD/Hg==";
@Test
public void testGetAccessToken() throws Exception {
OapiGettokenResponse response = getToken();
System.out.println(JSON.toJSONString(response));
}
private OapiGettokenResponse getToken() throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appKey);
request.setAppsecret(appSecret);
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
System.out.println(JSON.toJSONString(response));
return response;
}
@Test
public void test2() throws ApiException {
String token = "8b154088e6f235fa8155daffca12b6cc";
DingDingActionCardDto actionCardDto = new DingDingActionCardDto();
actionCardDto.setTitle("标题");
//消息内容
actionCardDto.setMarkdown("消息内容");
actionCardDto.setSingleTitle(null);
actionCardDto.setSingleUrl(null);
DingDingBtnJsonListDto btnJson1 = new DingDingBtnJsonListDto();
btnJson1.setTitle("百度");
btnJson1.setActionUrl("www.baidu.com");
DingDingBtnJsonListDto btnJson2 = new DingDingBtnJsonListDto();
btnJson2.setTitle("MQ文档");
btnJson2.setActionUrl("https://rocketmq.apache.org/zh/docs/domainModel/02topic/");
actionCardDto.setBtnJsonList(new ArrayList<>());
actionCardDto.getBtnJsonList().add(btnJson1);
actionCardDto.getBtnJsonList().add(btnJson2);
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(Long.valueOf(agentId));
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
msg.getActionCard().setTitle(actionCardDto.getTitle());
msg.getActionCard().setMarkdown(actionCardDto.getMarkdown());
msg.getActionCard().setSingleTitle(actionCardDto.getSingleTitle());
msg.getActionCard().setSingleUrl(actionCardDto.getSingleUrl());
msg.getActionCard().setBtnOrientation(actionCardDto.getBtnOrientation());
if (!CollectionUtils.isEmpty(actionCardDto.getBtnJsonList())) {
List<OapiMessageCorpconversationAsyncsendV2Request.BtnJsonList> btnJsonList = new ArrayList<>();
actionCardDto.getBtnJsonList().forEach(item -> {
OapiMessageCorpconversationAsyncsendV2Request.BtnJsonList btnJsonList1 = new OapiMessageCorpconversationAsyncsendV2Request.BtnJsonList();
btnJsonList1.setActionUrl(item.getActionUrl());
btnJsonList1.setTitle(item.getTitle());
btnJsonList.add(btnJsonList1);
});
msg.getActionCard().setBtnJsonList(btnJsonList);
//0竖直排列
//1横向排列
msg.getActionCard().setBtnOrientation("1");
}
msg.setMsgtype("action_card");
req.setMsg(msg);
req.setToAllUser(true);
req.getHeaderMap().put("chatId", "1231");
OapiMessageCorpconversationAsyncsendV2Response rsp;
try {
rsp = client.execute(req, token);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
System.out.println(JSON.toJSONString(rsp));
}
@Test
public void test() throws ApiException {
String token = "8b154088e6f235fa8155daffca12b6cc";
DingDingActionCardDto actionCardDto = new DingDingActionCardDto();
actionCardDto.setTitle("标题");
//消息内容
actionCardDto.setMarkdown("消息内容");
actionCardDto.setSingleTitle(null);
actionCardDto.setSingleUrl(null);
DingDingBtnJsonListDto btnJson1 = new DingDingBtnJsonListDto();
btnJson1.setTitle("百度");
btnJson1.setActionUrl("www.baidu.com");
DingDingBtnJsonListDto btnJson2 = new DingDingBtnJsonListDto();
btnJson2.setTitle("MQ文档");
btnJson2.setActionUrl("https://rocketmq.apache.org/zh/docs/domainModel/02topic/");
actionCardDto.setBtnJsonList(new ArrayList<>());
actionCardDto.getBtnJsonList().add(btnJson1);
actionCardDto.getBtnJsonList().add(btnJson2);
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartbot/msg/push");
OapiSmartbotMsgPushRequest req = new OapiSmartbotMsgPushRequest();
OapiSmartbotMsgPushRequest.Msg msg = new OapiSmartbotMsgPushRequest.Msg();
msg.setActionCard(new OapiSmartbotMsgPushRequest.ActionCard());
msg.getActionCard().setTitle(actionCardDto.getTitle());
msg.getActionCard().setMarkdown(actionCardDto.getMarkdown());
msg.getActionCard().setSingleTitle(actionCardDto.getSingleTitle());
msg.getActionCard().setSingleUrl(actionCardDto.getSingleUrl());
msg.getActionCard().setBtnOrientation(actionCardDto.getBtnOrientation());
if (!CollectionUtils.isEmpty(actionCardDto.getBtnJsonList())) {
List<OapiSmartbotMsgPushRequest.BtnJson> btnJsonList = new ArrayList<>();
actionCardDto.getBtnJsonList().forEach(item -> {
OapiSmartbotMsgPushRequest.BtnJson btnJsonList1 = new OapiSmartbotMsgPushRequest.BtnJson();
btnJsonList1.setActionUrl(item.getActionUrl());
btnJsonList1.setTitle(item.getTitle());
btnJsonList.add(btnJsonList1);
});
msg.getActionCard().setBtnJsonList(btnJsonList);
}
msg.setMsgtype("action_card");
req.setMsg(msg);
req.setUserIdList(null);
req.setToAllUser(null);
OapiSmartbotMsgPushResponse rsp;
try {
rsp = client.execute(req, token);
} catch (Exception e) {
LogUtil.error(LogUtil.ErrorLevel.P1, LogUtil.ErrorType.ERROR_THIRD_SERVICE, "RPC钉钉系统异常,api={},", "smartBotMsgPush", e);
throw Throws.bizDefaultException();
}
System.out.println(JSON.toJSONString(rsp));
}
@Test
public void test3() {
String token = "8b154088e6f235fa8155daffca12b6cc";
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/get");
OapiChatGetRequest req = new OapiChatGetRequest();
req.setHttpMethod("GET");
OapiChatGetResponse rsp = client.execute(req, token);
System.out.println(JSON.toJSONString(rsp));
} catch (ApiException e) {
e.printStackTrace();
}
}
@Test
public void createGroup() {
String token = "8b154088e6f235fa8155daffca12b6cc";
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/get");
OapiChatGetRequest req = new OapiChatGetRequest();
req.setHttpMethod("GET");
OapiChatGetResponse rsp = client.execute(req, token);
System.out.println(JSON.toJSONString(rsp));
} catch (ApiException e) {
e.printStackTrace();
}
}
//自定义机器人发送消息
@Test
public void test4() throws ApiException {
String token = "8b154088e6f235fa8155daffca12b6cc";
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send");
OapiRobotSendRequest req = new OapiRobotSendRequest();
OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
actioncard.setBtns(new ArrayList<>());
actioncard.setTitle("标题");
actioncard.setText("内容");
OapiRobotSendRequest.Btns btns1 = new OapiRobotSendRequest.Btns();
btns1.setTitle("百度");
btns1.setActionURL("http://www.baidu.com");
OapiRobotSendRequest.Btns btns2 = new OapiRobotSendRequest.Btns();
btns2.setTitle("MQ文档");
btns2.setActionURL("https://rocketmq.apache.org/zh/docs/domainModel/02topic/");
actioncard.getBtns().add(btns1);
actioncard.getBtns().add(btns2);
req.setMarkdown(new OapiRobotSendRequest.Markdown());
req.setMsgtype("action_card");
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
at.setAtMobiles(Arrays.asList("15008222057"));
req.setAt(at);
OapiRobotSendResponse rsp = client.execute(req, token);
System.out.println(rsp.getBody());
}
public static com.aliyun.dingtalkim_1_0.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
config.protocol = "https";
config.regionId = "central";
return new com.aliyun.dingtalkim_1_0.Client(config);
}
//企业机器人发送消息无权限
@Test
public void test5() throws Exception {
String token = "8b154088e6f235fa8155daffca12b6cc";
com.aliyun.dingtalkim_1_0.Client client = createClient();
com.aliyun.dingtalkim_1_0.models.SendRobotMessageHeaders sendRobotMessageHeaders = new com.aliyun.dingtalkim_1_0.models.SendRobotMessageHeaders();
sendRobotMessageHeaders.xAcsDingtalkAccessToken = token;
com.aliyun.dingtalkim_1_0.models.SendRobotMessageRequest sendRobotMessageRequest = new com.aliyun.dingtalkim_1_0.models.SendRobotMessageRequest();
//发送多个群
sendRobotMessageRequest.setOpenConversationIds(Arrays.asList("cidAy86bIrB513xUaCOmvD/Hg=="));
sendRobotMessageRequest.setMsgType("action_card");
sendRobotMessageRequest.setMsgContent("消息内容");
try {
SendRobotMessageResponse response = client.sendRobotMessageWithOptions(sendRobotMessageRequest, sendRobotMessageHeaders, new com.aliyun.teautil.models.RuntimeOptions());
System.out.println(JSON.toJSONString(response));
} catch (TeaException err) {
err.printStackTrace();
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code message 属性可帮助开发定位问题
}
} catch (Exception _err) {
_err.printStackTrace();
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 属性可帮助开发定位问题
}
}
// OapiRobotSendRequest req = new OapiRobotSendRequest();
//
// OapiRobotSendRequest.Actioncard actioncard = new OapiRobotSendRequest.Actioncard();
//
// actioncard.setBtns(new ArrayList<>());
// actioncard.setTitle("标题");
// actioncard.setText("内容");
//
// OapiRobotSendRequest.Btns btns1 = new OapiRobotSendRequest.Btns();
// btns1.setTitle("百度");
// btns1.setActionURL("http://www.baidu.com");
// OapiRobotSendRequest.Btns btns2 = new OapiRobotSendRequest.Btns();
//
// btns2.setTitle("MQ文档");
// btns2.setActionURL("https://rocketmq.apache.org/zh/docs/domainModel/02topic/");
// actioncard.getBtns().add(btns1);
// actioncard.getBtns().add(btns2);
//
// req.setMarkdown(new OapiRobotSendRequest.Markdown());
// req.setMsgtype("action_card");
// OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
// at.setAtMobiles(Arrays.asList("15008222057"));
// req.setAt(at);
//
// OapiRobotSendResponse rsp = client.execute(req, token);
// System.out.println(rsp.getBody());
}
//企业机器人发送消息无权限
@Test
public void test6() throws Exception {
String token = "8b154088e6f235fa8155daffca12b6cc";
com.aliyun.dingtalkim_1_0.Client client = createClient();
com.aliyun.dingtalkim_1_0.models.SendRobotMessageHeaders sendRobotMessageHeaders = new com.aliyun.dingtalkim_1_0.models.SendRobotMessageHeaders();
sendRobotMessageHeaders.xAcsDingtalkAccessToken = token;
com.aliyun.dingtalkim_1_0.models.SendRobotMessageRequest sendRobotMessageRequest = new com.aliyun.dingtalkim_1_0.models.SendRobotMessageRequest();
//发送多个群
sendRobotMessageRequest.setOpenConversationIds(Arrays.asList("cidAy86bIrB513xUaCOmvD/Hg=="));
sendRobotMessageRequest.setMsgType("action_card");
sendRobotMessageRequest.setMsgContent("消息内容");
try {
SendRobotMessageResponse response = client.sendRobotMessageWithOptions(sendRobotMessageRequest, sendRobotMessageHeaders, new com.aliyun.teautil.models.RuntimeOptions());
System.out.println(JSON.toJSONString(response));
} catch (TeaException err) {
err.printStackTrace();
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code message 属性可帮助开发定位问题
}
} catch (Exception _err) {
_err.printStackTrace();
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 属性可帮助开发定位问题
}
}
}
public static com.aliyun.dingtalkrobot_1_0.Client createClient1() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
config.protocol = "https";
config.regionId = "central";
return new com.aliyun.dingtalkrobot_1_0.Client(config);
}
//企业机器人通过群的openConversationId获取群的机器人
@Test
public void test7() throws Exception {
String token = "8b154088e6f235fa8155daffca12b6cc";
com.aliyun.dingtalkrobot_1_0.Client client = createClient1();
com.aliyun.dingtalkrobot_1_0.models.GetBotListInGroupHeaders getBotListInGroupHeaders = new com.aliyun.dingtalkrobot_1_0.models.GetBotListInGroupHeaders();
getBotListInGroupHeaders.xAcsDingtalkAccessToken = token;
com.aliyun.dingtalkrobot_1_0.models.GetBotListInGroupRequest getBotListInGroupRequest = new com.aliyun.dingtalkrobot_1_0.models.GetBotListInGroupRequest();
getBotListInGroupRequest.setOpenConversationId(openConversationId);
try {
GetBotListInGroupResponse response = client.getBotListInGroupWithOptions(getBotListInGroupRequest, getBotListInGroupHeaders, new com.aliyun.teautil.models.RuntimeOptions());
System.out.println(JSON.toJSONString(response));
} catch (TeaException err) {
err.printStackTrace();
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code message 属性可帮助开发定位问题
}
} catch (Exception _err) {
_err.printStackTrace();
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 属性可帮助开发定位问题
}
}
}
//企业机器人发送消息指定群发送带有2个按钮的消息卡片
@Test
public void test8() throws Exception {
com.aliyun.dingtalkrobot_1_0.Client client = createClient1();
com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendHeaders orgGroupSendHeaders = new com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendHeaders();
orgGroupSendHeaders.xAcsDingtalkAccessToken = token;
com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendRequest orgGroupSendRequest = new com.aliyun.dingtalkrobot_1_0.models.OrgGroupSendRequest();
orgGroupSendRequest.setRobotCode(robotCode);
orgGroupSendRequest.setOpenConversationId(openConversationId);
orgGroupSendRequest.setMsgKey("sampleActionCard");
// SampleActionCard6 replyMessage = new SampleActionCard6("标题","内容");
JSONObject actionCard = new JSONObject();
actionCard.put("title","标题");
actionCard.put("text","内容");
actionCard.put("buttonTitle1","百度");
actionCard.put("buttonUrl1","http://www.baidu.com");
actionCard.put("buttonTitle2","MQ");
actionCard.put("buttonUrl2","https://rocketmq.apache.org/zh/docs/domainModel/02topic/");
orgGroupSendRequest.setMsgParam(actionCard.toJSONString());
try {
OrgGroupSendResponse response = client.orgGroupSendWithOptions(orgGroupSendRequest, orgGroupSendHeaders, new com.aliyun.teautil.models.RuntimeOptions());
System.out.println(JSON.toJSONString(response));
} catch (TeaException err) {
err.printStackTrace();
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code message 属性可帮助开发定位问题
}
} catch (Exception _err) {
_err.printStackTrace();
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 属性可帮助开发定位问题
}
}
}
}