栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

企业微信机器人自动消息发送webhook接入代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

企业微信机器人自动消息发送webhook接入代码

主要代码类如下

package com.test.common;

import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;

import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; 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.json.JSONObject;

/*

  • 文件名:com.test.common;.SendRobotMsgUtils.java
  • 简述:TODO
  • 详述:
  • 新建时间:2022年5月2日 下午12:31:41
  • 修改内容:[新增]
  • 修改时间:2022年5月2日 下午12:31:41
  • 版本:1.0
  • / public class SendRobotMsgUtils {
@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean wechatRobotTxtMsg(String webhookUrl,String msgContent,List mentioned_mobile_list) {
    boolean falg=false;
    //webhook地址 
    String msgtype="text";
    HttpResponse response=null;
    try {
        if(webhookUrl!=null&&msgContent!=null) {
        HttpPost httpPost=new HttpPost(webhookUrl);
        CloseableHttpClient httpclient=HttpClients.createDefault();
        httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
        Map param=new HashMap();
        param.put("msgtype",msgtype);
        Map param_msg=new HashMap();
        param_msg.put("content", msgContent);
        if(mentioned_mobile_list!=null&&mentioned_mobile_list.size()>0) {
            param_msg.put("mentioned_mobile_list", mentioned_mobile_list);
        }
        //封装消息体
        param.put(msgtype, param_msg);
        String txtmsg=JSONObject.valueToString(param);
        System.out.println("*************执行前txtmsg:"+txtmsg);
        StringEntity entity = new StringEntity(txtmsg, "utf-8");
        httpPost.setEntity(entity);
        System.out.println("*************执行前:"+httpPost.toString());
        response=httpclient.execute(httpPost);
        if(response!=null&&response.getStatusLine()!=null&&response.getStatusLine().getStatusCode()==200) {
            String responseStr=response.getEntity().toString();
            falg=true;
            System.out.println("*************执行结果:"+responseStr);
        }
        }else {
            System.out.println("*************输入参数为空********************************");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return falg;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean wechatRobotMarkdownMsg(String webhookUrl,String msgContent,List mentioned_mobile_list) {
    boolean falg=false;
    //webhook地址 
    String msgtype="markdown";
    HttpResponse response=null;
    try {
        if(webhookUrl!=null&&msgContent!=null) {
        HttpPost httpPost=new HttpPost(webhookUrl);
        CloseableHttpClient httpclient=HttpClients.createDefault();
        httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
        Map param=new HashMap();
        param.put("msgtype",msgtype);
        Map param_msg=new HashMap();
        param_msg.put("content", msgContent);
        if(mentioned_mobile_list!=null) {
            param_msg.put("mentioned_mobile_list", mentioned_mobile_list);
        }
        //封装消息体
        param.put(msgtype, param_msg);
        String txtmsg=JSONObject.valueToString(param);
        System.out.println("*************执行前txtmsg:"+txtmsg);
        StringEntity entity = new StringEntity(txtmsg, "utf-8");
        httpPost.setEntity(entity);
        System.out.println("*************执行前:"+httpPost.toString());
        response=httpclient.execute(httpPost);
        if(response!=null&&response.getStatusLine()!=null&&response.getStatusLine().getStatusCode()==200) {
            String responseStr=response.getEntity().toString();
            falg=true;
            System.out.println("*************执行结果:"+responseStr);
        }
        }else {
            System.out.println("*************输入参数为空********************************");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return falg;
}


public static void main(String[] args) {
    SendRobotMsgUtils srmu=new SendRobotMsgUtils();
    SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:SSS");
    String nowstr=sdf.format(new Date());
    String webhookUrl="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c17661d0c2e6";
    String msgContent="监控机器人-普通文本消息n消息时间:"+nowstr+";n交易日期+支付渠道+渠道笔数+当日总笔数n"
            + "你们所有人可以关闭此群消息!!!";
    List mlist=new ArrayList();
    mlist.add("@all");
    mlist.add("18688888888");

// srmu.wechatRobotTxtMsg(webhookUrl,msgContent,mlist); String msgmkContent= "

监控名称rn" + " rn" + " 编号rn" + " 用户名rn" + " 姓名rn" + " 联系电话rn" + " rn" + " rn" + " rn" + " 01rn" + " 张三rn" + " 姓名rn" + " 18688888888rn" + " rn" + "rn"; srmu.wechatRobotMarkdownMsg(webhookUrl, msgmkContent, mlist);

}

} 实际开发完成的功能效果可以访问:https://www.onekbit.com/wechatrobotinfo/selectlist.do 体验使用。

使用微信机器人定时群发消息:

本文由博客一文多发平台 OpenWrite 发布!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988428.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号