java对接钉钉 发送/更新 互动卡片消息
- 一、准备工作
- 二、发送互动卡片消息
- 三、更新互动消息卡片
先看下效果图
点击相应的积分按钮,就会给对应的申请人发送对应的积分(对接到我们自己的系统发送),审核完之后,按钮变成已审核。
接下来就开始吧!
一、准备工作
参考钉钉提供的 互动卡片消息发送流程
步骤一:创建消息模板
这里简单给出示例,具体可以参考钉钉给的示例
步骤二:注册卡片回调地址
需要告诉钉钉用户点击卡片后,要回调到我们自己系统的哪个接口
部分代码如下:
public void interactiveCardRegisterCallBack(String callBackUrl, Boolean forceUpdate) {try {DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/im/chat/scencegroup/interactivecard/callback/register");OapiImChatScencegroupInteractivecardCallbackRegisterRequest req = new OapiImChatScencegroupInteractivecardCallbackRegisterRequest();req.setCallbackUrl(callBackUrl);req.setForceUpdate(forceUpdate);OapiImChatScencegroupInteractivecardCallbackRegisterResponse registerRespone = client.execute(req, getAccessToken());if (registerRespone.isSuccess()) {logger.info("互动卡片回调注册成功!");} else {logger.info("互动卡片回调注册失败,原因:" + registerRespone.getErrmsg());}} catch (ApiException e) {logger.info("互动卡片回调注册失败,原因是:" + e.getErrMsg());}}
步骤三:创建群模板
因为卡片消息是基于场景群发送的,所以我们要先创建一个群模板,群模板里面包括:群插件和群机器人,二者的介绍可以参考场景群的开发文档 https://developers.dingtalk.com/document/chatgroup
创建群模板的流程钉钉给的已经很详细了,我这里就不多赘述了,但是要注意一点,创建完群模板要点一下灰度,这样才能使该群模板运用到其他场景群中。下图是创建好的模板
步骤四:给场景群启用群模板
有两种方式,第一种是根据群模板ID创建一个新的场景群,适用于还没有场景群的情况;第二种是给已存在的群设置群模板,其中调用api启用群模板的时候需要一个open_conversation_id(群id),感觉这里钉钉文档写的有问题
因为我们要查询已存在群的群id,按照提示,就只能调用查询群信息接口获取了对吧,但当我点进去发现
后来我仔细研究api文档后找到了一个获取群id的方法:通过chatid获取群信息,这个接口里面可以查询到open_conversation_id,但是需要chatid(群会话的ID)作为参数,而这个chatid可以通过调用前端接口获取到。
至此准备工作完成!
二、发送互动卡片消息
1.发送卡片消息接口,需要注意的是如果两个卡片消息的outTrackId一样的话,只会发一次
/*** 使用 Token 初始化账号Client* @return Client* @exception Exception*/public static Client createClient() throws Exception {Config config = new Config();config.protocol = "https";config.regionId = "central";return new Client(config);}public void sendInteractiveCardMessage(String cardTemplateId, String openConversationId, String outTrackId, Map<String, String> cardDataCardParamMap, Map<String, String> cardDataCardMediaIdParamMap) {try {Client client = DingServiceImpl.createClient();SendInteractiveCardHeaders sendInteractiveCardHeaders = new SendInteractiveCardHeaders();sendInteractiveCardHeaders.xAcsDingtalkAccessToken = getAccessToken();SendInteractiveCardRequest.SendInteractiveCardRequestCardData cardData = new SendInteractiveCardRequest.SendInteractiveCardRequestCardData().setCardMediaIdParamMap(cardDataCardMediaIdParamMap).setCardParamMap(cardDataCardParamMap);SendInteractiveCardRequest sendInteractiveCardRequest = new SendInteractiveCardRequest().setCardTemplateId("消息模板id").setOpenConversationId("接收卡片消息的群id").setOutTrackId(outTrackId).setRobotCode("群模板里的群机器人id").setConversationType(1).setCardData(cardData);client.sendInteractiveCardWithOptions(sendInteractiveCardRequest, sendInteractiveCardHeaders, new RuntimeOptions());sendToWebHook("互动卡片消息发送成功", ApiUrlConstant.WEB_HOOK_TEST);} catch (Exception e) {logger.error("互动卡片消息发送失败", e);sendToWebHook("互动卡片消息发送失败,请查看日志", ApiUrlConstant.WEB_HOOK_TEST);}}
2.在业务代码里面调用上述接口,代码里面写的参数一定要和消息模板的参数对应
private void sendInteractiveCardMessage(IntegralApplicationDO integralApplicationDO) {// 消息的参数Map<String, String> cardDataParamMap = new HashMap<>(16);cardDataParamMap.put("applyContent", integralApplicationDO.getApplyContent());UserInfoDO applyUser = userInfoService.getUserByUserId(integralApplicationDO.getApplyUserId());if (Objects.nonNull(applyUser)) {cardDataParamMap.put("userName", applyUser.getUserName());}String interestGroupName = "-";Long interestGroup = integralApplicationDO.getInterestGroup();if (interestGroup != null) {InterestGroupDO interestGroupDO = interestGroupDao.getById(interestGroup);if (Objects.nonNull(interestGroupDO)) {interestGroupName = interestGroupDO.getGroupName();}}cardDataParamMap.put("interestGroup", interestGroupName);cardDataParamMap.put("applyTime", DateUtils.formatDate("yyyy-MM-dd HH:mm:ss", integralApplicationDO.getGmtCommit()));String applyType = "积分类型";cardDataParamMap.put("applyType", applyType);Map<String, String> cardDataMediaIdParamMap = new HashMap<>(16);// 这里是因为我们卡片消息里面要放图片String applyImagesUrl = integralApplicationDO.getApplyImagesUrl();if (StringUtils.isNotEmpty(applyImagesUrl)) {String[] applyImagesUrlArr = applyImagesUrl.split(",");for (int i = 1; i <= applyImagesUrlArr.length; i++) {String imageName = "image" + i;String mediaId = dingService.getUploadMediaId(applyImagesUrlArr[i - 1], "image");cardDataMediaIdParamMap.put(imageName, mediaId);}}String cardTemplateId = "消息模板id";String openConversationId = "接收卡片消息的群id";dingService.sendInteractiveCardMessage(cardTemplateId, openConversationId, integralApplicationDO.getId().toString() + "-" + UUID.randomUUID().toString().replace("-", ""), cardDataParamMap, cardDataMediaIdParamMap);}
这样卡片消息就会发到对应的群里面啦!
三、更新互动消息卡片
更新就比较简单啦,部分代码如下,其中outTrackId就是发送卡片消息的时候的那个唯一标识。
public void updateInteractiveCardMessage(Map<String, String> cardDataCardParamMap, String outTrackId) {try {Client client = DingServiceImpl.createClient();logger.info("cardDataCardParamMap: " + cardDataCardParamMap + ",outTrackId: " + outTrackId);UpdateInteractiveCardHeaders updateInteractiveCardHeaders = new UpdateInteractiveCardHeaders();updateInteractiveCardHeaders.xAcsDingtalkAccessToken = getAccessToken();UpdateInteractiveCardRequest.UpdateInteractiveCardRequestCardOptions cardOptions = new UpdateInteractiveCardRequest.UpdateInteractiveCardRequestCardOptions().setUpdateCardDataByKey(true);UpdateInteractiveCardRequest.UpdateInteractiveCardRequestCardData cardData = new UpdateInteractiveCardRequest.UpdateInteractiveCardRequestCardData().setCardParamMap(cardDataCardParamMap);UpdateInteractiveCardRequest updateInteractiveCardRequest = new UpdateInteractiveCardRequest().setOutTrackId(outTrackId).setCardOptions(cardOptions).setUserIdType(1).setCardData(cardData);client.updateInteractiveCardWithOptions(updateInteractiveCardRequest, updateInteractiveCardHeaders, new RuntimeOptions());} catch (Exception e) {logger.error("互动卡片消息更新失败", e);sendToWebHook("互动卡片消息更新失败,请查看日志", ApiUrlConstant.WEB_HOOK_TEST);}}