企业微信机器人通知

相关链接
https://work.weixin.qq.com/api/doc/90000/90136/91770
 

频率限制每分钟20条,适合触发较少的场景.

//直接撸代码
 

public class QyWxMsgUtil {

    private static String MSGURL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=机器人的key";

    private static String MSG="{%s},hhh!\n>  创建时间: %s";
  

    public static Map sendRfqMsg(String content) throws Exception{
       return sendMsg(String.format(MSG,content, "2020-03-11 12:12:12")));
    }


    public static Map sendMsg(String content) throws Exception{
        HashMap headers = new HashMap();
        headers.put("Content-Type", "application/json");

        HashMap param = new HashMap();
        param.put("content",content);

        E body = new E();
        body.put("msgtype", "markdown");
        body.put("markdown", param);
        String bodyStr = JsonUtil.writeValueAsString(body);
        Map result = OkHttpUtils.sendPost(MSGURL,bodyStr,headers);
        if (!StringUtils.equals("0",result.get("errcode").toString())){
            System.out.println("fail request");
            result = sendPost(MSGURL,bodyStr,headers);
        }
        return result;
    }
public static Map sendPost(String url, String body,Map headers) throws Exception {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        int statusCode = 200;
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            Iterator iterator = headers.entrySet().iterator();
            while(iterator.hasNext()) {
                Map.Entry query = (Map.Entry)iterator.next();
                conn.setRequestProperty((String)query.getKey(), query.getValue().toString());
            }
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(body);
//            System.out.println("【请求参数:】"+body);
//            System.out.println("【请求头部:】"+headers);
            out.flush();
            statusCode = ((HttpURLConnection)conn).getResponseCode();
            if(statusCode != 200) {
                in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        if (statusCode != 200) {
            throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
        }
//        System.out.println("【sendPost返回结果:】" + result);
        if(StringUtils.isEmpty(result)){
            return null;
        } else {
            return JsonUtil.convertJson(result,Map.class);
        }
    }

 

你可能感兴趣的:(JAVA)