加入群聊

This commit is contained in:
Leo
2025-04-05 14:38:09 +08:00
parent c7534437d8
commit 01d7b10f5a
2 changed files with 79 additions and 55 deletions

View File

@@ -35,6 +35,7 @@ public class WXUtil {
public static String default_bot_wxid = "wxid_kr145nk7l0an31";
public static Map<String, SuperAdmin> super_admins = new HashMap<>();
public static Map<String, String> jdidToWxidMap = new HashMap<>();
public static List<String> chatRoomList = new ArrayList<>();
/**
* url http://127.0.0.1:7777/DaenWxHook/httpapi/
* 获取微信列表 (X0000)
@@ -105,6 +106,8 @@ public class WXUtil {
SuperAdmin admin2 = new SuperAdmin("wxid_yneqf1implxu12", "", "2025353364", "e3c161242c8a1416fada5b5564d7ee70", "41ae9aabf03b41e6ba309682e36b323e");
super_admins.put(admin2.getWxid(), admin2);
jdidToWxidMap.put(admin2.getUnionId(), admin2.getWxid());
chatRoomList.add("50400969285@chatroom");
//wxTsUtil.sendNotify("initSuperAdmins 初始化完成");
//发送通知到微信
sendTextMessage(default_super_admin_wxid, "Jarvis 重启完成 [亲亲][亲亲][亲亲] ", 1, default_super_admin_wxid, false);

View File

@@ -3,11 +3,14 @@ package cn.van.business.util;
import cn.van.business.enums.FromType;
import cn.van.business.model.wx.WxMessage;
import cn.van.business.model.wx.WxMessageDataForChat;
import cn.van.business.model.wx.WxUser;
import cn.van.business.repository.SettingRepository;
import cn.van.business.repository.WxMessageDataForChatRepository;
import cn.van.business.repository.WxUserRepository;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,11 +19,14 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static cn.van.business.util.WXUtil.chatRoomList;
import static cn.van.business.util.WXUtil.super_admins;
@@ -189,29 +195,7 @@ public class WxMessageConsumer {
// wxUtil.sendTextMessage(wxid, result, msgType, null);
//
//}
private static String getUrlStr(String msg) {
//String urlPattern = "https?://[\\w-\\.]+(\\.[a-z]{2,})?(/[\\w-./?%&=]*)?"
String urlPattern = "https?://\\S+?\\.(html|htm)(\\?\\S*?)?";
Pattern pattern = Pattern.compile(urlPattern);
Matcher matcher = pattern.matcher(msg);
// 检查是否存在URL如果存在则打印出来
String finallyUrl = null;
if (matcher.find()) {
finallyUrl = matcher.group();
System.out.println("Extracted URL: " + finallyUrl);
} else {
System.out.println("No URL found in the given text.");
}
if (finallyUrl != null && finallyUrl.endsWith("?")) {
// 移除最后一个字符(即问号)
finallyUrl = finallyUrl.substring(0, finallyUrl.length() - 1);
}
if (finallyUrl != null && finallyUrl.contains("item.m.jd.com/product")) {
finallyUrl = finallyUrl.replace("item.m.jd.com/product", "item.jd.com");
}
return finallyUrl;
}
@Async("threadPoolTaskExecutor")
public void consume(WxMessage wxMessage) throws Exception {
@@ -230,7 +214,7 @@ public class WxMessageConsumer {
if (FromType.PRIVATE.getKey().equals(wxMessage.getEvent())) {
handlePrivateMessage(wxMessage);
} else if (FromType.GROUP.getKey().equals(wxMessage.getEvent())) {
//handleGroupMessage(wxMessage);
handleGroupMessage(wxMessage);
}
//if (event.equals(EventType.TRANSFER_EVENT.getKey())) {
// handleTransferEvent(wxMessage);
@@ -244,13 +228,10 @@ public class WxMessageConsumer {
* @param wxMessage
*/
private void handlePrivateMessage(WxMessage wxMessage) throws Exception {
Integer msgType = 1;
logger.info("处理消息: {}", JSON.toJSONString(wxMessage));
logger.info("处理私聊消息: {}", JSON.toJSONString(wxMessage));
WxMessage.DataSection data = wxMessage.getData();
WxMessage.DataSection.InnerData innerData = data.getData();
Integer event = wxMessage.getEvent();
if (Util.isAnyEmpty(innerData.getMsg(), innerData.getFromWxid())) {
logger.info("消息内容为空,不处理");
return;
@@ -267,30 +248,9 @@ private void handlePrivateMessage(WxMessage wxMessage) throws Exception {
String msg = innerData.getMsg();
//if (msg.contains("【京东】")) {
// logger.info("消息包含【京东】,处理转链消息");
// String wxid;
// if (Objects.equals(event, fromGR)) {
// wxid = innerData.getFromWxid();
// } else {
// wxid = innerData.getFinalFromWxid();
// }
//
// String finallyUrl = getUrlStr(msg);
// if (Util.isNotEmpty(finallyUrl)) {
// logger.info("提取到URL{}", finallyUrl);
// String transferResultUrl = jdUtils.transfer(finallyUrl);
// wxUtil.sendTextMessage(wxid, transferResultUrl, msgType, null);
// } else {
// logger.info("未提取到URL");
// }
// return;
//} else {
// logger.info("消息不包含【京东】");
//}
if (msg.startsWith("")) {
logger.info("消息以京开头,处理京东订单消息");
logger.info("消息以京开头,处理京东指令消息");
jdUtils.sendOrderToWxByOrderJD(msg.replace("", ""), fromWxid);
return;
} else {
@@ -300,7 +260,67 @@ private void handlePrivateMessage(WxMessage wxMessage) throws Exception {
logger.info("未命中前置指令,开始命中 Default 流程");
jdUtils.sendOrderToWxByOrderDefault(msg, fromWxid);
}
private void handleGroupMessage (WxMessage wxMessage) {
logger.info("处理群聊消息: {}", JSON.toJSONString(wxMessage));
WxMessage.DataSection data = wxMessage.getData();
WxMessage.DataSection.InnerData innerData = data.getData();
if (Util.isAnyEmpty(innerData.getMsg(), innerData.getFromWxid())) {
logger.info("消息内容为空,不处理");
return;
} else {
logger.info("消息内容:{}", innerData.getMsg());
}
String fromWxid = innerData.getFromWxid();
if ( !chatRoomList.contains(fromWxid)) {
logger.info("不是白名单群聊,不处理");
return;
}
String msg = innerData.getMsg();
if (msg.startsWith("")) {
logger.info("消息以京开头,处理京东指令消息");
jdUtils.sendOrderToWxByOrderJD(msg.replace("", ""), fromWxid);
return;
} else {
logger.info("消息不以京开头");
}
logger.info("未命中前置指令,开始命中 Default 流程");
jdUtils.sendOrderToWxByOrderDefault(msg, fromWxid);
/**
* {
* "event": 10008,
* "wxid": "wxid_nq6r0w9v12612",
* "data": {
* "type": "recvMsg",
* "des": "收到消息",
* "data": {
* "timeStamp": "1716619844146",
* "fromType": 2,
* "msgType": 1,
* "msgSource": 0,
* "fromWxid": "34457730396@chatroom",
* "finalFromWxid": "wxid_3sq4tklb6c3121",
* "atWxidList": ["wxid_nq6r0w9v12612", "wxid_s4icha2bm2zg12"],
* "silence": 0,
* "membercount": 3,
* "signature": "V1_1YiTEwOY|v1_1YiTEwOY",
* "msg": "@忆白\\u2005@小鹿\\uD83D\\uDE00\\uD83D\\uDE00摸\\u2005你好\\uD83E\\uDD23世界",
* "msgId": "7053704521966022857",
* "sendId": ""
* },
* "timestamp": "1716619844147",
* "wxid": "wxid_nq6r0w9v12612",
* "port": 8888,
* "pid": 3944,
* "flag": "7888"
* }
* }*/
}
//else if (msg.startsWith("美团 ")) {
// logger.info("处理美团的消息");
// msg = msg.substring(msg.indexOf("https://i.meituan.com/mttouch/page/account"));
@@ -393,12 +413,13 @@ private void handlePrivateMessage(WxMessage wxMessage) throws Exception {
//}
// wxMessageDataForChatService.save(wxMessageDataForChat);
/**
* @param wxMessage
* @return
* @throws
* @description 处理群聊消息
*/
/**
* @param wxMessage
* @return
* @throws
* @description 处理群聊消息
*/
// private void handleGroupMessage (WxMessage wxMessage){
// Integer msgType = 2;
// /**