欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java封装一个钉钉机器人的工具类

程序员文章站 2022-05-16 18:33:17
...
package cn.tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


public class DingDingTools {

    private final String WEBHOOK_TOKEN;

    public DingDingTools(String token) {
        WEBHOOK_TOKEN = token;
    }

    //获取加签名后的token
    public static String getSecretToken(String token, String secret) throws NoSuchAlgorithmException, IOException, InvalidKeyException {
        Long timestamp = System.currentTimeMillis();
        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));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        return token + "&timestamp=" + timestamp + "&sign=" + sign;
    }


    private String send(String textMsg) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(WEBHOOK_TOKEN);
        httpPost.addHeader("Content-Type", "application/json; charset=utf-8");

        StringEntity se = new StringEntity(textMsg, "utf-8");
        httpPost.setEntity(se);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println("发送钉钉消息成功 " + result);
            return result;
        } else {
            System.out.println("发送钉钉消息失败");
        }
        // 关闭
        httpClient.close();
        response.close();

        return "发送钉钉消息失败";
    }


    //发送text格式的消息
    public String text(String mag, String[] atMobile, boolean isAtAll) throws IOException {
        JSONObject object = new JSONObject();
        JSONObject object_text = new JSONObject();
        JSONObject object_at = new JSONObject();

        object_text.put("content", mag);
        object_at.put("atMobiles", atMobile);
        object_at.put("isAtAll", isAtAll);

        object.put("msgtype", "text");
        object.put("text", object_text);
        object.put("at", object_at);


        return send(String.valueOf(object));
    }

    //发送link格式的消息
    public String link(String title, String text, String messageUrl, String picUrl) throws IOException {
        JSONObject object = new JSONObject();
        JSONObject object_link = new JSONObject();

        object_link.put("text", text);
        object_link.put("title", title);
        object_link.put("picUrl", picUrl);
        object_link.put("messageUrl", messageUrl);
        object.put("msgtype", "link");
        object.put("link", object_link);

        return send(String.valueOf(object));

    }

    //发送markdown格式的消息
    public String markdown(String title, String text, String atMobile, boolean isAtAll) throws IOException {
        JSONObject object = new JSONObject();
        JSONObject object_markdown = new JSONObject();
        JSONObject object_at = new JSONObject();

        object_markdown.put("title", title);
        object_markdown.put("text", text);
        object_at.put("atMobiles", atMobile);
        object_at.put("isAtAll", isAtAll);

        object.put("msgtype", "markdown");
        object.put("markdown", object_markdown);
        object.put("at", object_at);

        return send(String.valueOf(object));
    }

    //整体跳转ActionCard类型
    public String actionCard(String title, String text, String singleTitle, String singleURL, int btnOrientation) throws IOException {
        JSONObject object = new JSONObject();
        JSONObject object_actionCard = new JSONObject();

        object_actionCard.put("title", title);
        object_actionCard.put("text", text);
        object_actionCard.put("btnOrientation", btnOrientation);
        object_actionCard.put("singleTitle", singleTitle);
        object_actionCard.put("singleURL", singleURL);

        object.put("actionCard", object_actionCard);
        object.put("msgtype", "actionCard");

        return send(String.valueOf(object));

    }

    //独立跳转ActionCard类型
    public String actionCard1(String title, String text, String btnsTitle1, String btnsTitle2, String actionURL1, String actionURL2, int btnOrientation) throws IOException {
        JSONObject object = new JSONObject();
        JSONObject object_actionCard1 = new JSONObject();
        JSONObject object_btns1 = new JSONObject();
        JSONObject object_btns2 = new JSONObject();

        object_btns1.put("title", btnsTitle1);
        object_btns1.put("actionURL", actionURL1);
        object_btns2.put("title", btnsTitle2);
        object_btns2.put("actionURL", actionURL2);

        object_actionCard1.put("title", title);
        object_actionCard1.put("text", text);
        object_actionCard1.put("btnOrientation", btnOrientation);

        JSONArray btns = new JSONArray();
        btns.add(object_btns1);
        btns.add(object_btns2);
        object_actionCard1.put("btns", btns);

        object.put("actionCard", object_actionCard1);
        object.put("msgtype", "actionCard");

        return send(String.valueOf(object));
    }

    //FeedCard类型
    public String feedCard(String title1, String messageURL1, String picURL1, String title2, String messageURL2, String picURL2) throws IOException {
        JSONObject object = new JSONObject();
        JSONObject object_feedCard = new JSONObject();
        JSONObject object_link1 = new JSONObject();
        JSONObject object_link2 = new JSONObject();

        object_link1.put("title", title1);
        object_link1.put("messageURL", messageURL1);
        object_link1.put("picURL", picURL1);

        object_link2.put("title", title2);
        object_link2.put("messageURL", messageURL2);
        object_link2.put("picURL", picURL2);

        JSONArray links = new JSONArray();
        links.add(object_link1);
        links.add(object_link2);
        object_feedCard.put("links", links);

        object.put("feedCard", object_feedCard);
        object.put("msgtype", "feedCard");

        return send(String.valueOf(object));
    }
}

 

相关标签: 程序开发