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

企业微信机器人实现发送消息的功能(文本消息,图片消息,图片+文字消息)

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

企业微信机器人实现发送消息的功能(文本消息,图片消息,图片+文字消息)

1.打开企业微信电脑版点击红框位置添加机器人

2.复制地址

3.代码环节Controller
@GetMapping(value = "testRobot")
public void testRobot() throws IOException {
    //VO这段可以写在业务逻辑层
    RobotVo vo = new RobotVo();
    //机器人地址
    vo.setWebhookAddress("刚才复制的地址");
    //1.第一种情况:发送文本消息
    // vo.setContent("我发送的消息是:文本消息");
    // List memberList = new ArrayList<>();
    // memberList.add("@all");
    // vo.setMemberList(memberList);
    // vo.setMsgType("text");

    // 2.第二种情况,发送图片消息
    // vo.setMsgType("image");
    // vo.setSavePath("C:/Users/Administrator/Desktop/吴彦祖.jpg");

    //3.第三种情况,发送机器人消息
    vo.setMsgType("news");
    vo.setTitle("test");
    vo.setDescription("test");
    vo.setUrl("url");
    // vo.setImageUrl("url")括号中的url是上传oss返回的路径
    vo.setImageUrl("url");
    service.run(vo);
}
4.VO
@Data
public class RobotVo {
    
    private String robotId;
    
    private String robotName;
    
    private String webhookAddress;
    
    private String msgType;
    
    private String content;
    
    private List memberList;
    
    private String mobileList;
    
    private String imageUrl;
    
    private String imagebase64Value;

    
    private String imageMd5Value;

    
    private String title;
    
    private String description;
    
    private List imageUrlList;
    
    private String url;
    
    private List contentList;
    
    private String savePath;
}
5.Service接口
 	
    void run(RobotVo vo) throws IOException;
6.Service业务逻辑
    @Override
    public void run(RobotVo vo) throws IOException {
        List memberList = vo.getMemberList();
        String jsonData = "";
        String mobileList = "";
        String strMember = "";
        if (vo.getMsgType().equals("text")) {
            if (!Strings.isNullOrEmpty(vo.getMobileList())) {
                mobileList = vo.getMobileList();
            } else {
                mobileList = "";
            }
            for (int i = 0; i < memberList.size(); i++) {
                if (i == memberList.size() - 1) {
                    strMember += """ + memberList.get(i) + """;
                } else {
                    strMember += """ + memberList.get(i) + """ + ",";
                }
            }
            String[] members = new String[memberList.size()];
            for (int i = 0; i < memberList.size(); i++) {
                members[i] = memberList.get(i);
            }
            jsonData = "{n" +
                    "t"msgtype": "" + vo.getMsgType() + "",n" +
                    "    "text": {n" +
                    "        "content": "" + vo.getContent() + "",n" +
                    "        "mentioned_list":[" + strMember + "],n" +
                    "        "mentioned_mobile_list":["" + mobileList + ""]n" +
                    "    }n" +
                    "}";

        } else if (vo.getMsgType().equals("image")) {
            //图片base64加密的值
            vo.setImagebase64Value(getImageStr(vo.getSavePath()));
            //图片md5加密的值
            vo.setImageMd5Value(DigestUtils.md5Hex(new FileInputStream(vo.getSavePath())));
            jsonData = "{n" +
                    "    "msgtype": "" + vo.getMsgType() + "",n" +
                    "    "image": {n" +
                    "        "base64": "" + vo.getImagebase64Value() + "",n" +
                    "        "md5": "" + vo.getImageMd5Value() + ""n" +
                    "    }n" +
                    "}";
        } else if (vo.getMsgType().equals("news")) {
            //图片+文字消息
            vo.setTitle(!Strings.isNullOrEmpty(vo.getTitle()) ? vo.getTitle() : "");
            jsonData = "{n" +
                    "    "msgtype": "" + vo.getMsgType() + "",n" +
                    "    "news": {n" +
                    "       "articles" : [n" +
                    "           {n" +
                    "               "title" : "" + vo.getTitle() + "",n" +
                    "               "description" : "" + vo.getDescription() + "",n" +
                    "               "url" : "" + vo.getUrl() + "",n" +
                    "               "picurl" : "" + vo.getImageUrl() + ""n" +
                    "           }n" +
                    "        ]n" +
                    "    }n" +
                    "}";
        }
        send(vo.getWebhookAddress(), jsonData);
    }
7.将图片地址转化成base64编码
    
    public static String getImageStr(String imgFile) {
       //要想发送图片内容需要将图片加密转化成base64编码
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 图片加密
        base64.Encoder er= base64.getEncoder();
        return er.encodeToString(data);
    }
8.send方法
  public static JSONObject send(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        JSONObject jsonObject = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流(设置请求编码为UTF-8)
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 获取请求返回数据(设置返回数据编码为UTF-8)
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            jsonObject = JSONObject.parseObject(result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return jsonObject;
    }
9.效果图



end ~

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

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

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