机器人API添加根据邮箱获取用户信息和批量获取用户信息的接口

This commit is contained in:
heavyrian2012
2026-04-29 15:31:59 +08:00
parent c7f7ce949e
commit 776c9e26c6
5 changed files with 221 additions and 0 deletions
@@ -0,0 +1,87 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package com.xiaoleilu.loServer.action.robot;
import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.common.ErrorCode;
import cn.wildfirechat.pojos.InputOutputUserInfo;
import cn.wildfirechat.pojos.InputStringList;
import cn.wildfirechat.pojos.OutputUserInfoList;
import cn.wildfirechat.proto.WFCMessage;
import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.annotation.HttpMethod;
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;
import java.util.ArrayList;
import java.util.List;
@Route(APIPath.Robot_User_Batch_Get_Infos)
@HttpMethod("POST")
public class GetBatchUserAction extends RobotAction {
@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputStringList inputUserId = getRequestBody(request.getNettyRequest(), InputStringList.class);
if (inputUserId != null && inputUserId.getList() != null && !inputUserId.getList().isEmpty()) {
OutputUserInfoList res = new OutputUserInfoList();
res.userInfos = new ArrayList<>();
int mask = messagesStore.getRobotGetUserInfoMask();
for (String userId : inputUserId.getList()) {
WFCMessage.User user = messagesStore.getUserInfo(userId);
if(user == null || StringUtil.isNullOrEmpty(user.getName())) {
continue;
}
InputOutputUserInfo inputOutputUserInfo = InputOutputUserInfo.fromPbUser(user);
if((mask & 1) == 0) {
inputOutputUserInfo.setName(null);
}
if((mask & 2) == 0) {
inputOutputUserInfo.setMobile(null);
}
if((mask & 4) == 0) {
inputOutputUserInfo.setEmail(null);
}
if((mask & 8) == 0) {
inputOutputUserInfo.setAddress(null);
}
if((mask & 16) == 0) {
inputOutputUserInfo.setCompany(null);
}
if((mask & 32) == 0) {
inputOutputUserInfo.setExtra(null);
}
if((mask & 64) == 0) {
inputOutputUserInfo.setUpdateDt(0);
}
if((mask & 128) == 0) {
inputOutputUserInfo.setGender(0);
}
if((mask & 256) == 0) {
inputOutputUserInfo.setSocial(null);
}
if((mask & 512) == 0) {
inputOutputUserInfo.setType(0);
}
res.userInfos.add(inputOutputUserInfo);
}
RestResult result = RestResult.ok(res);
setResponseContent(result, response);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
}
}
@@ -0,0 +1,92 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package com.xiaoleilu.loServer.action.robot;
import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.common.ErrorCode;
import cn.wildfirechat.pojos.InputOutputUserInfo;
import cn.wildfirechat.pojos.OutputUserInfoList;
import cn.wildfirechat.proto.WFCMessage;
import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.annotation.HttpMethod;
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;
import java.util.ArrayList;
import java.util.List;
@Route(APIPath.Robot_User_Get_Email_Info)
@HttpMethod("POST")
public class GetUsersByEmailAction extends RobotAction {
@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
String email = getRequestBody(request.getNettyRequest(), String.class);
if (!StringUtil.isNullOrEmpty(email)) {
List<WFCMessage.User> users = messagesStore.getUserInfosByEmail(email);
List<InputOutputUserInfo> list = new ArrayList<>();
int mask = messagesStore.getRobotGetUserInfoMask();
for (WFCMessage.User user : users) {
InputOutputUserInfo outputUserInfo = InputOutputUserInfo.fromPbUser(user);
if((mask & 1) == 0) {
outputUserInfo.setName(null);
}
if((mask & 2) == 0) {
outputUserInfo.setMobile(null);
}
if((mask & 4) == 0) {
outputUserInfo.setEmail(null);
}
if((mask & 8) == 0) {
outputUserInfo.setAddress(null);
}
if((mask & 16) == 0) {
outputUserInfo.setCompany(null);
}
if((mask & 32) == 0) {
outputUserInfo.setExtra(null);
}
if((mask & 64) == 0) {
outputUserInfo.setUpdateDt(0);
}
if((mask & 128) == 0) {
outputUserInfo.setGender(0);
}
if((mask & 256) == 0) {
outputUserInfo.setSocial(null);
}
if((mask & 512) == 0) {
outputUserInfo.setType(0);
}
list.add(outputUserInfo);
}
RestResult result;
if (list.isEmpty()) {
result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST);
} else {
OutputUserInfoList outputUserInfoList = new OutputUserInfoList();
outputUserInfoList.userInfos = list;
result = RestResult.ok(outputUserInfoList);
}
setResponseContent(result, response);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
}
}
@@ -154,6 +154,8 @@ public interface APIPath {
String Channel_Application_Get_UserInfo = "/channel/application/get_user_info"; String Channel_Application_Get_UserInfo = "/channel/application/get_user_info";
String Robot_User_Info = "/robot/user_info"; String Robot_User_Info = "/robot/user_info";
String Robot_User_Get_Email_Info = "/robot/user/get_info_by_email";
String Robot_User_Batch_Get_Infos = "/robot/user/batch_get_infos";
String Robot_Get_Profile = "/robot/profile"; String Robot_Get_Profile = "/robot/profile";
String Robot_Message_Send = "/robot/message/send"; String Robot_Message_Send = "/robot/message/send";
String Robot_Message_Reply = "/robot/message/reply"; String Robot_Message_Reply = "/robot/message/reply";
@@ -1975,6 +1975,7 @@ public class Main {
createRobot.setName(robotId); createRobot.setName(robotId);
createRobot.setDisplayName("机器人"); createRobot.setDisplayName("机器人");
createRobot.setOwner("userId1"); createRobot.setOwner("userId1");
createRobot.setEmail("[email protected]");
createRobot.setSecret(robotSecret); createRobot.setSecret(robotSecret);
createRobot.setCallback("http://127.0.0.1:8883/robot/recvmsg"); createRobot.setCallback("http://127.0.0.1:8883/robot/recvmsg");
IMResult<OutputCreateRobot> resultCreateRobot = UserAdmin.createRobot(createRobot); IMResult<OutputCreateRobot> resultCreateRobot = UserAdmin.createRobot(createRobot);
@@ -2134,6 +2135,21 @@ public class Main {
System.out.println("robot get user info by name failure"); System.out.println("robot get user info by name failure");
} }
//测试机器人通过邮箱获取用户信息
IMResult<OutputUserInfoList> resultRobotGetUserInfoByEmail = robotService.getUserInfoByEmail("[email protected]");
if (resultRobotGetUserInfoByEmail != null && resultRobotGetUserInfoByEmail.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("robot get user info by email success");
} else {
System.out.println("robot get user info by email failure");
}
IMResult<OutputUserInfoList> userInfoListIMResult = robotService.getBatchUsers(Arrays.asList("userId1", "userId2"));
if (userInfoListIMResult != null && userInfoListIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("robot get batch user info by email success");
} else {
System.out.println("robot get batch user info by email failure");
}
//创建群组信息,用于测试机器人群组管理功能 //创建群组信息,用于测试机器人群组管理功能
String groupId = "robot_group" + System.currentTimeMillis(); String groupId = "robot_group" + System.currentTimeMillis();
PojoGroupInfo groupInfo = new PojoGroupInfo(); PojoGroupInfo groupInfo = new PojoGroupInfo();
@@ -192,6 +192,30 @@ public class RobotService implements Closeable {
return robotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); return robotHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class);
} }
/**
* 根据邮箱获取用户信息列表
* @param email 邮箱地址
* @return 用户信息列表
* @throws Exception 请求失败时抛出异常
*/
public IMResult<OutputUserInfoList> getUserInfoByEmail(String email) throws Exception {
String path = APIPath.Robot_User_Get_Email_Info;
return robotHttpUtils.httpJsonPost(path, email, OutputUserInfoList.class);
}
/**
* 批量获取用户信息
* @param userIds 用户ID列表
* @return 用户信息列表
* @throws Exception 请求失败时抛出异常
*/
public IMResult<OutputUserInfoList> getBatchUsers(List<String> userIds) throws Exception {
String path = APIPath.Robot_User_Batch_Get_Infos;
InputStringList input = new InputStringList();
input.setList(userIds);
return robotHttpUtils.httpJsonPost(path, input, OutputUserInfoList.class);
}
/** /**
* 设置机器人消息回调地址 * 设置机器人消息回调地址
* @param url 回调地址 * @param url 回调地址