| package com.xcong.excoin.utils.dingtalk; | 
|   | 
| 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 lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.codec.binary.Base64; | 
|   | 
| import javax.crypto.Mac; | 
| import javax.crypto.spec.SecretKeySpec; | 
| import java.net.URLEncoder; | 
| import java.util.ArrayList; | 
| import java.util.List; | 
|   | 
| /** | 
|  * @author wzy | 
|  * @date 2020-04-17 22:18 | 
|  **/ | 
| @Slf4j | 
| public class DingTalkUtils { | 
|   | 
|     private static final String SECRET = "SECc0b73559742b950f07eabbd050c406a6abb3b67d112d3735289e90f58884c543"; | 
|   | 
|     public static void sendActionCard(int type) { | 
|         log.info("send dingtalk"); | 
|         String url = "https://oapi.dingtalk.com/robot/send?access_token=161d5e5b60ae5d6b4c80f2a9c35f9f212961a7c7154aa7e94b99503eca3886b0"; | 
|         Long timestamp = System.currentTimeMillis(); | 
|         try { | 
|             String sign = generateSign(timestamp); | 
|             url = url + "×tamp=" + timestamp + "&sign=" + sign; | 
|             DingTalkClient client = new DefaultDingTalkClient(url); | 
|             OapiRobotSendRequest request = new OapiRobotSendRequest(); | 
|             request.setMsgtype("actionCard"); | 
|             OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard(); | 
|             actionCard.setTitle(DingTalkType.getName(type)); | 
|             actionCard.setBtnOrientation("1"); | 
|             actionCard.setText(DingTalkType.getName(type)); | 
|             List<OapiRobotSendRequest.Btns> btns = new ArrayList<>(); | 
|             OapiRobotSendRequest.Btns btn1 = new OapiRobotSendRequest.Btns(); | 
|             btn1.setTitle("查看详情"); | 
|             btn1.setActionURL("http://baidu.com"); | 
|             btns.add(btn1); | 
|             actionCard.setBtns(btns); | 
|   | 
|             request.setActionCard(actionCard); | 
|             OapiRobotSendResponse response = client.execute(request); | 
|             log.info(JSONObject.toJSONString(response)); | 
|         } catch (Exception e) { | 
|             log.error("#dingtalk send error#", e); | 
|         } finally { | 
|             log.error("#dingtalk finally#"); | 
|         } | 
|     } | 
|   | 
|   | 
|     private static String generateSign(Long timestamp) throws Exception { | 
|         String stringToToken = timestamp + "\n" + SECRET; | 
|         Mac mac = Mac.getInstance("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; | 
|     } | 
|   | 
| } |