Java 实现企业微信群机器人功能

官方解读

 

企业微信群机器人官方配置说明文档:群机器人配置说明文档

自定义机器人支持文本(text)、markdown(markdown)、图片(image)、图文(news)、文件(file)、语音(voice)、模板卡片(template_card)七种消息类型。

1.文本类型

 

2.markdown类型

3.图片类型

4.图文类型

 5.文件类型

 6.语音类型

 7.模板卡片类型

获取群地址

 特别特别要注意:一定要保护好机器人的webhook地址,避免泄漏!不要分享到github、博客等可被公开查阅的地方,否则坏人就可以用你的机器人来发垃圾消息了。

Java 实现文本类型

官方示例

{
    "msgtype": "text",
    "text": {
        "content": "广州今日天气:29度,大部分多云,降雨概率:60%",
		"mentioned_list":["wangqing","@all"],
		"mentioned_mobile_list":["13800001111","@all"]
    }
}
参数是否必填说明
msgtype 消息类型,此时固定为text
content 文本内容,最长不超过2048个字节,必须是utf8编码
mentioned_list userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
mentioned_mobile_list 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人

 

 个人自定义实现

// 1. 设置请求URL
// 替换成你的机器人webhook地址
String webhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=*************";
URL url = new URL(webhookUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 2. 设置请求方法为POST
connection.setRequestMethod("POST");

// 3. 设置请求头
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);

// 4. 设置发送消息内容

JSONObject inputJson = new JSONObject();
inputJson.put("msgtype","text");

//4.1.  手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
List<String> mobilelist = new ArrayList<>();
mobilelist.add("176********");
mobilelist.add("@all");

String content = "【测试信息】快递:圆通(YTO)面单余额低于预警值,请尽快处理。\n";
content +=">面单所属平台:菜鸟电子面单\n";
content +=">面单所剩余额:999\n";
JSONObject testJson = new JSONObject();
testJson.put("content",content);
testJson.put("mentioned_mobile_list",mobilelist);

inputJson.put("text",testJson);
String jsonInputString = inputJson.toJSONString();

// 5. 发送POST输出
try (OutputStream os = connection.getOutputStream()) {
    byte[] input = jsonInputString.getBytes("utf-8");
    os.write(input, 0, input.length);
}

// 6. 获取响应码
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);

实现效果

Java 实现markdown类型

官方示例

{
    "msgtype": "markdown",
    "markdown": {
        "content": "实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
         >类型:<font color=\"comment\">用户反馈</font>
         >普通用户反馈:<font color=\"comment\">117例</font>
         >VIP用户反馈:<font color=\"comment\">15例</font>"
    }
}
参数是否必填说明
msgtype 消息类型,此时固定为markdown
content markdown内容,最长不超过4096个字节,必须是utf8编码

个人自定义实现

// 1. 设置请求URL
// 替换成你的机器人webhook地址
String webhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=*************";
URL url = new URL(webhookUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 2. 设置请求方法为POST
connection.setRequestMethod("POST");

// 3. 设置请求头
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);

// 4. 设置发送消息内容
JSONObject inputJson = new JSONObject();
inputJson.put("msgtype","markdown");

String content = "【测试信息】快递:<font color=\\\"warning\\\">圆通(YTO)</font>面单余额<font color=\\\"warning\\\">低于预警值,请尽快处理</font>\n";
content +=">面单所属平台:<font color=\\\"comment\\\">菜鸟电子面单</font>\n";
content +=">面单所剩余额:<font color=\\\"comment\\\">999</font>\n";

JSONObject markdownJson = new JSONObject();
markdownJson.put("content",content);

inputJson.put("markdown",markdownJson);

String jsonInputString = inputJson.toJSONString();

// 5. 发送POST输出
try (OutputStream os = connection.getOutputStream()) {
    byte[] input = jsonInputString.getBytes("utf-8");
    os.write(input, 0, input.length);
}

// 6. 获取响应码
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);

实现效果

 

 

 

posted @ 2025-04-27 11:08  凉年技术  阅读(275)  评论(0)    收藏  举报