Commit bd088915 authored by wujipeng's avatar wujipeng
Browse files

企业微信@指定人消息

parent 459a6ae7
...@@ -128,6 +128,11 @@ ...@@ -128,6 +128,11 @@
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>5.2.5</version> <version>5.2.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
......
package com.ruzhoukj.sms.common;
import org.apache.http.client.methods.CloseableHttpResponse;
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.apache.http.util.EntityUtils;
import java.util.Arrays;
public class WeChatRobotSender {
private final String webhookUrl;
public WeChatRobotSender(String webhookUrl) {
this.webhookUrl = webhookUrl;
}
public void sendTextMessage(String content, String[] mentionedList, String[] mentionedMobileList) throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(this.webhookUrl);
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
// 构建JSON请求体
String jsonMsg = buildTextMessageJson(content, mentionedList, mentionedMobileList);
System.out.println("请求体: " + jsonMsg);
StringEntity entity = new StringEntity(jsonMsg, "UTF-8");
httpPost.setEntity(entity);
// 发送请求并获取响应
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("响应结果: " + responseBody);
}
}
}
private String buildTextMessageJson(String content, String[] mentionedList, String[] mentionedMobileList) {
// 使用字符串拼接或Json库(如Jackson、Gson)构建JSON更稳妥,此处为示例
StringBuilder json = new StringBuilder();
json.append("{");
json.append("\"msgtype\": \"text\",");
json.append("\"text\": {");
json.append("\"content\": \"").append(content).append("\"");
if (mentionedList != null && mentionedList.length > 0) {
json.append(", \"mentioned_list\": ").append(Arrays.toString(mentionedList).replace("[", "[\"").replace("]", "\"]").replace(", ", "\", \""));
}
if (mentionedMobileList != null && mentionedMobileList.length > 0) {
json.append(", \"mentioned_mobile_list\": ").append(Arrays.toString(mentionedMobileList).replace("[", "[\"").replace("]", "\"]").replace(", ", "\", \""));
}
json.append("}");
json.append("}");
return json.toString();
}
public static void main(String[] args) {
String webhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=04c5f353-33a7-4cb0-8d00-798dee5df953";
WeChatRobotSender sender = new WeChatRobotSender(webhookUrl);
try {
// 发送文本消息,并@指定手机号的成员和所有人
// sender.sendTextMessage("这个月的运维工作记得做", null, new String[]{"19825040761", "@all"});
// sender.sendTextMessage("这个月的运维工作记得做", null, new String[]{"19825040761"});
// sender.sendTextMessage("这个月的运维工作记得做", null, new String[]{"19825040761"});
} catch (Exception ignored) {
}
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ package com.ruzhoukj.sms.controller; ...@@ -2,6 +2,7 @@ package com.ruzhoukj.sms.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.platform.model.resp.Result; import com.platform.model.resp.Result;
import com.ruzhoukj.sms.common.WeChatRobotSender;
import com.ruzhoukj.sms.entity.CertificateEntity; import com.ruzhoukj.sms.entity.CertificateEntity;
import com.ruzhoukj.sms.entity.kzReminderEntity; import com.ruzhoukj.sms.entity.kzReminderEntity;
import com.ruzhoukj.sms.pojo.dto.CertificateDTO; import com.ruzhoukj.sms.pojo.dto.CertificateDTO;
...@@ -136,4 +137,17 @@ public class CertificateController { ...@@ -136,4 +137,17 @@ public class CertificateController {
return Result.data(null); return Result.data(null);
} }
@GetMapping("/specified/send")
public Result<Boolean> send(@RequestParam("phone") String phone, @RequestParam("content") String content) {
String webhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=04c5f353-33a7-4cb0-8d00-798dee5df953";
WeChatRobotSender sender = new WeChatRobotSender(webhookUrl);
try {
// 发送文本消息,并@指定手机号的成员
sender.sendTextMessage(content, null, new String[]{phone});
return Result.data(true);
} catch (Exception ignored) {
return Result.data(false);
}
}
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment