1.先在群里添加机器人,然后获取机器人的webhook地址:
假设webhook是:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
2.有多种方式发送群消息,可以采用curl,也可以采用发送http请求的方式,我这里采用okhttp发送http请求,pom如下:
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.0</version> </dependency>
调用okhttp核心代码如下:
/** * @content:要发送的消息 * WECHAT_GROUP:机器人的webhook */ public static String callWeChatBot(String content) { OkHttpClient client = new OkHttpClient() .newBuilder() .build(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(content,mediaType); Request request = new Request.Builder() .url(WECHAT_GROUP) .method("POST", body) .addHeader("Content-Type", "application/json") .build(); Response response = null; try { response = client.newCall(request).execute(); } catch (IOException e) { e.printStackTrace(); } return response.message(); }
3.生成核心方法的参数,
如果发送markdown格式的消息:
public static String generalBotBodyInfo(File f1, File f2, String cusenv, String myenv) { String content = "**信息摘要:** \n" + ">文件1:<font color=\\\"info\\\">" + f1.getName() + "</font>\n " + ">服务器路径:<font color=\\\"info\\\">" + f1.getAbsolutePath() + "</font>\n" + ">文件传输环境:<font color=\\\"info\\\">" + myenv + "</font>\n " + ">客户接受环境:<font color=\\\"info\\\">" + cusenv + "</font>\n" + ">时间:<font color=\\\"info\\\">" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "</font>"; String textTemplate = "{\n" + " \"msgtype\": \"markdown\",\n" + " \"markdown\": {\n" + " \"content\": \"" + content + "\",\n" + " }\n" + "}"; return textTemplate; }
如果发送text格式的消息:
public static String generalBotBodyAT() { String textTemplate = "{\n" + " \"msgtype\": \"text\",\n" + " \"text\": {\n" + " \"content\": \"今天下午大暴雨\",\n" + " \"mentioned_list\":[\"@all\"],\n" + " }\n" + "}"; return textTemplate; }
如果发送图片,不能超过2M,支持的格式为JPG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htld44Z40Bp1nmjRV/wxkYf8/kOQ86CK1jYDrLcIi7ZVoznZIBIyPA4FBRnAa5uVQdYXZMHZAAPWAED9s+VBZ0oqJpzuBYL2eyCMEg5wBigkefGpq+4wqOpZ/Bic/YfGgJA5oFoEoG+z8KBRQVtUdksJWQkHHEUAOVymoiEYMaRkKrKGAGR40B+zdnt0LHJxQM0/8O3/ACP/AOjQC9SlcXFwQ2CnV7Pu7YqBzSPCZTGxUtbrI2ObeNARigi9BCbA2WU5Hjkb6oZpMjyWSs7FiCRk+6gvUHUH/9k="; String md5 = "862715fb36992d6746641ff02ee85b8c"; String textTemplate = "{\n" + " \"msgtype\": \"image\",\n" + " \"image\": {\n" + " \"base64\": \"" + base64 + "\",\n" + " \"md5\": \"" + md5 + "\"\n" + " }\n" + "}"; return textTemplate; }
发送图片需要对图片进行base64编码并计算图片的md5值,把计算的方法也一并献上,需要注意图片不要过大,不然base64编码的结果会超级长,甚至超过String的最大长度,java.util.Base64这个工具类有现成的,直接用就行
public static String getFileBase64(String path){ try { FileInputStream inputStream = new FileInputStream(new File(path)); byte [] bs = new byte[inputStream.available()]; inputStream.read(bs); return Base64.getEncoder().encodeToString(bs); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } return null; }
标签:发送,textTemplate,java,String,微信群,机器人,return,content,new From: https://www.cnblogs.com/tiandi/p/17768638.html