Skip to content

Commit

Permalink
钉钉工作消息对各种消息类型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongFuCheng3y committed Jun 2, 2022
1 parent 1febb2c commit 34de9e3
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

/**
* @author 3y
* 钉钉 自定义机器人 + 工作通知
* 钉钉 自定义机器人
* <p>
* https://open.dingtalk.com/document/group/custom-robot-access
* <p>
* https://open.dingtalk.com/document/orgapp-server/asynchronous-sending-of-enterprise-session-messages
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DingDingContentModel extends ContentModel {
public class DingDingRobotContentModel extends ContentModel {

/**
* 发送类型
Expand Down Expand Up @@ -62,13 +60,4 @@ public class DingDingContentModel extends ContentModel {
* "[{\"picUrl\":\"https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png\",\"title\":\"{$title1}\",\"url\":\"https://www.dingtalk.com/\"},{\"picUrl\":\"https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png\\t\",\"title\":\"时代的火车向前开2\",\"url\":\"https://www.dingtalk.com/\"}]"}
*/
private String feedCards;


/**
* 图片、文件、语音消息 需要发送使用的素材ID字段
*/
private String mediaId;

// ...

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.java3y.austin.common.dto.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author 3y
* 钉钉 工作通知
* <p>
* https://open.dingtalk.com/document/orgapp-server/asynchronous-sending-of-enterprise-session-messages
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DingDingWorkContentModel extends ContentModel {

/**
* 发送类型
*/
private String sendType;

/**
* 【文本消息】内容,【markdown消息】内容,【ActionCard消息】内容
*/
private String content;

/**
* 【markdown消息】标题,【ActionCard消息】标题
*/
private String title;

/**
* 【ActionCard消息】按钮布局
*/
private String btnOrientation;

/**
* 【ActionCard消息】按钮的文案和跳转链接的json
* [{"title":"一个按钮","action_url":"https://www.taobao.com"},{"title":"两个按钮","action_url":"https://www.tmall.com"}]
*/
private String btns;


/**
* 【链接消息】点击消息跳转的URL,
*/
private String url;

/**
* 【链接消息】图片URL,
*/
private String picUrl;

/**
* 图片、文件、语音消息 需要发送使用的素材ID字段
*/
private String mediaId;

/**
* 语音时长
*/
private String duration;

/**
* OA消息头
* {"bgcolor":"FFBBBBBB","text":"头部标题"}
*/
private String head;

/**
* OA消息内容
* {"title":"正文标题","form":[{"key":"姓名:","value":"张三"},{"key":"年龄:","value":"20"},{"key":"身高:","value":"1.8米"},{"key":"体重:","value":"130斤"},{"key":"学历:","value":"本科"},{"key":"爱好:","value":"打球、听音乐"}],"rich":{"num":"15.6","unit":"元"},"content":"大段文本大段文本大段文本大段文本大段文本大段文本","image":"@lADOADmaWMzazQKA","file_count":"3","author":"李四 "}
*/
private String body;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public enum ChannelType {
OFFICIAL_ACCOUNT(50, "OfficialAccounts(服务号)", OfficialAccountsContentModel.class, "official_accounts"),
MINI_PROGRAM(60, "miniProgram(小程序)", MiniProgramContentModel.class, "mini_program"),
ENTERPRISE_WE_CHAT(70, "EnterpriseWeChat(企业微信)", EnterpriseWeChatContentModel.class, "enterprise_we_chat"),
DING_DING_ROBOT(80, "dingDingRobot(钉钉机器人)", DingDingContentModel.class, "ding_ding_robot"),
DING_DING_WORK_NOTICE(90, "dingDingWorkNotice(钉钉工作通知)", DingDingContentModel.class, "ding_ding_work_notice"),
DING_DING_ROBOT(80, "dingDingRobot(钉钉机器人)", DingDingRobotContentModel.class, "ding_ding_robot"),
DING_DING_WORK_NOTICE(90, "dingDingWorkNotice(钉钉工作通知)", DingDingWorkContentModel.class, "ding_ding_work_notice"),
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,37 @@
@AllArgsConstructor
public enum SendMessageType {

TEXT("10", "文本", "text"),
VOICE("20", "语音", null),
VIDEO("30", "视频", null),
NEWS("40", "图文", "feedCard"),
TEXT_CARD("50", "文本卡片", null),
FILE("60", "文件", null),
MINI_PROGRAM_NOTICE("70", "小程序通知", null),
MARKDOWN("80", "markdown", "markdown"),
TEMPLATE_CARD("90", "模板卡片", null),
IMAGE("100", "图片", null),
LINK("110", "链接消息", "link"),
ACTION_CARD("120", "跳转卡片消息", "actionCard"),
TEXT("10", "文本", "text", "text"),
VOICE("20", "语音", null, "voice"),
VIDEO("30", "视频", null, null),
NEWS("40", "图文", "feedCard", null),
TEXT_CARD("50", "文本卡片", null, null),
FILE("60", "文件", null, "file"),
MINI_PROGRAM_NOTICE("70", "小程序通知", null, null),
MARKDOWN("80", "markdown", "markdown", "markdown"),
TEMPLATE_CARD("90", "模板卡片", null, null),
IMAGE("100", "图片", null, "image"),
LINK("110", "链接消息", "link", "link"),
ACTION_CARD("120", "跳转卡片消息", "actionCard", "action_card"),
OA("130", "OA消息", null, "oa"),
;

private String code;
private String description;

/**
* 钉钉工作消息的类型值
*/
private String dingDingRobotType;

/**
* 钉钉机器人消息的类型值
*/
private String dingDingWorkType;


/**
* 通过code获取钉钉的Type值
* 通过code获取钉钉机器人的Type值
*
* @param code
* @return
Expand All @@ -48,5 +58,20 @@ public static String getDingDingRobotTypeByCode(String code) {
return null;
}

/**
* 通过code获取钉钉工作通知的Type值
*
* @param code
* @return
*/
public static String getDingDingWorkTypeByCode(String code) {
for (SendMessageType value : SendMessageType.values()) {
if (value.getCode().equals(code)) {
return value.getDingDingWorkType();
}
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.base.Throwables;
import com.java3y.austin.common.constant.AustinConstant;
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.domain.TaskInfo;
import com.java3y.austin.common.dto.account.DingDingRobotAccount;
import com.java3y.austin.common.dto.model.DingDingContentModel;
import com.java3y.austin.common.dto.model.DingDingRobotContentModel;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.common.enums.SendMessageType;
import com.java3y.austin.handler.domain.dingding.DingDingRobotParam;
Expand Down Expand Up @@ -74,7 +73,7 @@ private DingDingRobotParam assembleParam(TaskInfo taskInfo) {
}

// 消息类型以及内容相关
DingDingContentModel contentModel = (DingDingContentModel) taskInfo.getContentModel();
DingDingRobotContentModel contentModel = (DingDingRobotContentModel) taskInfo.getContentModel();
DingDingRobotParam param = DingDingRobotParam.builder().at(atVo)
.msgtype(SendMessageType.getDingDingRobotTypeByCode(contentModel.getSendType()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import com.java3y.austin.common.constant.SendAccountConstant;
import com.java3y.austin.common.domain.TaskInfo;
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
import com.java3y.austin.common.dto.model.DingDingContentModel;
import com.java3y.austin.common.dto.model.DingDingRobotContentModel;
import com.java3y.austin.common.dto.model.DingDingWorkContentModel;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.common.enums.SendMessageType;
import com.java3y.austin.handler.domain.dingding.DingDingRobotParam;
import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.support.utils.AccountUtils;
Expand All @@ -22,6 +25,8 @@
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 钉钉消息自定义机器人 消息处理器
* <p>
Expand Down Expand Up @@ -73,7 +78,7 @@ public boolean handler(TaskInfo taskInfo) {
*/
private OapiMessageCorpconversationAsyncsendV2Request assembleParam(DingDingWorkNoticeAccount account, TaskInfo taskInfo) {
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
DingDingContentModel contentModel = (DingDingContentModel) taskInfo.getContentModel();
DingDingWorkContentModel contentModel = (DingDingWorkContentModel) taskInfo.getContentModel();
try {
// 接收者相关
if (AustinConstant.SEND_ALL.equals(CollUtil.getFirst(taskInfo.getReceiver()))) {
Expand All @@ -83,13 +88,64 @@ private OapiMessageCorpconversationAsyncsendV2Request assembleParam(DingDingWork
}
req.setAgentId(Long.parseLong(account.getAgentId()));

// 内容相关

OapiMessageCorpconversationAsyncsendV2Request.Msg message = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
message.setMsgtype("text");
OapiMessageCorpconversationAsyncsendV2Request.Text textObj = new OapiMessageCorpconversationAsyncsendV2Request.Text();
textObj.setContent(contentModel.getContent());
message.setText(textObj);
message.setMsgtype(SendMessageType.getDingDingWorkTypeByCode(contentModel.getSendType()));

// 根据类型设置入参
if (SendMessageType.TEXT.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.Text textObj = new OapiMessageCorpconversationAsyncsendV2Request.Text();
textObj.setContent(contentModel.getContent());
message.setText(textObj);
}
if (SendMessageType.IMAGE.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.Image image = new OapiMessageCorpconversationAsyncsendV2Request.Image();
image.setMediaId(contentModel.getMediaId());
message.setImage(image);
}
if (SendMessageType.VOICE.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.Voice voice = new OapiMessageCorpconversationAsyncsendV2Request.Voice();
voice.setMediaId(contentModel.getMediaId());
voice.setDuration(contentModel.getDuration());
message.setVoice(voice);
}
if (SendMessageType.FILE.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.File file = new OapiMessageCorpconversationAsyncsendV2Request.File();
file.setMediaId(contentModel.getMediaId());
message.setFile(file);
}
if (SendMessageType.LINK.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.Link link = new OapiMessageCorpconversationAsyncsendV2Request.Link();
link.setText(contentModel.getContent());
link.setTitle(contentModel.getTitle());
link.setPicUrl(contentModel.getPicUrl());
link.setMessageUrl(contentModel.getUrl());
message.setLink(link);
}

if (SendMessageType.MARKDOWN.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.Markdown markdown = new OapiMessageCorpconversationAsyncsendV2Request.Markdown();
markdown.setText(contentModel.getContent());
markdown.setTitle(contentModel.getTitle());
message.setMarkdown(markdown);

}
if (SendMessageType.ACTION_CARD.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.ActionCard actionCard = new OapiMessageCorpconversationAsyncsendV2Request.ActionCard();
actionCard.setTitle(contentModel.getTitle());
actionCard.setMarkdown(contentModel.getContent());
actionCard.setBtnOrientation(contentModel.getBtnOrientation());
actionCard.setBtnJsonList(JSON.parseArray(contentModel.getBtns(), OapiMessageCorpconversationAsyncsendV2Request.BtnJsonList.class));
message.setActionCard(actionCard);

}
if (SendMessageType.ACTION_CARD.getCode().equals(contentModel.getSendType())) {
OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();
oa.setMessageUrl(contentModel.getUrl());
oa.setHead(JSON.parseObject(contentModel.getHead(), OapiMessageCorpconversationAsyncsendV2Request.Head.class));
oa.setBody(JSON.parseObject(contentModel.getBody(), OapiMessageCorpconversationAsyncsendV2Request.Body.class));
message.setOa(oa);
}
req.setMsg(message);
} catch (Exception e) {
log.error("assembleParam fail:{},params:{}", Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo));
Expand Down

0 comments on commit 34de9e3

Please sign in to comment.