釘釘推送模板


釘釘推送java類模板

依賴jar包:

httpclient-4.5.5.jar
httpcore-4.4.1.jar
httpclient-cache-4.1.3.jar

package base.util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

/**
 * @author 馬家立
 * @version 創建時間:2020年2月4日上午10:27:34
 * @Description:TODO 釘釘推送Util
 */
public class DingDingSendUtil {
    static Logger forPk = Logger.getLogger("forPk");
    //推送到釘釘的department群指令
    static String pk_WEBHOOK_TOKEN ="https://oapi.dingtalk.com/robot/send?access_token=略";
    
    /**
     * @Title:sendDingPkMessage
     * @author:馬家立
     * @date:2020年2月4日 上午10:33:24  
     * @Description:TODO 釘釘推送排課數據
     * @param sendContent--待推送的排課數據信息
     * @param WEBHOOK_TOKEN--推送到釘釘的群指令
     * @return--String
     */
    public static String sendDingPkMessage(String sendContent) {
        forPk.info("進入DingDingSendBean的sendDingPkMessage");
        try {
            //單獨推送人的手機號
            String sendPhone = "";
            //是否@所有人(true為所有人,false為不@所有人)
            Boolean isAtAll = false;
            // 監控數據信息推送到釘釘群根據傳入Boolean值判斷是否@所有人
            String result = sendDingMessage(pk_WEBHOOK_TOKEN, sendContent, sendPhone, isAtAll);
            return result;
        } catch (Exception e) {
            forPk.error("DingDingSendBean的sendDingPkMessage err", e);
            return "error";
        }
    }
    /**
     * @Title:sendDingMessage
     * @author:馬家立
     * @date:2020年2月4日 上午10:37:15  
     * @Description:TODO 監控數據信息推送到釘釘群根據傳入Boolean值判斷是否@所有人
     * @param WEBHOOK_TOKEN--釘釘群機器指令(http格式)
     * @param sendContent--釘釘群機器指令(http格式)
     * @param sendPhone--推送手機號
     * @param isAtAll--是否@所有人(true為所有人,false為不@所有人)
     * @return--String
     */
    public static String sendDingMessage(String WEBHOOK_TOKEN, String sendContent, String sendPhone, Boolean isAtAll) {
        forPk.info("進入DingDingSendBean的sendDingMessage");
        try {
            String result = "";
            // new一個post的http (Ps:WEBHOOK_TOKEN是群機器人的推送指令)
            HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
            httppost.addHeader("Content-Type", "application/json; charset=utf-8");
            // 釘釘參數格式拼接
            String sendMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + sendPhone + "" + sendContent
                + "\"}, \"at\":{\"atMobiles\":[\"" + sendPhone + "\"],  \"isAtAll\": " + isAtAll + "}}";
            // 設置編碼格式utf-8
            StringEntity encode = new StringEntity(sendMsg, "utf-8");
            httppost.setEntity(encode);
            // 創建http客戶端
            HttpClient httpclient = HttpClients.createDefault();
            // 響應http客戶端
            HttpResponse response = httpclient.execute(httppost);
            forPk.info("response.getStatusLine().getStatusCode()==" + response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            } else {
                result = "釘釘信息推送失敗";
            }
            forPk.info("釘釘信息發送結果:" + result);
            return result;
        } catch (Exception e) {
            forPk.error("DingDingSendBean的sendDingMessage err", e);
            return "error";
        }
    }
    
    /**
     * @Title:main
     * @author:馬家立
     * @date:2019年1月9日 下午5:49:12
     * @Description:main函數測試
     * @param:@param args
     */
    public static void main(String[] args) {
        DingDingSendUtil.sendDingPkMessage("排課測試推送釘釘");
        System.out.println("over");
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM