Commit 055f9475 authored by shiyu's avatar shiyu

ai会话

parent ee7f85ea
...@@ -36,6 +36,16 @@ public class IMEnum { ...@@ -36,6 +36,16 @@ public class IMEnum {
return null; return null;
} }
public static Integer getCodeByValue(String value) {
for (IMEnum.MsgTypeEnum msgTypeEnum : IMEnum.MsgTypeEnum.values()) {
if (msgTypeEnum.getValue().equals(value)) {
return msgTypeEnum.getCode();
}
}
return null;
}
public int getCode() { public int getCode() {
return code; return code;
} }
......
...@@ -4,6 +4,9 @@ import com.wwdz.ch.db.dto.request.BaseRequestDto; ...@@ -4,6 +4,9 @@ import com.wwdz.ch.db.dto.request.BaseRequestDto;
import com.xxdxxs.entity.Entity; import com.xxdxxs.entity.Entity;
import lombok.Data; import lombok.Data;
/**
* 拉取会话
*/
@Data @Data
public class IMChatRequestDto extends BaseRequestDto implements Entity { public class IMChatRequestDto extends BaseRequestDto implements Entity {
......
...@@ -3,6 +3,8 @@ package com.wwdz.ch.core.entity.im; ...@@ -3,6 +3,8 @@ package com.wwdz.ch.core.entity.im;
import com.xxdxxs.entity.Entity; import com.xxdxxs.entity.Entity;
import lombok.Data; import lombok.Data;
import java.util.Date;
/** /**
* 历史聊天记录请求参数类 * 历史聊天记录请求参数类
*/ */
...@@ -27,12 +29,12 @@ public class IMHistoryMsgRequestDto implements Entity { ...@@ -27,12 +29,12 @@ public class IMHistoryMsgRequestDto implements Entity {
/** /**
* 请求的消息时间范围的最小值(单位:秒) * 请求的消息时间范围的最小值(单位:秒)
*/ */
private Integer minTime; private Date minTime;
/** /**
* 请求的消息时间范围的最大值(单位:秒) * 请求的消息时间范围的最大值(单位:秒)
*/ */
private Integer maxTime; private Date maxTime;
/** /**
* 上一次拉取到的最后一条消息的 MsgKey,续拉时需要填该字段 * 上一次拉取到的最后一条消息的 MsgKey,续拉时需要填该字段
......
package com.wwdz.ch.core.entity.im.vo;
import com.xxdxxs.entity.Entity;
import lombok.Data;
import java.util.List;
@Data
public class HistoryMsgResponse implements Entity {
private List<HistoryMsgVo> dataList;
/**
* 本次拉取到的消息里的最后一条消息的标识
*/
private String lastMsgKey;
/**
* 本次拉取到的消息里的最后一条消息的时间
*/
private Integer lastMsgTime;
/**
* 是否全部拉取:
* 0:表示未全部拉取,需要续拉
* 1:表示已全部拉取
*/
private Integer complete;
/**
* 本次拉取到的消息条数
*/
private Integer msgCnt;
}
package com.wwdz.ch.core.entity.im.vo;
import com.xxdxxs.entity.Entity;
import lombok.Data;
@Data
public class HistoryMsgVo implements Entity {
private String fromAccount;
private String toAccount;
private String msgKsy;
/**
* 消息发送时间
*/
private Long msgTimeStamp;
/**
* 该条消息的属性:
* 0:表示正常消息
* 8:表示被撤回的消息
*/
private Integer msgFlagBits;
/**
* 消息接收方是否发送该条消息的已读回执。
* 0:表示未发送
* 1:表示已发送
*/
private Integer isPeerRead;
/**
* 消息类型
*/
private Integer msgType;
private Object msgContent;
}
...@@ -4,14 +4,18 @@ package com.wwdz.ch.core.im.api; ...@@ -4,14 +4,18 @@ package com.wwdz.ch.core.im.api;
import com.wwdz.ch.core.consts.IMEnum; import com.wwdz.ch.core.consts.IMEnum;
import com.wwdz.ch.core.entity.im.IMChatMsg; import com.wwdz.ch.core.entity.im.IMChatMsg;
import com.wwdz.ch.core.entity.im.IMChatRequestDto; import com.wwdz.ch.core.entity.im.IMChatRequestDto;
import com.wwdz.ch.core.entity.im.IMHistoryMsgRequestDto;
import com.wwdz.ch.core.entity.im.SendChatMsgRequestDto; import com.wwdz.ch.core.entity.im.SendChatMsgRequestDto;
import com.wwdz.ch.core.entity.im.vo.ConversationResponse; import com.wwdz.ch.core.entity.im.vo.ConversationResponse;
import com.wwdz.ch.core.entity.im.vo.HistoryMsgResponse;
import com.wwdz.ch.core.entity.im.vo.HistoryMsgVo;
import com.wwdz.ch.core.entity.im.vo.IMChatVo; import com.wwdz.ch.core.entity.im.vo.IMChatVo;
import com.wwdz.ch.core.type.Result; import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.IMUtil; import com.wwdz.ch.core.util.IMUtil;
import com.wwdz.ch.core.util.OkHttpUtil; import com.wwdz.ch.core.util.OkHttpUtil;
import com.xxdxxs.utils.JsonUtils; import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.MapUtils; import com.xxdxxs.utils.MapUtils;
import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -32,6 +36,8 @@ public class ConversationApi { ...@@ -32,6 +36,8 @@ public class ConversationApi {
private static final String GET_CHAT_LIST_URL = "https://console.tim.qq.com/v4/recentcontact/get_list"; private static final String GET_CHAT_LIST_URL = "https://console.tim.qq.com/v4/recentcontact/get_list";
private static final String GET_HISTORY_CHAT_URL = "https://console.tim.qq.com/v4/openim/admin_getroammsg";
@Autowired @Autowired
TLSSigApi tlsSigApi; TLSSigApi tlsSigApi;
...@@ -43,7 +49,7 @@ public class ConversationApi { ...@@ -43,7 +49,7 @@ public class ConversationApi {
* @param sendChatMsgRequestDto * @param sendChatMsgRequestDto
* @return * @return
*/ */
public Result sendChatMsg(SendChatMsgRequestDto sendChatMsgRequestDto) { public Result<String> sendChatMsg(SendChatMsgRequestDto sendChatMsgRequestDto) {
Random random = new Random(4294967295L); Random random = new Random(4294967295L);
int randomNum = random.nextInt(); int randomNum = random.nextInt();
String url = imUtil.formatIMUrl(SEND_MSG_URL); String url = imUtil.formatIMUrl(SEND_MSG_URL);
...@@ -64,8 +70,9 @@ public class ConversationApi { ...@@ -64,8 +70,9 @@ public class ConversationApi {
logger.error("用户id : {}, 发送对话信息失败, errorCode : {}, errmsg : {} ", imChatMsg.getFrom_Account(), errorCode, errorInfo); logger.error("用户id : {}, 发送对话信息失败, errorCode : {}, errmsg : {} ", imChatMsg.getFrom_Account(), errorCode, errorInfo);
return Result.failed(errorInfo); return Result.failed(errorInfo);
} }
String msgKey = JsonUtils.getValueByPath(responseStr, "MsgKey");
logger.info("============= 发送对话信息成功 ==========="); logger.info("============= 发送对话信息成功 ===========");
return Result.success(); return Result.success(msgKey);
} }
...@@ -127,12 +134,81 @@ public class ConversationApi { ...@@ -127,12 +134,81 @@ public class ConversationApi {
/** /**
* 拉取历史聊天记录 * 拉取历史聊天记录
* @param imChatRequestDto * @param imHistoryMsgRequestDto
* @return * @return
*/ */
public Result getHistoryMsg(IMChatRequestDto imChatRequestDto) { public Result getHistoryMsg(IMHistoryMsgRequestDto imHistoryMsgRequestDto) {
try {
List<HistoryMsgVo> historyMsgVos = new ArrayList<>();
String url = imUtil.formatIMUrl(GET_HISTORY_CHAT_URL);
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
map.put("Operator_Account", imHistoryMsgRequestDto.getOperatorAccount());
map.put("Peer_Account", imHistoryMsgRequestDto.getPeerAccount());
map.put("MaxCnt", imHistoryMsgRequestDto.getMaxCnt());
map.put("MinTime", imHistoryMsgRequestDto.getMinTime().getTime());
map.put("MaxTime", imHistoryMsgRequestDto.getMaxTime().getTime());
String lastMsgKey = imHistoryMsgRequestDto.getLastMsgKey();
if (StringUtils.hasLength(lastMsgKey)) {
map.put("LastMsgKey", imHistoryMsgRequestDto.getLastMsgKey());
}
logger.info("============ 拉取历史聊天记录参数 : {}", JsonUtils.from(map));
okHttpUtil.addParams(map);
okHttpUtil.post(true);
String responseStr = okHttpUtil.async();
logger.info("拉取历史聊天记录 response :{}", 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("用户id : {}, 拉取历史聊天记录失败, errorCode : {}, errmsg : {} ", imHistoryMsgRequestDto.getOperatorAccount(), errorCode, errorInfo);
return Result.failed(errorInfo);
}
//是否拉取完成1:完成,0:未拉取完
int complete = Integer.valueOf(JsonUtils.getValueByPath(responseStr, "Complete"));
//本次拉取的消息条数
int msgCnt = Integer.valueOf(JsonUtils.getValueByPath(responseStr, "MsgCnt"));
int lastMsgTime = Integer.valueOf(JsonUtils.getValueByPath(responseStr, "LastMsgTime"));
lastMsgKey = JsonUtils.getValueByPath(responseStr, "LastMsgKey");
return null; String msgList = JsonUtils.getValueByPath(responseStr, "MsgList");
List<Map<String, Object>> msgMapList = JsonUtils.toMapList(msgList);
for (Map<String, Object> m : msgMapList) {
String fromAccount = m.get("From_Account").toString();
String toAccount = m.get("To_Account").toString();
long msgTimeStamp = (long)m.get("MsgTimeStamp");
int msgFlagBits = (int)m.get("MsgFlagBits");
int isPeerRead = (int)m.get("IsPeerRead");
String msgKsy = m.get("MsgKey").toString();
//消息体
String msgBody = m.get("MsgBody").toString();
List<Map<String, Object>> msgBodyList = JsonUtils.toMapList(msgBody);
String msgType = msgBodyList.get(0).get("MsgType").toString();
String contentJsonStr = msgBodyList.get(0).get("MsgContent").toString();
HistoryMsgVo historyMsgVo = new HistoryMsgVo();
historyMsgVo.setFromAccount(fromAccount);
historyMsgVo.setToAccount(toAccount);
historyMsgVo.setMsgType(IMEnum.MsgTypeEnum.getCodeByValue(msgType));
historyMsgVo.setMsgContent(contentJsonStr);
historyMsgVo.setIsPeerRead(isPeerRead);
historyMsgVo.setMsgFlagBits(msgFlagBits);
historyMsgVo.setMsgTimeStamp(msgTimeStamp);
historyMsgVo.setMsgKsy(msgKsy);
historyMsgVos.add(historyMsgVo);
}
HistoryMsgResponse historyMsgResponse = new HistoryMsgResponse();
historyMsgResponse.setDataList(historyMsgVos);
historyMsgResponse.setMsgCnt(msgCnt);
historyMsgResponse.setLastMsgKey(lastMsgKey);
historyMsgResponse.setLastMsgTime(lastMsgTime);
historyMsgResponse.setComplete(complete);
logger.info("============= 拉取历史聊天记录成功 =============");
return Result.success(historyMsgResponse);
} catch (Exception e) {
logger.error("拉取历史聊天记录 error:{}", e);
}
return Result.failed();
} }
} }
package com.wwdz.ch.wx.api; package com.wwdz.ch.wx.api;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.core.consts.IMEnum;
import com.wwdz.ch.db.domain.ImRecord;
import com.wwdz.ch.wx.service.ImRecordService; import com.wwdz.ch.wx.service.ImRecordService;
import com.xxdxxs.utils.JsonUtils; import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.StringUtils; import com.xxdxxs.utils.StringUtils;
...@@ -10,6 +12,10 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -10,6 +12,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/imCallback") @RequestMapping("/imCallback")
public class IMCallbackController { public class IMCallbackController {
...@@ -27,33 +33,59 @@ public class IMCallbackController { ...@@ -27,33 +33,59 @@ public class IMCallbackController {
* 、Web(使用 Web SDK 发送请求)、Android、iOS、Windows、Mac、iPad、Unknown(使用未知类型的设备发送请求) * 、Web(使用 Web SDK 发送请求)、Android、iOS、Windows、Mac、iPad、Unknown(使用未知类型的设备发送请求)
* @return * @return
*/ */
@RequestMapping(value ="/getCallbackInfo/{appid}") @RequestMapping(value ="/getCallbackInfo")
public Object getCallbackInfo(@PathVariable String appid, public Object getCallbackInfo(@PathVariable String appid,
@RequestParam(required = false, name = "SdkAppid") String sdkAppid, @RequestParam(required = false, name = "SdkAppid") String sdkAppid,
@RequestParam(required = false, name = "OptPlatform") String optPlatform, @RequestParam(required = false, name = "OptPlatform") String optPlatform,
@RequestParam(required = false, name = "CallbackCommand") String callbackCommand, @RequestParam(required = false, name = "CallbackCommand") String callbackCommand,
@RequestBody(required = false) String jsonStr) { @RequestBody(required = false) String jsonStr) {
try { try {
logger.info("接收推送信息校验 -> SdkAppid: {}, OptPlatform:{}, CallbackCommand:{}", sdkAppid, optPlatform, callbackCommand); logger.info("消息发送之后的回调url参数 -> SdkAppid: {}, OptPlatform:{}, CallbackCommand:{}", sdkAppid, optPlatform, callbackCommand);
if (!sdkAppid.equals(String.valueOf(SDKAppID))) { if (!sdkAppid.equals(String.valueOf(SDKAppID))) {
logger.info(">>>>>>>>>>> IM回调sdkappid 不一致 <<<<<<<<<<<"); logger.info(">>>>>>>>>>> IM回调sdkappid 不一致 <<<<<<<<<<<");
return false; return false;
} }
logger.info("回调推送的消息原文 -> {}", jsonStr); logger.info("消息发送之后的回调推送原文 -> {}", jsonStr);
ImRecord imRecord = new ImRecord();
//发送消息后回调信息处理 //发送消息后回调信息处理
String fromAccount = JsonUtils.getValueByPath(jsonStr, "From_Account"); String fromAccount = JsonUtils.getValueByPath(jsonStr, "From_Account");
String toAccount = JsonUtils.getValueByPath(jsonStr, "From_Account"); String toAccount = JsonUtils.getValueByPath(jsonStr, "From_Account");
String MsgTime = JsonUtils.getValueByPath(jsonStr, "MsgTime"); long msgTime = Long.valueOf(JsonUtils.getValueByPath(jsonStr, "MsgTime"));
String MsgKey = JsonUtils.getValueByPath(jsonStr, "MsgKey"); String msgKey = JsonUtils.getValueByPath(jsonStr, "MsgKey");
String OnlineOnlyFlag = JsonUtils.getValueByPath(jsonStr, "OnlineOnlyFlag"); String onlineOnlyFlag = JsonUtils.getValueByPath(jsonStr, "OnlineOnlyFlag");
String SendMsgResult = JsonUtils.getValueByPath(jsonStr, "SendMsgResult"); String sendMsgResult = JsonUtils.getValueByPath(jsonStr, "SendMsgResult");
String ErrorInfo = JsonUtils.getValueByPath(jsonStr, "ErrorInfo"); String errorInfo = JsonUtils.getValueByPath(jsonStr, "ErrorInfo");
if (!"0".equals(sendMsgResult)) {
logger.error("msgKey:{}, 消息下发失败错误信息:{}", msgKey, errorInfo);
imRecord.setState(IMEnum.MsgStateEnum.FAILED.getCode());
} else {
imRecord.setState(IMEnum.MsgStateEnum.READED.getCode());
}
//消息体 //消息体
String msgBody = JsonUtils.getValueByPath(jsonStr, "MsgBody"); String msgBody = JsonUtils.getValueByPath(jsonStr, "MsgBody");
JsonUtils.toMapList(msgBody); List<Map<String, Object>> msgBodyList = JsonUtils.toMapList(msgBody);
String msgType = msgBodyList.get(0).get("MsgType").toString();
String contentJsonStr = msgBodyList.get(0).get("MsgContent").toString();
String content = null;
if (IMEnum.MsgTypeEnum.TIMTextElem.getValue().equals(msgType)) {
content = JsonUtils.getValueByPath(contentJsonStr, "Text");
}
imRecord.setFromAccount(fromAccount);
imRecord.setToAccount(toAccount);
imRecord.setMsgTime(new Date( msgTime));
imRecord.setMsgKey(msgKey);
imRecord.setMsgType(IMEnum.MsgTypeEnum.getCodeByValue(msgType));
imRecord.setSendMsgResult(Integer.valueOf(sendMsgResult));
imRecord.setOnlineOnlyFlag(Integer.valueOf(onlineOnlyFlag));
imRecord.setMsgContent(content);
Date now = new Date();
imRecord.setCreateTime(now);
imRecord.setUpdateTime(now);
imRecordService.create(imRecord);
} catch (Exception e) { } catch (Exception e) {
logger.error("视频号回调推送error : {}", e); logger.error("消息发送之后的回调error : {}", e);
} }
return "success"; return "success";
} }
......
...@@ -116,7 +116,7 @@ public class ImServiceImpl implements IMService { ...@@ -116,7 +116,7 @@ public class ImServiceImpl implements IMService {
@Override @Override
public Result sendAiChatMsg(SendChatMsgRequestDto dto) { public Result sendAiChatMsg(SendChatMsgRequestDto dto) {
try { try {
Result askResult = conversationApi.sendChatMsg(dto); Result<String> askResult = conversationApi.sendChatMsg(dto);
if (!askResult.getSuccess()) { if (!askResult.getSuccess()) {
return askResult; return askResult;
} }
...@@ -136,6 +136,7 @@ public class ImServiceImpl implements IMService { ...@@ -136,6 +136,7 @@ public class ImServiceImpl implements IMService {
imRecord.setCreateTime(now); imRecord.setCreateTime(now);
imRecord.setUpdateTime(now); imRecord.setUpdateTime(now);
imRecord.setState(IMEnum.MsgStateEnum.READED.getCode()); imRecord.setState(IMEnum.MsgStateEnum.READED.getCode());
imRecord.setMsgKey(askResult.getData());
imRecordService.create(imRecord); imRecordService.create(imRecord);
...@@ -156,7 +157,7 @@ public class ImServiceImpl implements IMService { ...@@ -156,7 +157,7 @@ public class ImServiceImpl implements IMService {
if (redisUtils.hasKey(dto.getReqId())){ if (redisUtils.hasKey(dto.getReqId())){
return Result.success("回答已被终止"); return Result.success("回答已被终止");
} }
Result answerResult = conversationApi.sendChatMsg(answerDto); Result<String> answerResult = conversationApi.sendChatMsg(answerDto);
if (!answerResult.getSuccess()) { if (!answerResult.getSuccess()) {
logger.info("=============== 发送问题的ai答复消息失败 ============="); logger.info("=============== 发送问题的ai答复消息失败 =============");
return answerResult; return answerResult;
...@@ -166,6 +167,7 @@ public class ImServiceImpl implements IMService { ...@@ -166,6 +167,7 @@ public class ImServiceImpl implements IMService {
ImRecord answerImRecord = new ImRecord(); ImRecord answerImRecord = new ImRecord();
answerImRecord.setFromAccount(dto.getToAccount()); answerImRecord.setFromAccount(dto.getToAccount());
answerImRecord.setToAccount(dto.getFromAccount()); answerImRecord.setToAccount(dto.getFromAccount());
imRecord.setMsgKey(answerResult.getData());
answerImRecord.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode()); answerImRecord.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode());
answerImRecord.setMsgContent(answer); answerImRecord.setMsgContent(answer);
answerImRecord.setOnlineOnlyFlag(0); answerImRecord.setOnlineOnlyFlag(0);
......
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