消息推送改为DINGDING
This commit is contained in:
parent
f8f94bcc97
commit
ed92db8b91
@ -2,7 +2,6 @@ package top.baogutang.admin.schedule;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.TypeReference;
|
import com.alibaba.fastjson.TypeReference;
|
||||||
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -13,7 +12,6 @@ import org.springframework.stereotype.Component;
|
|||||||
import top.baogutang.admin.domain.EdgeXAnnouncementsDto;
|
import top.baogutang.admin.domain.EdgeXAnnouncementsDto;
|
||||||
import top.baogutang.admin.services.IWxMsgPushService;
|
import top.baogutang.admin.services.IWxMsgPushService;
|
||||||
import top.baogutang.admin.utils.DingTalkMsgPushUtils;
|
import top.baogutang.admin.utils.DingTalkMsgPushUtils;
|
||||||
import top.baogutang.common.constants.DingTalkMsgTypeEnum;
|
|
||||||
import top.baogutang.common.domain.Page;
|
import top.baogutang.common.domain.Page;
|
||||||
import top.baogutang.common.domain.Results;
|
import top.baogutang.common.domain.Results;
|
||||||
import top.baogutang.common.properties.WxMsgPushProperties;
|
import top.baogutang.common.properties.WxMsgPushProperties;
|
||||||
@ -74,27 +72,10 @@ public class EdgeXNoticeSchedule {
|
|||||||
|
|
||||||
Boolean result = redisTemplate.opsForValue().setIfAbsent(cacheKey, 1, 5, TimeUnit.DAYS);
|
Boolean result = redisTemplate.opsForValue().setIfAbsent(cacheKey, 1, 5, TimeUnit.DAYS);
|
||||||
if (Boolean.TRUE.equals(result)) {
|
if (Boolean.TRUE.equals(result)) {
|
||||||
dingTalkMsgPushUtils.dingTalkMsgPush(agentId, Boolean.TRUE, null, null, this.geneMsg(announcementsDto));
|
String content = "# " + announcementsDto.getTitle() + "\n\n + ")\n\n> 点击下方链接查看更多详情:\n\n[查看详情](" + "https://app.edge-x.cn/#/noticeDetail?noticeId=" + announcementsDto.getId() + ")";
|
||||||
|
dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), content);
|
||||||
// wxMsgPushService.msgPush(Message.CONTENT_TYPE_HTML, announcementsDto.getTitle(), announcementsDto.getContent(), wxMsgPushProperties.getTopicIds());
|
// wxMsgPushService.msgPush(Message.CONTENT_TYPE_HTML, announcementsDto.getTitle(), announcementsDto.getContent(), wxMsgPushProperties.getTopicIds());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private OapiMessageCorpconversationAsyncsendV2Request.Msg geneMsg(EdgeXAnnouncementsDto announcementsDto) {
|
|
||||||
// 读取消息文件
|
|
||||||
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
|
|
||||||
msg.setMsgtype(DingTalkMsgTypeEnum.ACTION_CARD.getType());
|
|
||||||
OapiMessageCorpconversationAsyncsendV2Request.ActionCard actionCard = new OapiMessageCorpconversationAsyncsendV2Request.ActionCard();
|
|
||||||
// 透出到会话列表和通知的文案
|
|
||||||
actionCard.setTitle(announcementsDto.getTitle());
|
|
||||||
// 支持markdown格式的正文内容
|
|
||||||
String title = "# " + announcementsDto.getTitle() + "+\n\n";
|
|
||||||
String image = " + ")";
|
|
||||||
actionCard.setMarkdown(title + image);
|
|
||||||
// 跳转按钮展示
|
|
||||||
actionCard.setSingleTitle("查看详情");
|
|
||||||
// 跳转URL
|
|
||||||
actionCard.setSingleUrl("https://app.edge-x.cn/#/noticeDetail?noticeId=" + announcementsDto.getId());
|
|
||||||
msg.setActionCard(actionCard);
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,19 +5,27 @@ import com.dingtalk.api.DefaultDingTalkClient;
|
|||||||
import com.dingtalk.api.DingTalkClient;
|
import com.dingtalk.api.DingTalkClient;
|
||||||
import com.dingtalk.api.request.OapiGettokenRequest;
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
||||||
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
||||||
|
import com.dingtalk.api.request.OapiRobotSendRequest;
|
||||||
import com.dingtalk.api.response.OapiGettokenResponse;
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
||||||
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
||||||
import com.taobao.api.ApiException;
|
import com.dingtalk.api.response.OapiRobotSendResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import top.baogutang.common.constants.DingTalkMsgTypeEnum;
|
||||||
import top.baogutang.common.exceptions.BusinessException;
|
import top.baogutang.common.exceptions.BusinessException;
|
||||||
import top.baogutang.common.properties.DingTalkConfigFactory;
|
import top.baogutang.common.properties.DingTalkConfigFactory;
|
||||||
import top.baogutang.common.properties.DingTalkConfigStrategy;
|
import top.baogutang.common.properties.DingTalkConfigStrategy;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.crypto.Mac;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -42,6 +50,12 @@ public class DingTalkMsgPushUtils {
|
|||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String, Object> redisTemplate;
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
|
@Value("${dingtalk.baogutang.niko_notice_reboot}")
|
||||||
|
private String robotWebhookUrl;
|
||||||
|
|
||||||
|
@Value("${dingtalk.baogutang.secrect}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 钉钉消息推送 {https://open.dingtalk.com/document/orgapp/work-notice-option}
|
* 钉钉消息推送 {https://open.dingtalk.com/document/orgapp/work-notice-option}
|
||||||
*
|
*
|
||||||
@ -119,4 +133,43 @@ public class DingTalkMsgPushUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void robotMarkdownMsgPush(String title, String content) {
|
||||||
|
// 计算签名
|
||||||
|
long timestamp = System.currentTimeMillis();
|
||||||
|
String sign = this.sign(timestamp);
|
||||||
|
String url = robotWebhookUrl + "×tamp=" + timestamp + "&sign=" + sign;
|
||||||
|
DingTalkClient client = new DefaultDingTalkClient(url);
|
||||||
|
OapiRobotSendRequest request = new OapiRobotSendRequest();
|
||||||
|
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
|
||||||
|
at.setIsAtAll(true);
|
||||||
|
request.setAt(at);
|
||||||
|
request.setMsgtype(DingTalkMsgTypeEnum.MARKDOWN.getType());
|
||||||
|
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
|
||||||
|
markdown.setTitle(title);
|
||||||
|
markdown.setText(content);
|
||||||
|
request.setMarkdown(markdown);
|
||||||
|
OapiRobotSendResponse response = null;
|
||||||
|
try {
|
||||||
|
response = client.execute(request);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(">>>>>>>>>>robot msg send error:{}<<<<<<<<<<", e.getMessage(), e);
|
||||||
|
}
|
||||||
|
log.info(">>>>>>>>>>robot msg send request:{}, response:{}<<<<<<<<<<", JSON.toJSONString(request), JSON.toJSONString(response));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String sign(Long timestamp) {
|
||||||
|
try {
|
||||||
|
String stringToSign = timestamp + "\n" + secret;
|
||||||
|
Mac mac = Mac.getInstance("HmacSHA256");
|
||||||
|
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||||
|
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
|
||||||
|
return URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(">>>>>>>>>>sign error:{}<<<<<<<<<<", e.getMessage(), e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user