| | |
| | | package com.xcong.excoin.utils.dingtalk; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.dingtalk.api.DefaultDingTalkClient; |
| | | import com.dingtalk.api.DingTalkClient; |
| | | import com.dingtalk.api.request.OapiRobotSendRequest; |
| | | import com.dingtalk.api.response.OapiRobotSendResponse; |
| | | import com.xcong.excoin.configurations.properties.ApplicationProperties; |
| | | import com.xcong.excoin.utils.SpringContextHolder; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | |
| | |
| | | **/ |
| | | @Slf4j |
| | | public class DingTalkUtils { |
| | | |
| | | private static final String SECRET = "SECbc84fd6c2edb9f2f440f9f969981ca310ba553e7c7994cf68cf26e4607cc5943"; |
| | | private static final String URL = "https://oapi.dingtalk.com/robot/send?access_token=54a0d627111f3667f7e98691c15becadb742c4da895e60f6d77392389e49658a"; |
| | | |
| | | private static final String EXCEPTION_URL = "https://oapi.dingtalk.com/robot/send?access_token=da71769f2b21fe2004f00980a8d8e9c4b9db76a0b8025b6f4d957f55653fea39"; |
| | | private static final String EXCEPTION_SECRET = "SECd9169511c38c3643bbda2440b1dcd1d3a57203656186fc2ceb7fcbef18c777ed"; |
| | | |
| | | |
| | | private static DingTalkClient initClient(String url, String secret) throws Exception { |
| | | Long timestamp = System.currentTimeMillis(); |
| | | String sign = generateSign(timestamp, secret); |
| | | url = url + "×tamp=" + timestamp + "&sign=" + sign; |
| | | return new DefaultDingTalkClient(url); |
| | | } |
| | | |
| | | public static void sendActionCard(int type) { |
| | | log.info("send dingtalk"); |
| | | String url = "https://oapi.dingtalk.com/robot/send?access_token=54a0d627111f3667f7e98691c15becadb742c4da895e60f6d77392389e49658a"; |
| | | Long timestamp = System.currentTimeMillis(); |
| | | |
| | | ApplicationProperties properties = SpringContextHolder.getBean(ApplicationProperties.class); |
| | | try { |
| | | String sign = generateSign(timestamp); |
| | | url = url + "×tamp=" + timestamp + "&sign=" + sign; |
| | | DingTalkClient client = new DefaultDingTalkClient(url); |
| | | DingTalkClient client = initClient(URL, SECRET); |
| | | OapiRobotSendRequest request = new OapiRobotSendRequest(); |
| | | request.setMsgtype("actionCard"); |
| | | OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard(); |
| | | actionCard.setTitle(DingTalkType.getName(type)); |
| | | actionCard.setTitle(properties.getProject() + "--" + DingTalkType.getName(type)); |
| | | actionCard.setBtnOrientation("1"); |
| | | actionCard.setText(DingTalkType.getName(type)); |
| | | actionCard.setText(properties.getProject() + "--" + DingTalkType.getName(type)); |
| | | List<OapiRobotSendRequest.Btns> btns = new ArrayList<>(); |
| | | OapiRobotSendRequest.Btns btn1 = new OapiRobotSendRequest.Btns(); |
| | | btn1.setTitle("查看详情"); |
| | |
| | | log.info(JSONObject.toJSONString(response)); |
| | | } catch (Exception e) { |
| | | log.error("#dingtalk send error#", e); |
| | | } finally { |
| | | log.error("#dingtalk finally#"); |
| | | } |
| | | } |
| | | |
| | | public static void sendMsg(String title, String msg, Long id) { |
| | | try { |
| | | String text = "### {} \n\n {}"; |
| | | DingTalkClient client = initClient(EXCEPTION_URL, EXCEPTION_SECRET); |
| | | |
| | | OapiRobotSendRequest request = new OapiRobotSendRequest(); |
| | | request.setMsgtype("actionCard"); |
| | | OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard(); |
| | | actionCard.setTitle("异常消息"); |
| | | actionCard.setBtnOrientation("0"); |
| | | actionCard.setText(StrUtil.format(text, title, msg)); |
| | | actionCard.setSingleURL("http://123.60.4.127:8880/common/getExceptionMsg?id=" + id); |
| | | actionCard.setSingleTitle("查询详情"); |
| | | |
| | | request.setActionCard(actionCard); |
| | | OapiRobotSendResponse response = client.execute(request); |
| | | log.info(JSONObject.toJSONString(response)); |
| | | } catch (Exception e) { |
| | | log.error("#dingtalk send error#", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | private static String generateSign(Long timestamp) throws Exception { |
| | | String stringToToken = timestamp + "\n" + SECRET; |
| | | private static String generateSign(Long timestamp, String secret) throws Exception { |
| | | String stringToToken = timestamp + "\n" + secret; |
| | | Mac mac = Mac.getInstance("HmacSHA256"); |
| | | mac.init(new SecretKeySpec(SECRET.getBytes("UTF-8"), "HmacSHA256")); |
| | | mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); |
| | | byte[] signData = mac.doFinal(stringToToken.getBytes("UTF-8")); |
| | | String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); |
| | | return sign; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // sendMsg("测试一下", "内容"); |
| | | } |
| | | |
| | | } |