接入 企业微信(文案) 消息推送

master
3y 3 years ago
parent 7e75a5b88f
commit b57d512a70

@ -1,9 +1,38 @@
package com.java3y.austin.common.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author 3y
*
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EnterpriseWeChatContentModel extends ContentModel {
/**
*
*/
private String messageType;
/**
* -
*/
private String content;
/**
* id
*/
private String mediaId;
/**
* https://developer.work.weixin.qq.com/document/path/90372#%E6%96%87%E6%9C%AC%E6%B6%88%E6%81%AF
*/
}

@ -23,7 +23,7 @@ public enum ChannelType {
EMAIL(40, "email(邮件)", EmailContentModel.class, "email"),
OFFICIAL_ACCOUNT(50, "OfficialAccounts(服务号)", OfficialAccountsContentModel.class, "official_accounts"),
MINI_PROGRAM(60, "miniProgram(小程序)", MiniProgramContentModel.class, "mini_program"),
ENTERPRISE_WE_CHAT(70, "EnterpriseWeChat(企业微信)", MiniProgramContentModel.class, "enterprise_we_chat"),
ENTERPRISE_WE_CHAT(70, "EnterpriseWeChat(企业微信)", EnterpriseWeChatContentModel.class, "enterprise_we_chat"),
;
/**

@ -7,6 +7,7 @@ import lombok.ToString;
/**
* ID
*
* @author 3y
*/
@Getter
@ -17,8 +18,9 @@ public enum IdType {
DID(20, "did"),
PHONE(30, "phone"),
OPEN_ID(40, "openId"),
EMAIL(50, "email");
EMAIL(50, "email"),
ENTERPRISE_USER_ID(60, "enterprise_user_id"),
;
private Integer code;
private String description;

@ -0,0 +1,32 @@
package com.java3y.austin.common.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
/**
*
*
* @author 3y
*/
@Getter
@ToString
@AllArgsConstructor
public enum WechatMessageType {
TEST(10, "文本"),
VOICE(20, "语音"),
VIDEO(30, "视频"),
NEWS(40, "图文"),
TEXT_CARD(50, "文本卡片"),
FILE(60, "文件"),
MINI_PROGRAM_NOTICE(70, "小程序通知"),
MARKDOWN(80, "markdown"),
TEMPLATE_CARD(90, "模板卡片"),
IMAGE(100, "图片"),
;
private Integer code;
private String description;
}

@ -1,15 +1,22 @@
package com.java3y.austin.handler.handler.impl;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.common.domain.TaskInfo;
import com.java3y.austin.common.dto.EnterpriseWeChatContentModel;
import com.java3y.austin.common.enums.ChannelType;
import com.java3y.austin.handler.handler.BaseHandler;
import com.java3y.austin.handler.handler.Handler;
import com.java3y.austin.handler.script.EnterpriseWeChatService;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -21,9 +28,20 @@ import org.springframework.stereotype.Component;
@Slf4j
public class EnterpriseWeChatHandler extends BaseHandler implements Handler {
@Autowired
private EnterpriseWeChatService enterpriseWeChatService;
/**
* WxCpMessage
*/
private static final String ALL = "@all";
private static final String DELIMITER = "|";
/**
*
*/
private static final String ENTERPRISE_WECHAT_ACCOUNT_KEY = "enterpriseWechatAccount";
private static final String PREFIX = "enterprise_wechat_";
@Autowired
private AccountUtils accountUtils;
public EnterpriseWeChatHandler() {
channelCode = ChannelType.ENTERPRISE_WE_CHAT.getCode();
@ -31,10 +49,11 @@ public class EnterpriseWeChatHandler extends BaseHandler implements Handler {
@Override
public boolean handler(TaskInfo taskInfo) {
WxCpMessage wxCpMessage = new WxCpMessage();
try {
WxCpMessageSendResult result = enterpriseWeChatService.send(wxCpMessage);
WxCpDefaultConfigImpl accountConfig = accountUtils.getAccount(taskInfo.getSendAccount(), ENTERPRISE_WECHAT_ACCOUNT_KEY, PREFIX, new WxCpDefaultConfigImpl());
WxCpMessageServiceImpl messageService = new WxCpMessageServiceImpl(initService(accountConfig));
WxCpMessageSendResult result = messageService.send(buildWxCpMessage(taskInfo, accountConfig.getAgentId()));
buildAnchorState(result);
return true;
} catch (Exception e) {
log.error("EnterpriseWeChatHandler#handler fail:{},params:{}",
@ -43,5 +62,50 @@ public class EnterpriseWeChatHandler extends BaseHandler implements Handler {
return false;
}
/**
*
*
* @param result
*/
private void buildAnchorState(WxCpMessageSendResult result) {
}
/**
* WxCpServiceImpl
*
* @param config
* @return
*/
private WxCpService initService(WxCpDefaultConfigImpl config) {
WxCpServiceImpl wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(config);
return wxCpService;
}
/**
*
*
* @param taskInfo
* @param agentId ID
* @return
*/
private WxCpMessage buildWxCpMessage(TaskInfo taskInfo, Integer agentId) {
String userId;
if (ALL.equals(CollUtil.getFirst(taskInfo.getReceiver()))) {
userId = CollUtil.getFirst(taskInfo.getReceiver());
} else {
userId = StringUtils.join(taskInfo.getReceiver(), DELIMITER);
}
EnterpriseWeChatContentModel enterpriseWeChatContentModel = (EnterpriseWeChatContentModel) taskInfo.getContentModel();
return WxCpMessage
.TEXT()
.agentId(agentId)
.toUser(userId)
.content(enterpriseWeChatContentModel.getContent())
.build();
}
}

@ -1,22 +0,0 @@
package com.java3y.austin.handler.script;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
/**
*
*
* @author 3y
*/
public interface EnterpriseWeChatService {
/**
* userId/@all)
*
* @param wxCpMessage
* @return
* @throws WxErrorException
*/
WxCpMessageSendResult send(WxCpMessage wxCpMessage) throws WxErrorException;
}

@ -1,45 +0,0 @@
package com.java3y.austin.handler.script.impl;
import com.java3y.austin.handler.script.EnterpriseWeChatService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.stereotype.Service;
/**
* @author 3y
* @date 2022/3/15
*/
@Slf4j
@Service
public class EnterpriseWeChatServiceImpl implements EnterpriseWeChatService {
@Override
public WxCpMessageSendResult send(WxCpMessage wxCpMessage) throws WxErrorException {
WxCpMessageServiceImpl messageService = new WxCpMessageServiceImpl(initService());
return messageService.send(wxCpMessage);
}
private WxCpService initService() {
WxCpServiceImpl wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(initConfig());
return wxCpService;
}
private WxCpConfigStorage initConfig() {
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setCorpId("");
config.setCorpSecret("");
config.setAgentId(1);
config.setToken("");
config.setAesKey("");
return config;
}
}

@ -23,6 +23,7 @@ public class AccountUtils {
/**
* (key:smsAccount)[{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDhDUUDfffffMEqBF1WljQq","secretKey":"B4h39yWnfffff7D2btue7JErDJ8gxyi","smsSdkAppId":"140025","templateId":"11897","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}}]
* (key:emailAccount)[{"email_10":{"host":"smtp.qq.com","port":465,"user":"403686131@qq.com","pass":"","from":"403686131@qq.com"}}]
* (key:enterpriseWechatAccount)[{"enterprise_wechat_10":{"corpId":"wwf87603333e00069c","corpSecret":"-IFWxS2222QxzPIorNVUQn144444D915DM","agentId":10044442,"token":"rXROB3333Kf6i","aesKey":"MKZtoFxHIM44444M7ieag3r9ZPUsl"}}]
*/
public <T> T getAccount(Integer sendAccount, String apolloKey, String prefix, T t) {
String accountValues = config.getProperty(apolloKey, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY);

@ -13,8 +13,8 @@ CREATE TABLE `message_template`
`cron_task_id` bigint(20) COMMENT '定时任务Id (xxl-job-admin返回)',
`cron_crowd_path` varchar(500) COMMENT '定时发送人群的文件路径',
`expect_push_time` varchar(100) COLLATE utf8mb4_unicode_ci COMMENT '期望发送时间0:立即发送 定时任务以及周期任务:cron表达式',
`id_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息的发送ID类型10. userId 20.did 30.手机号 40.openId 50.email',
`send_channel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息发送渠道10.IM 20.Push 30.短信 40.Email 50.公众号 60.小程序',
`id_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息的发送ID类型10. userId 20.did 30.手机号 40.openId 50.email 60.企业微信userId',
`send_channel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '消息发送渠道10.IM 20.Push 30.短信 40.Email 50.公众号 60.小程序 70.企业微信',
`template_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '10.运营类 20.技术类接口调用',
`msg_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '10.通知类消息 20.营销类消息 30.验证码类消息',
`msg_content` varchar(600) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '消息内容 占位符用{$var}表示',

Loading…
Cancel
Save