企业微信机器人通知

相关链接
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="<font color=\"warning\">{%s}</font>,hhh!\n>  创建时间: <font color=\"comment\">%s</font>";
  

    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<String,String> 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);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值