Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Q
qk_backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
quanku
qk_backend
Commits
998368a1
Commit
998368a1
authored
Oct 19, 2023
by
shiyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ai会话
parent
6e60cc2d
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
83 additions
and
19 deletions
+83
-19
ch-core/src/main/java/com/wwdz/ch/core/entity/im/SendChatMsgRequestDto.java
...ava/com/wwdz/ch/core/entity/im/SendChatMsgRequestDto.java
+8
-8
ch-core/src/main/java/com/wwdz/ch/core/im/api/ConversationApi.java
...rc/main/java/com/wwdz/ch/core/im/api/ConversationApi.java
+1
-1
ch-core/src/main/java/com/wwdz/ch/core/util/IMUtil.java
ch-core/src/main/java/com/wwdz/ch/core/util/IMUtil.java
+2
-2
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/IMServiceImpl.java
...-api/src/main/java/com/wwdz/ch/wx/impl/IMServiceImpl.java
+47
-3
ch-wx-api/src/main/java/com/wwdz/ch/wx/service/IMService.java
...x-api/src/main/java/com/wwdz/ch/wx/service/IMService.java
+8
-0
ch-wx-api/src/main/java/com/wwdz/ch/wx/web/IMController.java
ch-wx-api/src/main/java/com/wwdz/ch/wx/web/IMController.java
+15
-2
ch-wx-api/src/test/java/com/wwdz/ch/wx/api/ChatApiTest.java
ch-wx-api/src/test/java/com/wwdz/ch/wx/api/ChatApiTest.java
+2
-3
No files found.
ch-core/src/main/java/com/wwdz/ch/core/entity/im/SendChatMsgRequestDto.java
View file @
998368a1
...
...
@@ -12,17 +12,17 @@ public class SendChatMsgRequestDto implements Entity {
* 2:消息不同步至 From_Account
* 若不填写默认情况下会将消息存 From_Account 漫游
*/
private
Integer
S
yncOtherMachine
;
private
Integer
s
yncOtherMachine
;
/**
* 消息发送方 UserID(用于指定发送消息方账号)
*/
private
String
From_
Account
;
private
String
from
Account
;
/**
* 消息接收方 UserID
*/
private
String
To_
Account
;
private
String
to
Account
;
/**
* 消息离线保存时长(单位:秒),最长为7天(604800秒)
...
...
@@ -30,28 +30,28 @@ public class SendChatMsgRequestDto implements Entity {
* 若设置该字段超过7天(604800秒),仍只保存7天
* 若不设置该字段,则默认保存7天
*/
private
Integer
M
sgLifeTime
;
private
Integer
m
sgLifeTime
;
/**
* 消息序列号(32位无符号整数),后台会根据该字段去重及进行同秒内消息的排序,
* 详细规则请看本接口的功能说明。若不填该字段,则由后台填入随机数
*/
private
Integer
M
sgSeq
;
private
Integer
m
sgSeq
;
/**
* 消息随机数(32位无符号整数),后台用于同一秒内的消息去重。请确保该字段填的是随机
*/
private
Integer
M
sgRandom
;
private
Integer
m
sgRandom
;
/**
* 消息对象类型
* 枚举转换成字符串文本
*/
private
Integer
M
sgType
;
private
Integer
m
sgType
;
/**
* 消息内容
*/
private
Object
M
sgContent
;
private
Object
m
sgContent
;
}
ch-core/src/main/java/com/wwdz/ch/core/im/api/ConversationApi.java
View file @
998368a1
...
...
@@ -65,7 +65,7 @@ public class ConversationApi {
OkHttpUtil
okHttpUtil
=
OkHttpUtil
.
builder
().
url
(
url
);
IMChatMsg
imChatMsg
=
imUtil
.
convert
(
sendChatMsgRequestDto
);
//消息同步至发送方
imChatMsg
.
setSyncOtherMachine
(
IMEnum
.
SyncOtherMachineEnum
.
SYNC
.
getCod
e
());
imChatMsg
.
setSyncOtherMachine
(
sendChatMsgRequestDto
.
getSyncOtherMachine
()==
null
?
IMEnum
.
SyncOtherMachineEnum
.
SYNC
.
getCode
()
:
sendChatMsgRequestDto
.
getSyncOtherMachin
e
());
imChatMsg
.
setMsgRandom
(
randomNum
);
logger
.
info
(
"============ 发送对话信息内容 : {}"
,
JsonUtils
.
from
(
imChatMsg
));
okHttpUtil
.
addParams
(
MapUtils
.
fromEntity
(
imChatMsg
));
...
...
ch-core/src/main/java/com/wwdz/ch/core/util/IMUtil.java
View file @
998368a1
...
...
@@ -46,8 +46,8 @@ public class IMUtil {
public
IMChatMsg
convert
(
SendChatMsgRequestDto
dto
)
{
IMChatMsg
imChatMsg
=
new
IMChatMsg
();
//userid需要转换成IM的用户ID
imChatMsg
.
setTo_Account
(
formatUserId
(
dto
.
getTo
_
Account
()));
imChatMsg
.
setFrom_Account
(
formatUserId
(
dto
.
getFrom
_
Account
()));
imChatMsg
.
setTo_Account
(
formatUserId
(
dto
.
getToAccount
()));
imChatMsg
.
setFrom_Account
(
formatUserId
(
dto
.
getFromAccount
()));
Map
<
String
,
Object
>
msgContentMap
=
new
HashMap
<>();
msgContentMap
.
put
(
"MsgType"
,
IMEnum
.
MsgTypeEnum
.
getValueByCode
(
dto
.
getMsgType
()));
msgContentMap
.
put
(
"MsgContent"
,
new
HashMap
(){{
put
(
"Text"
,
dto
.
getMsgContent
());}});
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/IMServiceImpl.java
View file @
998368a1
...
...
@@ -9,12 +9,13 @@ import com.wwdz.ch.core.type.Result;
import
com.wwdz.ch.core.util.IMUtil
;
import
com.wwdz.ch.db.dao.AiAssistantDao
;
import
com.wwdz.ch.db.domain.AiAssistant
;
import
com.wwdz.ch.wx.api.ChatApi
;
import
com.wwdz.ch.wx.service.IMService
;
import
com.xxdxxs.utils.JsonUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -38,6 +39,9 @@ public class IMServiceImpl implements IMService {
@Autowired
IMUtil
imUtil
;
@Autowired
ChatApi
chatApi
;
/**
* 新增ai会话,发送默认打招呼的消息
* @param dto
...
...
@@ -46,7 +50,7 @@ public class IMServiceImpl implements IMService {
@Override
public
Result
addAiChat
(
SendChatMsgRequestDto
dto
)
{
try
{
AiAssistant
aiAssistant
=
aiAssistantDao
.
findByCode
(
dto
.
getFrom
_
Account
());
AiAssistant
aiAssistant
=
aiAssistantDao
.
findByCode
(
dto
.
getFromAccount
());
String
msgContent
=
aiAssistant
.
getProfile
();
dto
.
setMsgContent
(
msgContent
);
dto
.
setMsgType
(
IMEnum
.
MsgTypeEnum
.
TIMTextElem
.
getCode
());
...
...
@@ -87,6 +91,46 @@ public class IMServiceImpl implements IMService {
@Override
public
Result
sendChatMsg
(
SendChatMsgRequestDto
dto
)
{
return
null
;
try
{
Result
result
=
conversationApi
.
sendChatMsg
(
dto
);
return
result
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"发送聊天消息失败:{}"
,
e
);
}
return
Result
.
failed
();
}
@Override
public
Result
sendAiChatMsg
(
SendChatMsgRequestDto
dto
)
{
try
{
Result
askResult
=
conversationApi
.
sendChatMsg
(
dto
);
if
(!
askResult
.
getSuccess
())
{
return
askResult
;
}
logger
.
info
(
"=========== userid:{}, 提问ai的消息发送成功 =========="
,
dto
.
getFromAccount
());
if
(
dto
.
getMsgType
()
==
IMEnum
.
MsgTypeEnum
.
TIMTextElem
.
getCode
())
{
String
question
=
(
String
)
dto
.
getMsgContent
();
String
response
=
chatApi
.
chatWithModel
(
question
);
String
answer
=
JsonUtils
.
getValueByPath
(
response
,
"response"
);
//将回答也发送给IM
SendChatMsgRequestDto
answerDto
=
new
SendChatMsgRequestDto
();
answerDto
.
setFromAccount
(
dto
.
getToAccount
());
answerDto
.
setToAccount
(
dto
.
getFromAccount
());
answerDto
.
setMsgType
(
IMEnum
.
MsgTypeEnum
.
TIMTextElem
.
getCode
());
answerDto
.
setMsgContent
(
answer
);
Result
answerResult
=
conversationApi
.
sendChatMsg
(
answerDto
);
if
(!
answerResult
.
getSuccess
())
{
logger
.
info
(
"=============== 发送问题的ai答复消息失败 ============="
);
return
answerResult
;
}
}
else
{
return
Result
.
failed
(
"目前尚不支持除文本外的消息发送"
);
}
return
Result
.
success
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"发送聊天消息失败:{}"
,
e
);
}
return
Result
.
failed
();
}
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/service/IMService.java
View file @
998368a1
...
...
@@ -35,4 +35,12 @@ public interface IMService {
* @return
*/
Result
sendChatMsg
(
SendChatMsgRequestDto
dto
);
/**
* 发送ai聊天消息
* 将问题发送给im,并把ai的回答也发送给im
* @param dto
* @return
*/
Result
sendAiChatMsg
(
SendChatMsgRequestDto
dto
);
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/web/IMController.java
View file @
998368a1
...
...
@@ -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
);
...
...
@@ -69,11 +69,24 @@ public class IMController {
@PostMapping
(
"/sendChatMsg"
)
public
Result
sendChatMsg
(
@RequestBody
SendChatMsgRequestDto
dto
)
{
logger
.
info
(
"【请求开始】发送聊天消息,请求参数:{}"
,
JSON
.
toJSONString
(
dto
));
if
(!
StringUtils
.
isAllNotEmpty
(
dto
.
getFrom
_Account
(),
dto
.
getTo_
Account
(),
dto
.
getMsgType
()))
{
if
(!
StringUtils
.
isAllNotEmpty
(
dto
.
getFrom
Account
(),
dto
.
getTo
Account
(),
dto
.
getMsgType
()))
{
return
Result
.
failed
(
ResultCode
.
PARAM_ERROR
);
}
return
imService
.
sendChatMsg
(
dto
);
}
@ApiOperation
(
value
=
"发送Ai聊天消息"
)
@PostMapping
(
"/sendAiChatMsg"
)
public
Result
sendAiChatMsg
(
@RequestBody
SendChatMsgRequestDto
dto
)
{
logger
.
info
(
"【请求开始】发送聊天消息,请求参数:{}"
,
JSON
.
toJSONString
(
dto
));
if
(!
StringUtils
.
isAllNotEmpty
(
dto
.
getFromAccount
(),
dto
.
getToAccount
(),
dto
.
getMsgType
()))
{
return
Result
.
failed
(
ResultCode
.
PARAM_ERROR
);
}
return
imService
.
sendAiChatMsg
(
dto
);
}
}
ch-wx-api/src/test/java/com/wwdz/ch/wx/api/ChatApiTest.java
View file @
998368a1
...
...
@@ -11,7 +11,6 @@ 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
;
...
...
@@ -90,8 +89,8 @@ public class ChatApiTest {
@Test
public
void
sendChatMsg
()
{
SendChatMsgRequestDto
sendChatMsgRequestDto
=
new
SendChatMsgRequestDto
();
sendChatMsgRequestDto
.
setTo
_
Account
(
"241"
);
sendChatMsgRequestDto
.
setFrom
_
Account
(
"ai10000"
);
sendChatMsgRequestDto
.
setToAccount
(
"241"
);
sendChatMsgRequestDto
.
setFromAccount
(
"ai10000"
);
sendChatMsgRequestDto
.
setMsgType
(
IMEnum
.
MsgTypeEnum
.
TIMTextElem
.
getCode
());
long
timestamp
=
System
.
currentTimeMillis
();
String
msgContent
=
timestamp
+
" >> ai测试消息的数据, 是否可以接收到"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment