feat:机器人注册接口更新到IM
This commit is contained in:
parent
a53451f7c1
commit
6cf02b7552
@ -27,7 +27,6 @@ public class MessageInfo {
|
||||
/**
|
||||
* 消息发送者Id
|
||||
*/
|
||||
@NotNull(message = "消息发送者不能为空")
|
||||
private String fromImAccount;
|
||||
|
||||
/**
|
||||
|
||||
@ -12,7 +12,6 @@ cn\axzo\im\center\api\vo\req\RobotPageQuery.class
|
||||
cn\axzo\im\center\api\vo\resp\UserAccountResp.class
|
||||
cn\axzo\im\center\api\vo\resp\MessageTemplateResp.class
|
||||
cn\axzo\im\center\api\vo\req\RobotMsgTemplateReq.class
|
||||
cn\axzo\im\center\api\vo\resp\MessageResp.class
|
||||
cn\axzo\im\center\api\vo\req\RobotTagQuery.class
|
||||
cn\axzo\im\center\api\vo\req\RobotAccountReq.class
|
||||
cn\axzo\im\center\api\vo\resp\RobotMsgTemplateResp.class
|
||||
|
||||
@ -15,8 +15,9 @@ D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\feign\RobotInfoAp
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\feign\RobotMsgTemplateApi.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\req\RobotPageQuery.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\req\RobotTagQuery.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\resp\MessageResp.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\resp\RobotInfoResp.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\req\BatchAccountReq.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\req\UpdateRobotInfoReq.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\req\AccountQuery.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\resp\MessageDispatchResp.java
|
||||
D:\im-center\im-center-api\src\main\java\cn\axzo\im\center\api\vo\resp\RobotMsgTemplateResp.java
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
package cn.axzo.im.channel.netease;
|
||||
|
||||
/**
|
||||
* im-center
|
||||
*
|
||||
* @author zuoqinbo
|
||||
* @version V1.0
|
||||
* @date 2023/10/16 9:32
|
||||
*/
|
||||
public interface INotifyService {
|
||||
|
||||
/**
|
||||
* 机器人信息变更,通知到IM用户系统
|
||||
* @param robotId
|
||||
*/
|
||||
void notifyAccountChange(String robotId);
|
||||
}
|
||||
@ -30,6 +30,8 @@ public class NimChannelService implements IMChannelProvider {
|
||||
|
||||
private static final String NIM_ACCOUNT_CREATE_URL = "https://api.netease.im/nimserver/user/create.action";
|
||||
|
||||
private static final String NIM_ACCOUNT_UPDATE_URL = "https://api.netease.im/nimserver/user/updateUinfo.action";
|
||||
|
||||
private static final String NIM_ACCOUNT_QUERY_URL = "https://api.netease.im/nimserver/user/getUinfos.action";
|
||||
|
||||
private static final String NIM_MESSAGE_DISPATCH_URL = "https://api.netease.im/nimserver/msg/sendMsg.action";
|
||||
@ -65,6 +67,7 @@ public class NimChannelService implements IMChannelProvider {
|
||||
paramMap.put("accid", register.getAccid());
|
||||
paramMap.put("icon", register.getIcon());
|
||||
paramMap.put("name", register.getName());
|
||||
log.info("im-center registerAccount 请求网易云信Server:{},请求参数:{}", NIM_ACCOUNT_CREATE_URL, JSONUtil.toJsonStr(paramMap));
|
||||
Map<String, String> authHeaderMap = buildAuthHeader(getProviderAppKey(), getProviderAppSecret());
|
||||
HttpResponse response = HttpRequest.post(NIM_ACCOUNT_CREATE_URL).addHeaders(authHeaderMap)
|
||||
.form(paramMap).timeout(5000).execute();
|
||||
@ -114,16 +117,17 @@ public class NimChannelService implements IMChannelProvider {
|
||||
paramMap.put("to", messageInfo.getTo());
|
||||
paramMap.put("type", messageInfo.getType());
|
||||
Map<String, String> authHeaderMap = buildAuthHeader(getProviderAppKey(), getProviderAppSecret());
|
||||
log.info("im-center dispatchMessage 请求网易云信Server:{},请求参数:{}", NIM_MESSAGE_DISPATCH_URL, JSONUtil.toJsonStr(paramMap));
|
||||
HttpResponse response = HttpRequest.post(NIM_MESSAGE_DISPATCH_URL).addHeaders(authHeaderMap)
|
||||
.form(paramMap).timeout(5000).execute();
|
||||
if (response.getStatus() == SUCCESS_CODE) {
|
||||
MessageDispatchResponse registerResponse = JSONUtil.toBean(response.body(), MessageDispatchResponse.class);
|
||||
if (registerResponse.getCode() != SUCCESS_CODE) {
|
||||
log.warn("im-center请求网易云信Server:{},返回异常:{}", NIM_ACCOUNT_CREATE_URL, response.body());
|
||||
log.warn("im-center请求网易云信Server:{},返回异常:{}", NIM_MESSAGE_DISPATCH_URL, response.body());
|
||||
}
|
||||
return registerResponse;
|
||||
} else {
|
||||
log.error("im-center请求网易云信Server:{},异常:{}", NIM_ACCOUNT_CREATE_URL, response.body());
|
||||
log.error("im-center请求网易云信Server:{},异常:{}", NIM_MESSAGE_DISPATCH_URL, response.body());
|
||||
}
|
||||
return MessageDispatchResponse.builder().desc("请求网易云信Server异常,请联系管理员!").build();
|
||||
}
|
||||
@ -141,18 +145,19 @@ public class NimChannelService implements IMChannelProvider {
|
||||
if (StringUtils.isNotBlank(updateRequest.getExtJson())) {
|
||||
paramMap.put("ex", updateRequest.getExtJson());
|
||||
}
|
||||
log.info("im-center请求网易云信Server:{},请求参数:{}", NIM_ACCOUNT_UPDATE_URL, JSONUtil.toJsonStr(paramMap));
|
||||
Map<String, String> authHeaderMap = buildAuthHeader(getProviderAppKey(), getProviderAppSecret());
|
||||
HttpResponse response = HttpRequest.post(NIM_ACCOUNT_CREATE_URL).addHeaders(authHeaderMap)
|
||||
HttpResponse response = HttpRequest.post(NIM_ACCOUNT_UPDATE_URL).addHeaders(authHeaderMap)
|
||||
.form(paramMap).timeout(5000).execute();
|
||||
String result = response.body();
|
||||
if (response.getStatus() == SUCCESS_CODE) {
|
||||
RegisterResponse registerResponse = JSONUtil.toBean(result, RegisterResponse.class);
|
||||
if (registerResponse.getCode() != SUCCESS_CODE) {
|
||||
log.warn("im-center请求网易云信Server:{},返回异常:{}", NIM_ACCOUNT_CREATE_URL, result);
|
||||
log.warn("im-center请求网易云信Server:{},返回异常:{}", NIM_ACCOUNT_UPDATE_URL, result);
|
||||
}
|
||||
return registerResponse;
|
||||
} else {
|
||||
log.error("im-center请求网易云信Server:{},异常:{}", NIM_ACCOUNT_CREATE_URL, result);
|
||||
log.error("im-center请求网易云信Server:{},异常:{}", NIM_ACCOUNT_UPDATE_URL, result);
|
||||
}
|
||||
return RegisterResponse.builder().desc("请求网易云信Server异常,请联系管理员!").build();
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import cn.axzo.im.center.api.vo.req.RobotInfoReq;
|
||||
import cn.axzo.im.center.api.vo.req.RobotPageQuery;
|
||||
import cn.axzo.im.center.api.vo.req.UpdateRobotInfoReq;
|
||||
import cn.axzo.im.center.api.vo.resp.RobotInfoResp;
|
||||
import cn.axzo.im.channel.netease.INotifyService;
|
||||
import cn.axzo.im.service.RobotInfoService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -31,15 +32,18 @@ public class RobotInfoController implements RobotInfoApi {
|
||||
@Resource
|
||||
private RobotInfoService infoService;
|
||||
|
||||
@Resource
|
||||
private INotifyService iNotifyService;
|
||||
|
||||
@Override
|
||||
public ApiResult<RobotInfoResp> saveRobotInfo(RobotInfoReq robotInfoReq) {
|
||||
RobotInfoResp robotTagResp = infoService.saveRobotInfo(robotInfoReq);
|
||||
RobotInfoResp robotTagResp = infoService.saveRobotInfo(robotInfoReq,iNotifyService);
|
||||
return ApiResult.ok(robotTagResp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResult<RobotInfoResp> updateRobotInfo(UpdateRobotInfoReq updateRobotInfoReq) {
|
||||
RobotInfoResp robotTagResp = infoService.updateRobotInfo(updateRobotInfoReq);
|
||||
RobotInfoResp robotTagResp = infoService.updateRobotInfo(updateRobotInfoReq,iNotifyService);
|
||||
return ApiResult.ok(robotTagResp);
|
||||
}
|
||||
|
||||
|
||||
@ -3,14 +3,17 @@ package cn.axzo.im.service;
|
||||
import cn.axzo.im.center.api.vo.resp.RobotInfoResp;
|
||||
import cn.axzo.im.center.api.vo.resp.RobotMsgTemplateResp;
|
||||
import cn.axzo.im.channel.IMChannelProvider;
|
||||
import cn.axzo.im.channel.netease.INotifyService;
|
||||
import cn.axzo.im.channel.netease.dto.RegisterUpdateRequest;
|
||||
import cn.axzo.im.dao.repository.RobotInfoDao;
|
||||
import cn.axzo.im.entity.RobotInfo;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* im-center
|
||||
@ -21,7 +24,7 @@ import javax.annotation.Resource;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class NotifyChannelService {
|
||||
public class NotifyChannelService implements INotifyService {
|
||||
|
||||
@Resource
|
||||
private IMChannelProvider channelProvider;
|
||||
@ -36,7 +39,9 @@ public class NotifyChannelService {
|
||||
private RobotMsgTemplateService templateService;
|
||||
|
||||
|
||||
@Override
|
||||
public void notifyAccountChange(String robotId) {
|
||||
log.info("机器人信息发生变更,robotId:{},正在通知到IM系统", robotId);
|
||||
RegisterUpdateRequest updateProfile = new RegisterUpdateRequest();
|
||||
RobotInfo robotInfo = robotInfoDao.lambdaQuery().eq(RobotInfo::getIsDelete, 0)
|
||||
.eq(RobotInfo::getRobotId, robotId).one();
|
||||
@ -46,7 +51,13 @@ public class NotifyChannelService {
|
||||
RobotInfoResp robotInfoResp = robotInfoService.findRobotInfoById(robotInfo.getId());
|
||||
RobotMsgTemplateResp robotMsgTemplate = templateService.queryRobotMsgTemplateList(robotInfoResp.getRobotId());
|
||||
robotInfoResp.setMsgTemplateList(robotMsgTemplate.getMsgTemplateList());
|
||||
String extJson = JSONUtil.toJsonStr(robotInfoResp);
|
||||
|
||||
HashMap<String, Object> userProfileMap = Maps.newHashMap();
|
||||
userProfileMap.put("headImageUrl", robotInfoResp.getHeadImageUrl());
|
||||
userProfileMap.put("nickName", robotInfoResp.getNickName());
|
||||
userProfileMap.put("robotTagList", robotInfoResp.getRobotTagList());
|
||||
|
||||
String extJson = JSONUtil.toJsonStr(userProfileMap);
|
||||
updateProfile.setExtJson(extJson);
|
||||
updateProfile.setAccid(robotInfoResp.getImAccount());
|
||||
updateProfile.setIcon(robotInfoResp.getHeadImageUrl());
|
||||
|
||||
@ -10,11 +10,13 @@ import cn.axzo.im.center.api.vo.resp.RobotInfoResp;
|
||||
import cn.axzo.im.center.api.vo.resp.RobotMsgTemplateResp;
|
||||
import cn.axzo.im.center.api.vo.resp.RobotTagResp;
|
||||
import cn.axzo.im.center.common.enums.RobotStatusEnum;
|
||||
import cn.axzo.im.channel.netease.INotifyService;
|
||||
import cn.axzo.im.dao.repository.RobotInfoDao;
|
||||
import cn.axzo.im.dao.repository.RobotTagDao;
|
||||
import cn.axzo.im.entity.RobotInfo;
|
||||
import cn.axzo.im.entity.RobotTag;
|
||||
import cn.axzo.im.utils.BeanConvertUtils;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -58,7 +60,7 @@ public class RobotInfoService {
|
||||
private NotifyChannelService notifyChannelService;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public RobotInfoResp saveRobotInfo(@Valid RobotInfoReq robotInfoRequest) {
|
||||
public RobotInfoResp saveRobotInfo(@Valid RobotInfoReq robotInfoRequest, INotifyService iNotifyService) {
|
||||
verifyRobotName(robotInfoRequest.getNickName(), null);
|
||||
RobotInfo robotInfo = BeanConvertUtils.copyBean(robotInfoRequest, RobotInfo.class);
|
||||
String robotId = UUID.randomUUID().toString();
|
||||
@ -69,11 +71,14 @@ public class RobotInfoService {
|
||||
if (robotInfo != null) {
|
||||
return findRobotInfoById(robotInfo.getId());
|
||||
}
|
||||
if (iNotifyService != null) {
|
||||
iNotifyService.notifyAccountChange(robotInfo.getRobotId());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public RobotInfoResp updateRobotInfo(@Valid UpdateRobotInfoReq updateRobotInfoReq) {
|
||||
public RobotInfoResp updateRobotInfo(@Valid UpdateRobotInfoReq updateRobotInfoReq, INotifyService iNotifyService) {
|
||||
Long primaryId = updateRobotInfoReq.getId();
|
||||
RobotInfo robotTag = robotInfoDao.lambdaQuery().eq(RobotInfo::getIsDelete, 0)
|
||||
.eq(RobotInfo::getId, primaryId).one();
|
||||
@ -86,7 +91,9 @@ public class RobotInfoService {
|
||||
RobotInfo robotInfo = BeanConvertUtils.copyBean(updateRobotInfoReq, RobotInfo.class);
|
||||
robotInfo.setUpdateAt(new Date());
|
||||
robotInfo = robotInfoDao.saveOrUpdateRobotInfo(robotInfo);
|
||||
notifyChannelService.notifyAccountChange(robotInfo.getRobotId());
|
||||
if (iNotifyService != null) {
|
||||
iNotifyService.notifyAccountChange(robotInfo.getRobotId());
|
||||
}
|
||||
return findRobotInfoById(robotInfo.getId());
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user