Commit 4837173d authored by shiyu's avatar shiyu

IM ai助手

parent e5cbbcc4
......@@ -2,7 +2,6 @@ package com.wwdz.ch.core.entity;
import com.xxdxxs.entity.Entity;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
......
......@@ -7,6 +7,7 @@ import com.wwdz.ch.core.util.IMUtil;
import com.wwdz.ch.core.util.OkHttpUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.AiAssistant;
import com.wwdz.ch.db.domain.User;
import com.xxdxxs.utils.JsonUtils;
import org.slf4j.Logger;
......@@ -86,6 +87,45 @@ public class ChatUserApi {
}
/**
* 导入AI助手信息到IM中
* @return
*/
public Result importAiUser(AiAssistant aiAssistant) {
String userSig = tlsSigApi.getUserSig(ADMIN_ID);
Random random = new Random(4294967295L);
long randomNum = random.nextInt();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("?sdkappid=" + SDKAppID);
stringBuffer.append("&identifier=" + ADMIN_ID);
stringBuffer.append("&usersig=" + userSig);
stringBuffer.append("&random=" + randomNum);
stringBuffer.append("&contenttype=json");
String url = ACCOUNT_URL + stringBuffer.toString();
logger.info("============== IM导入用户请求url : {}", url);
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
map.put("UserID", imUtil.formatUserId(aiAssistant.getCode()));
map.put("Nick", aiAssistant.getName());
map.put("FaceUrl", aiAssistant.getAvatar());
logger.info("============ 导入AI助手内容 : {}", JsonUtils.fromMap(map));
okHttpUtil.addParams(map);
okHttpUtil.post(true);
String responseStr = okHttpUtil.async();
logger.info("AI助手 : {}, 导入AI助手 response :{}", aiAssistant.getCode(), responseStr);
String actionStatus = JsonUtils.getValueByPath(responseStr, "ActionStatus");
String errorCode = JsonUtils.getValueByPath(responseStr, "ErrorCode");
String errorInfo = JsonUtils.getValueByPath(responseStr, "ErrorInfo");
if (!"OK".equals(actionStatus)) {
logger.error("AI助手 : {}, 导入AI助手失败, errorCode : {}, errmsg : {} ", aiAssistant.getCode(), errorCode, errorInfo);
return Result.failed(errorInfo);
}
logger.info("============= 导入AI助手成功 ===========");
return Result.success();
}
/**
* IM用户初始化
* 导入表中已有全部的用户
......
......@@ -42,7 +42,8 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
"/officialAccountCallback/**",
"/wx/officialAccount/**",
"/wx/item/**",
"/wx/aiAssistant/**"
"/wx/aiAssistant/**",
"/wx/im/**"
);
}
}
......@@ -84,4 +84,9 @@ public class IMServiceImpl implements IMService {
}
return Result.failed();
}
@Override
public Result sendChatMsg(SendChatMsgRequestDto dto) {
return null;
}
}
......@@ -53,6 +53,10 @@ import java.util.Objects;
@Service
public class UserServiceImpl implements UserService {
private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
//默认的ai机器人编码,用于ai助手的聊天,该机器人永远在会话列表中置顶
private static final String DEFAULT_AI_CODE = "ai10000";
@Autowired
private UserDao userDao;
@Autowired
......@@ -85,6 +89,8 @@ public class UserServiceImpl implements UserService {
private FollowFansRecordService followFansRecordService;
@Override
public Result regCaptcha(UserRequestDto dto) {
try {
......@@ -145,6 +151,8 @@ public class UserServiceImpl implements UserService {
if (!isOpen) {
result.put("isInvited", true);
}
//添加默认AI机器人code
result.put("defaultAiCode", DEFAULT_AI_CODE);
logger.info("【请求结束】手机号登录,响应结果:{}", JSONObject.toJSONString(result));
// 清除验证码缓存
redisUtil.del(key);
......@@ -213,6 +221,8 @@ public class UserServiceImpl implements UserService {
if (!isOpen) {
result.put("isInvited", true);
}
//添加默认AI机器人code
result.put("defaultAiCode", DEFAULT_AI_CODE);
logger.info("【请求结束】快捷登录成功,响应结果:{}", JSONObject.toJSONString(result));
return Result.success(result);
} catch (Exception e) {
......
......@@ -27,4 +27,12 @@ public interface IMService {
* @return
*/
Result getUserSig(IMChatRequestDto dto);
/**
* 发送会话消息
* @param dto
* @return
*/
Result sendChatMsg(SendChatMsgRequestDto dto);
}
......@@ -47,7 +47,7 @@ public class IMController {
@PostMapping("/addChat")
public Result addChat(@RequestBody SendChatMsgRequestDto dto) {
logger.info("【请求开始】新增AI会话,请求参数:{}", JSON.toJSONString(dto));
if (!StringUtils.isAllNotEmpty(dto.getFrom_Account(), dto.getTo_Account())) {
if (!StringUtils.isAllNotEmpty(dto.getFrom_Account(), dto.getTo_Account())) {
return Result.failed(ResultCode.PARAM_ERROR);
}
return imService.addAiChat(dto);
......@@ -63,6 +63,17 @@ public class IMController {
}
return imService.getUserSig(dto);
}
@ApiOperation(value = "发送聊天消息")
@PostMapping("/sendChatMsg")
public Result sendChatMsg(@RequestBody SendChatMsgRequestDto dto) {
logger.info("【请求开始】发送聊天消息,请求参数:{}", JSON.toJSONString(dto));
if (!StringUtils.isAllNotEmpty(dto.getFrom_Account(), dto.getTo_Account(), dto.getMsgType())) {
return Result.failed(ResultCode.PARAM_ERROR);
}
return imService.sendChatMsg(dto);
}
}
......@@ -6,14 +6,20 @@ import com.wwdz.ch.core.entity.im.SendChatMsgRequestDto;
import com.wwdz.ch.core.im.api.ChatUserApi;
import com.wwdz.ch.core.im.api.ConversationApi;
import com.wwdz.ch.core.im.api.TLSSigApi;
import com.wwdz.ch.db.dao.AiAssistantDao;
import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.AiAssistant;
import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.db.dto.request.AiAssistantRequestDto;
import com.wwdz.ch.wx.service.AiAssistantService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
......@@ -34,6 +40,9 @@ public class ChatApiTest {
@Autowired
TLSSigApi tlsSigApi;
@Autowired
AiAssistantDao aiAssistantDao;
@Test
public void chatWithModel() {
String question = "刀币的由来";
......@@ -55,6 +64,18 @@ public class ChatApiTest {
}
/**
* 导入Ai助手
*/
@Test
public void importAiUser() {
List<AiAssistant> aiAssistants = aiAssistantDao.findList(new AiAssistantRequestDto());
aiAssistants.forEach(aiAssistant -> {
chatUserApi.importAiUser(aiAssistant);
});
}
/**
* 导入系统所有用户
*/
......@@ -70,9 +91,10 @@ public class ChatApiTest {
public void sendChatMsg() {
SendChatMsgRequestDto sendChatMsgRequestDto = new SendChatMsgRequestDto();
sendChatMsgRequestDto.setTo_Account("241");
sendChatMsgRequestDto.setFrom_Account("251");
sendChatMsgRequestDto.setFrom_Account("ai10000");
sendChatMsgRequestDto.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode());
String msgContent = "测试消息4444444的数据, 是否可以接收到";
long timestamp = System.currentTimeMillis();
String msgContent = timestamp + " >> ai测试消息的数据, 是否可以接收到";
sendChatMsgRequestDto.setMsgContent(msgContent);
conversationApi.sendChatMsg(sendChatMsgRequestDto);
}
......@@ -83,7 +105,7 @@ public class ChatApiTest {
@Test
public void getChatList() {
IMChatRequestDto imChatRequestDto = new IMChatRequestDto();
imChatRequestDto.setUserId(251L);
imChatRequestDto.setUserId(241L);
conversationApi.getChatList(imChatRequestDto);
}
......
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