1. 程式人生 > 程式設計 >基於java使用釘釘機器人向釘釘群推送訊息

基於java使用釘釘機器人向釘釘群推送訊息

這篇文章主要介紹了基於java使用釘釘機器人向釘釘群推送訊息,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

第一步、登入釘釘電腦版,獲得釘釘機器人的webhook;

第二步,用java傳送post請求給釘釘完成訊息推送

package com.thinkgem.wlw.modules.lhjh.DingTalk;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
 * @Author: zhouhe
 * @Date: 2019/6/20 14:49
 */
public class SendHttps {
  private static Logger logger = LoggerFactory.getLogger(SendHttps.class);
  /**
   * 傳送POST請求,引數是Map,contentType=x-www-form-urlencoded
   *
   * @param url
   * @param mapParam
   * @return
   */
  public static String sendPostByMap(String url,Map<String,Object> mapParam) {
    Map<String,String> headParam = new HashMap();
    headParam.put("Content-type","application/json;charset=UTF-8");
    return sendPost(url,mapParam,headParam);
  }

  /**
   * 向指定 URL 傳送POST方法的請求
   *
   * @param url  傳送請求的 URL
   * @param param 請求引數,
   * @return 所代表遠端資源的響應結果
   */
  public static String sendPost(String url,Object> param,String> headParam) {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
      URL realUrl = new URL(url);
      // 開啟和URL之間的連線
      URLConnection conn = realUrl.openConnection();
      // 設定通用的請求屬性 請求頭
      conn.setRequestProperty("accept","*/*");
      conn.setRequestProperty("connection","Keep-Alive");
      conn.setRequestProperty("user-agent","Fiddler");

      if (headParam != null) {
        for (Entry<String,String> entry : headParam.entrySet()) {
          conn.setRequestProperty(entry.getKey(),entry.getValue());
        }
      }
      // 傳送POST請求必須設定如下兩行
      conn.setDoOutput(true);
      conn.setDoInput(true);
      // 獲取URLConnection物件對應的輸出流
      out = new PrintWriter(conn.getOutputStream());
      // 傳送請求引數
      out.print(JSON.toJSONString(param));
      // flush輸出流的緩衝
      out.flush();
      // 定義BufferedReader輸入流來讀取URL的響應
      in = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      logger.info("傳送 POST 請求出現異常!" + e);
      e.printStackTrace();
    }
    //使用finally塊來關閉輸出流、輸入流
    finally {
      try {
        if (out != null) {
          out.close();
        }
        if (in != null) {
          in.close();
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    return result;
  }
}

第三步,編寫測試類

package com.thinkgem.wlw.modules.lhjh.DingTalk;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhouhe
 * @Date: 2019/6/20 14:52
 */
public class SendMessage {
  public static void main(String[] args){

    // 釘釘的webhook
    String dingDingToken="https://oapi.dingtalk.com/robot/send?access_token=0f0daca33m98gn78f00189fe1e1e908b81fa26d0d8ddd48fa78a844cd8636187";
    // 請求的JSON資料,這裡我用map在工具類裡轉成json格式
    Map<String,Object> json=new HashMap();
    Map<String,Object> text=new HashMap();
    json.put("msgtype","text");
    text.put("content","臨渙焦化:VOCs排放濃度大於上限:61.89");
    json.put("text",text);
    // 傳送post請求
    String response = SendHttps.sendPostByMap(dingDingToken,json);
    System.out.println("相應結果:"+response);

  }
}

測試結果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。