parent
ec2950b0b3
commit
7ea71af0a4
@ -0,0 +1,24 @@
|
|||||||
|
package com.java3y.austin.common.dto.account;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 企业微信 机器人 账号信息
|
||||||
|
*
|
||||||
|
* @author 3y
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class EnterpriseWeChatRobotAccount {
|
||||||
|
/**
|
||||||
|
* 自定义群机器人中的 webhook
|
||||||
|
*/
|
||||||
|
private String webhook;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,437 @@
|
|||||||
|
package com.java3y.austin.handler.domain.wechat.robot;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 钉钉自定义机器人 入参
|
||||||
|
*
|
||||||
|
* https://open.dingtalk.com/document/group/custom-robot-access
|
||||||
|
*
|
||||||
|
* @author 3y
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class EnterpriseWeChatRobotParam {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* msgtype
|
||||||
|
*/
|
||||||
|
@JSONField(name = "msgtype")
|
||||||
|
private String msgType;
|
||||||
|
/**
|
||||||
|
* text
|
||||||
|
*/
|
||||||
|
@JSONField(name = "text")
|
||||||
|
private TextDTO text;
|
||||||
|
/**
|
||||||
|
* markdown
|
||||||
|
*/
|
||||||
|
@JSONField(name = "markdown")
|
||||||
|
private MarkdownDTO markdown;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* markdown
|
||||||
|
*/
|
||||||
|
@JSONField(name = "image")
|
||||||
|
private ImageDTO image;
|
||||||
|
/**
|
||||||
|
* news
|
||||||
|
*/
|
||||||
|
@JSONField(name = "news")
|
||||||
|
private NewsDTO news;
|
||||||
|
/**
|
||||||
|
* file
|
||||||
|
*/
|
||||||
|
@JSONField(name = "file")
|
||||||
|
private FileDTO file;
|
||||||
|
/**
|
||||||
|
* templateCard
|
||||||
|
*/
|
||||||
|
@JSONField(name = "template_card")
|
||||||
|
private TemplateCardDTO templateCard;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TextDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class TextDTO {
|
||||||
|
/**
|
||||||
|
* content
|
||||||
|
*/
|
||||||
|
@JSONField(name = "content")
|
||||||
|
private String content;
|
||||||
|
/**
|
||||||
|
* mentionedList
|
||||||
|
*/
|
||||||
|
@JSONField(name = "mentioned_list")
|
||||||
|
private List<String> mentionedList;
|
||||||
|
/**
|
||||||
|
* mentionedMobileList
|
||||||
|
*/
|
||||||
|
@JSONField(name = "mentioned_mobile_list")
|
||||||
|
private List<String> mentionedMobileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MarkdownDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class MarkdownDTO {
|
||||||
|
/**
|
||||||
|
* content
|
||||||
|
*/
|
||||||
|
@JSONField(name = "content")
|
||||||
|
private String content;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* ImageDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class ImageDTO {
|
||||||
|
/**
|
||||||
|
* base64
|
||||||
|
*/
|
||||||
|
@JSONField(name = "base64")
|
||||||
|
private String base64;
|
||||||
|
|
||||||
|
@JSONField(name = "md5")
|
||||||
|
private String md5;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NewsDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class NewsDTO {
|
||||||
|
/**
|
||||||
|
* articles
|
||||||
|
*/
|
||||||
|
@JSONField(name = "articles")
|
||||||
|
private List<ArticlesDTO> articles;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArticlesDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class ArticlesDTO {
|
||||||
|
/**
|
||||||
|
* title
|
||||||
|
*/
|
||||||
|
@JSONField(name = "title")
|
||||||
|
private String title;
|
||||||
|
/**
|
||||||
|
* description
|
||||||
|
*/
|
||||||
|
@JSONField(name = "description")
|
||||||
|
private String description;
|
||||||
|
/**
|
||||||
|
* url
|
||||||
|
*/
|
||||||
|
@JSONField(name = "url")
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* picurl
|
||||||
|
*/
|
||||||
|
@JSONField(name = "picurl")
|
||||||
|
private String picurl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FileDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class FileDTO {
|
||||||
|
/**
|
||||||
|
* mediaId
|
||||||
|
*/
|
||||||
|
@JSONField(name = "media_id")
|
||||||
|
private String mediaId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TemplateCardDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class TemplateCardDTO {
|
||||||
|
/**
|
||||||
|
* cardType
|
||||||
|
*/
|
||||||
|
@JSONField(name = "card_type")
|
||||||
|
private String cardType;
|
||||||
|
/**
|
||||||
|
* source
|
||||||
|
*/
|
||||||
|
@JSONField(name = "source")
|
||||||
|
private SourceDTO source;
|
||||||
|
/**
|
||||||
|
* mainTitle
|
||||||
|
*/
|
||||||
|
@JSONField(name = "main_title")
|
||||||
|
private MainTitleDTO mainTitle;
|
||||||
|
/**
|
||||||
|
* emphasisContent
|
||||||
|
*/
|
||||||
|
@JSONField(name = "emphasis_content")
|
||||||
|
private EmphasisContentDTO emphasisContent;
|
||||||
|
/**
|
||||||
|
* quoteArea
|
||||||
|
*/
|
||||||
|
@JSONField(name = "quote_area")
|
||||||
|
private QuoteAreaDTO quoteArea;
|
||||||
|
/**
|
||||||
|
* subTitleText
|
||||||
|
*/
|
||||||
|
@JSONField(name = "sub_title_text")
|
||||||
|
private String subTitleText;
|
||||||
|
/**
|
||||||
|
* horizontalContentList
|
||||||
|
*/
|
||||||
|
@JSONField(name = "horizontal_content_list")
|
||||||
|
private List<HorizontalContentListDTO> horizontalContentList;
|
||||||
|
/**
|
||||||
|
* jumpList
|
||||||
|
*/
|
||||||
|
@JSONField(name = "jump_list")
|
||||||
|
private List<JumpListDTO> jumpList;
|
||||||
|
/**
|
||||||
|
* cardAction
|
||||||
|
*/
|
||||||
|
@JSONField(name = "card_action")
|
||||||
|
private CardActionDTO cardAction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SourceDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class SourceDTO {
|
||||||
|
/**
|
||||||
|
* iconUrl
|
||||||
|
*/
|
||||||
|
@JSONField(name = "icon_url")
|
||||||
|
private String iconUrl;
|
||||||
|
/**
|
||||||
|
* desc
|
||||||
|
*/
|
||||||
|
@JSONField(name = "desc")
|
||||||
|
private String desc;
|
||||||
|
/**
|
||||||
|
* descColor
|
||||||
|
*/
|
||||||
|
@JSONField(name = "desc_color")
|
||||||
|
private Integer descColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MainTitleDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class MainTitleDTO {
|
||||||
|
/**
|
||||||
|
* title
|
||||||
|
*/
|
||||||
|
@JSONField(name = "title")
|
||||||
|
private String title;
|
||||||
|
/**
|
||||||
|
* desc
|
||||||
|
*/
|
||||||
|
@JSONField(name = "desc")
|
||||||
|
private String desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EmphasisContentDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class EmphasisContentDTO {
|
||||||
|
/**
|
||||||
|
* title
|
||||||
|
*/
|
||||||
|
@JSONField(name = "title")
|
||||||
|
private String title;
|
||||||
|
/**
|
||||||
|
* desc
|
||||||
|
*/
|
||||||
|
@JSONField(name = "desc")
|
||||||
|
private String desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* QuoteAreaDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class QuoteAreaDTO {
|
||||||
|
/**
|
||||||
|
* type
|
||||||
|
*/
|
||||||
|
@JSONField(name = "type")
|
||||||
|
private Integer type;
|
||||||
|
/**
|
||||||
|
* url
|
||||||
|
*/
|
||||||
|
@JSONField(name = "url")
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* appid
|
||||||
|
*/
|
||||||
|
@JSONField(name = "appid")
|
||||||
|
private String appid;
|
||||||
|
/**
|
||||||
|
* pagepath
|
||||||
|
*/
|
||||||
|
@JSONField(name = "pagepath")
|
||||||
|
private String pagepath;
|
||||||
|
/**
|
||||||
|
* title
|
||||||
|
*/
|
||||||
|
@JSONField(name = "title")
|
||||||
|
private String title;
|
||||||
|
/**
|
||||||
|
* quoteText
|
||||||
|
*/
|
||||||
|
@JSONField(name = "quote_text")
|
||||||
|
private String quoteText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CardActionDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class CardActionDTO {
|
||||||
|
/**
|
||||||
|
* type
|
||||||
|
*/
|
||||||
|
@JSONField(name = "type")
|
||||||
|
private Integer type;
|
||||||
|
/**
|
||||||
|
* url
|
||||||
|
*/
|
||||||
|
@JSONField(name = "url")
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* appid
|
||||||
|
*/
|
||||||
|
@JSONField(name = "appid")
|
||||||
|
private String appid;
|
||||||
|
/**
|
||||||
|
* pagepath
|
||||||
|
*/
|
||||||
|
@JSONField(name = "pagepath")
|
||||||
|
private String pagepath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HorizontalContentListDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class HorizontalContentListDTO {
|
||||||
|
/**
|
||||||
|
* keyname
|
||||||
|
*/
|
||||||
|
@JSONField(name = "keyname")
|
||||||
|
private String keyname;
|
||||||
|
/**
|
||||||
|
* value
|
||||||
|
*/
|
||||||
|
@JSONField(name = "value")
|
||||||
|
private String value;
|
||||||
|
/**
|
||||||
|
* type
|
||||||
|
*/
|
||||||
|
@JSONField(name = "type")
|
||||||
|
private Integer type;
|
||||||
|
/**
|
||||||
|
* url
|
||||||
|
*/
|
||||||
|
@JSONField(name = "url")
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* mediaId
|
||||||
|
*/
|
||||||
|
@JSONField(name = "media_id")
|
||||||
|
private String mediaId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JumpListDTO
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class JumpListDTO {
|
||||||
|
/**
|
||||||
|
* type
|
||||||
|
*/
|
||||||
|
@JSONField(name = "type")
|
||||||
|
private Integer type;
|
||||||
|
/**
|
||||||
|
* url
|
||||||
|
*/
|
||||||
|
@JSONField(name = "url")
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* title
|
||||||
|
*/
|
||||||
|
@JSONField(name = "title")
|
||||||
|
private String title;
|
||||||
|
/**
|
||||||
|
* appid
|
||||||
|
*/
|
||||||
|
@JSONField(name = "appid")
|
||||||
|
private String appid;
|
||||||
|
/**
|
||||||
|
* pagepath
|
||||||
|
*/
|
||||||
|
@JSONField(name = "pagepath")
|
||||||
|
private String pagepath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
package com.java3y.austin.handler.handler.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.codec.Base64;
|
||||||
|
import cn.hutool.core.io.file.FileReader;
|
||||||
|
import cn.hutool.crypto.digest.DigestUtil;
|
||||||
|
import cn.hutool.crypto.digest.MD5;
|
||||||
|
import cn.hutool.http.ContentType;
|
||||||
|
import cn.hutool.http.Header;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
|
import com.java3y.austin.common.constant.SendAccountConstant;
|
||||||
|
import com.java3y.austin.common.domain.TaskInfo;
|
||||||
|
import com.java3y.austin.common.dto.account.EnterpriseWeChatRobotAccount;
|
||||||
|
import com.java3y.austin.common.dto.model.EnterpriseWeChatRobotContentModel;
|
||||||
|
import com.java3y.austin.common.enums.ChannelType;
|
||||||
|
import com.java3y.austin.common.enums.SendMessageType;
|
||||||
|
import com.java3y.austin.handler.domain.wechat.robot.EnterpriseWeChatRobotParam;
|
||||||
|
import com.java3y.austin.handler.handler.BaseHandler;
|
||||||
|
import com.java3y.austin.handler.handler.Handler;
|
||||||
|
import com.java3y.austin.support.domain.MessageTemplate;
|
||||||
|
import com.java3y.austin.support.utils.AccountUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 企业微信群机器人 消息处理器
|
||||||
|
*
|
||||||
|
* @author 3y
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class EnterpriseWeChatRobotHandler extends BaseHandler implements Handler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AccountUtils accountUtils;
|
||||||
|
|
||||||
|
public EnterpriseWeChatRobotHandler() {
|
||||||
|
channelCode = ChannelType.ENTERPRISE_WE_CHAT_ROBOT.getCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean handler(TaskInfo taskInfo) {
|
||||||
|
try {
|
||||||
|
EnterpriseWeChatRobotAccount account = accountUtils.getAccount(taskInfo.getSendAccount(), SendAccountConstant.ENTERPRISE_WECHAT_ROBOT_ACCOUNT_KEY, SendAccountConstant.ENTERPRISE_WECHAT_ROBOT_PREFIX, EnterpriseWeChatRobotAccount.class);
|
||||||
|
EnterpriseWeChatRobotParam enterpriseWeChatRobotParam = assembleParam(taskInfo);
|
||||||
|
String result = HttpRequest.post(account.getWebhook()).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
|
||||||
|
.body(JSON.toJSONString(enterpriseWeChatRobotParam))
|
||||||
|
.timeout(2000)
|
||||||
|
.execute().body();
|
||||||
|
JSONObject jsonObject = JSON.parseObject(result);
|
||||||
|
if (jsonObject.getInteger("errcode") != 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
log.error("EnterpriseWeChatRobotHandler#handler fail! result:{},params:{}", JSON.toJSONString(jsonObject), JSON.toJSONString(taskInfo));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("EnterpriseWeChatRobotHandler#handler fail!e:{},params:{}", Throwables.getStackTraceAsString(e), JSON.toJSONString(taskInfo));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private EnterpriseWeChatRobotParam assembleParam(TaskInfo taskInfo) {
|
||||||
|
EnterpriseWeChatRobotContentModel contentModel = (EnterpriseWeChatRobotContentModel) taskInfo.getContentModel();
|
||||||
|
EnterpriseWeChatRobotParam param = EnterpriseWeChatRobotParam.builder()
|
||||||
|
.msgType(SendMessageType.getEnterpriseWeChatRobotTypeByCode(contentModel.getSendType())).build();
|
||||||
|
|
||||||
|
if (SendMessageType.TEXT.getCode().equals(contentModel.getSendType())) {
|
||||||
|
param.setText(EnterpriseWeChatRobotParam.TextDTO.builder().content(contentModel.getContent()).build());
|
||||||
|
}
|
||||||
|
if (SendMessageType.MARKDOWN.getCode().equals(contentModel.getSendType())) {
|
||||||
|
param.setMarkdown(EnterpriseWeChatRobotParam.MarkdownDTO.builder().content(contentModel.getContent()).build());
|
||||||
|
}
|
||||||
|
if (SendMessageType.IMAGE.getCode().equals(contentModel.getSendType())) {
|
||||||
|
FileReader fileReader = new FileReader(contentModel.getImagePath());
|
||||||
|
byte[] bytes = fileReader.readBytes();
|
||||||
|
param.setImage(EnterpriseWeChatRobotParam.ImageDTO.builder().base64(Base64.encode(bytes))
|
||||||
|
.md5(DigestUtil.md5Hex(bytes)).build());
|
||||||
|
}
|
||||||
|
if (SendMessageType.FILE.getCode().equals(contentModel.getSendType())) {
|
||||||
|
param.setFile(EnterpriseWeChatRobotParam.FileDTO.builder().mediaId(contentModel.getMediaId()).build());
|
||||||
|
}
|
||||||
|
if (SendMessageType.NEWS.getCode().equals(contentModel.getSendType())) {
|
||||||
|
List<EnterpriseWeChatRobotParam.NewsDTO.ArticlesDTO> articlesDTOS = JSON.parseArray(contentModel.getArticles(), EnterpriseWeChatRobotParam.NewsDTO.ArticlesDTO.class);
|
||||||
|
param.setNews(EnterpriseWeChatRobotParam.NewsDTO.builder().articles(articlesDTOS).build());
|
||||||
|
}
|
||||||
|
if (SendMessageType.TEMPLATE_CARD.getCode().equals(contentModel.getSendType())) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
return param;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recall(MessageTemplate messageTemplate) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue