Compare commits
6 Commits
cd2d8863a7
...
ac5fe7a266
Author | SHA1 | Date | |
---|---|---|---|
ac5fe7a266 | |||
7b76dd5142 | |||
1fc1ea9285 | |||
e1df94bb5e | |||
3fa1bfcb2a | |||
4643de4f6e |
File diff suppressed because it is too large
Load Diff
@ -886,6 +886,7 @@ message ChatRecordData{
|
|||||||
int32 waiterRead=12;//客服是否已读 1=已读 2=未读 (被任意客服读取过均为已读)
|
int32 waiterRead=12;//客服是否已读 1=已读 2=未读 (被任意客服读取过均为已读)
|
||||||
int64 localStamp = 13; //本地时间戳 用户端的消息唯一值,用于用户本地的一些逻辑处理
|
int64 localStamp = 13; //本地时间戳 用户端的消息唯一值,用于用户本地的一些逻辑处理
|
||||||
string domain =14;//域
|
string domain =14;//域
|
||||||
|
int32 role=15;//用户角色: 1=用户 2=客服 3=机器人
|
||||||
}
|
}
|
||||||
message CreateChatRecordResp{
|
message CreateChatRecordResp{
|
||||||
ChatRecordData data=1;
|
ChatRecordData data=1;
|
||||||
@ -1026,7 +1027,7 @@ message ChatUserData{
|
|||||||
int64 deletedAt = 4; //
|
int64 deletedAt = 4; //
|
||||||
string nickName = 5; //昵称
|
string nickName = 5; //昵称
|
||||||
string account = 6; //账号
|
string account = 6; //账号
|
||||||
int32 role = 7; //聊天角色 1=用户 2=客服
|
int32 role = 7; //聊天角色 1=用户 2=客服 3=机器人
|
||||||
string origin = 8; //数据来源
|
string origin = 8; //数据来源
|
||||||
int64 originId = 9; //数据来源对应的用户ID
|
int64 originId = 9; //数据来源对应的用户ID
|
||||||
string avatar = 10; //头像
|
string avatar = 10; //头像
|
||||||
|
@ -110,6 +110,9 @@ func (a *ChatAutoReplyRulerHandler) GetChatAutoReplyRulerList(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
var protoReq = accountFiee.GetChatAutoReplyRulerListRequest{Query: &accountFiee.ChatAutoReplyRulerData{}}
|
var protoReq = accountFiee.GetChatAutoReplyRulerListRequest{Query: &accountFiee.ChatAutoReplyRulerData{}}
|
||||||
utils.RequestDataConvert(&req, &protoReq)
|
utils.RequestDataConvert(&req, &protoReq)
|
||||||
|
if req.RuleType != "" {
|
||||||
|
protoReq.Where = fmt.Sprintf("ruler LIKE '%%\"%s\":{\"enable\":true}%%'", req.RuleType)
|
||||||
|
}
|
||||||
resp, err := service.AccountFieeProvider.GetChatAutoReplyRulerList(c, &protoReq)
|
resp, err := service.AccountFieeProvider.GetChatAutoReplyRulerList(c, &protoReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.Error(c, err)
|
service.Error(c, err)
|
||||||
|
@ -60,12 +60,12 @@ type MessageListType struct {
|
|||||||
Message Message `json:"message"`
|
Message Message `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MessageListType) BuildMessage(data *accountFiee.ChatRecordData, role int32) {
|
func (m *MessageListType) BuildMessage(data *accountFiee.ChatRecordData) {
|
||||||
m.ID = data.ID
|
m.ID = data.ID
|
||||||
m.CreatedAt = data.CreatedAt
|
m.CreatedAt = data.CreatedAt
|
||||||
m.UserId = data.UserId
|
m.UserId = data.UserId
|
||||||
m.Name = data.Name
|
m.Name = data.Name
|
||||||
m.Role = role
|
m.Role = data.Role
|
||||||
switch data.MsgType {
|
switch data.MsgType {
|
||||||
default:
|
default:
|
||||||
m.Message.MsgType = data.MsgType
|
m.Message.MsgType = data.MsgType
|
||||||
@ -152,6 +152,7 @@ type GetChatAutoReplyRulerListRequest struct {
|
|||||||
Page int64 `json:"page"`
|
Page int64 `json:"page"`
|
||||||
PageSize int64 `json:"pageSize"`
|
PageSize int64 `json:"pageSize"`
|
||||||
accountFiee.ChatAutoReplyRulerData
|
accountFiee.ChatAutoReplyRulerData
|
||||||
|
RuleType string `json:"ruleType"`
|
||||||
}
|
}
|
||||||
type ErpLoginDemoReq struct {
|
type ErpLoginDemoReq struct {
|
||||||
TelNum string `json:"telNum"`
|
TelNum string `json:"telNum"`
|
||||||
|
@ -322,7 +322,7 @@ func (cr ChatHandler) MessageList(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
returnDataIdList = append(returnDataIdList, message.ID)
|
returnDataIdList = append(returnDataIdList, message.ID)
|
||||||
var msg = &dto.MessageListType{}
|
var msg = &dto.MessageListType{}
|
||||||
msg.BuildMessage(message, 0)
|
msg.BuildMessage(message)
|
||||||
resp = append(resp, msg)
|
resp = append(resp, msg)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -354,7 +354,7 @@ func (cr ChatHandler) MessageList(c *gin.Context) {
|
|||||||
total++
|
total++
|
||||||
returnDataIdList = append(returnDataIdList, message.ID)
|
returnDataIdList = append(returnDataIdList, message.ID)
|
||||||
var msg = &dto.MessageListType{}
|
var msg = &dto.MessageListType{}
|
||||||
msg.BuildMessage(message, 0)
|
msg.BuildMessage(message)
|
||||||
resp = append(resp, msg)
|
resp = append(resp, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewMessage(ctx context.Context, cache *chatCache.ChatCache, chatUser *accountFiee.ChatUserData, request dto.NewMessageRequest) (err error) {
|
func NewMessage(ctx context.Context, cache *chatCache.ChatCache, sender *accountFiee.ChatUserData, request dto.NewMessageRequest) (err error) {
|
||||||
if request.SessionId == "" {
|
if request.SessionId == "" {
|
||||||
return errors.New("sessionId不能为空")
|
return errors.New("sessionId不能为空")
|
||||||
}
|
}
|
||||||
@ -30,19 +30,20 @@ func NewMessage(ctx context.Context, cache *chatCache.ChatCache, chatUser *accou
|
|||||||
fmt.Println("NewMessage 1111111111111111111111111111111")
|
fmt.Println("NewMessage 1111111111111111111111111111111")
|
||||||
fmt.Println("NewMessage 22222222222222222222222222222222222")
|
fmt.Println("NewMessage 22222222222222222222222222222222222")
|
||||||
//存储入库
|
//存储入库
|
||||||
if chatUser.NickName == "" {
|
if sender.NickName == "" {
|
||||||
chatUser.NickName = fmt.Sprintf("未知用户(%d)", chatUser.ID)
|
sender.NickName = fmt.Sprintf("未知用户(%d)", sender.ID)
|
||||||
}
|
}
|
||||||
fmt.Println("NewMessage 3333333333333333333333333333333333")
|
fmt.Println("NewMessage 3333333333333333333333333333333333")
|
||||||
var data = accountFiee.ChatRecordData{
|
var data = accountFiee.ChatRecordData{
|
||||||
SessionId: request.SessionId,
|
SessionId: request.SessionId,
|
||||||
UserId: chatUser.ID,
|
UserId: sender.ID,
|
||||||
Name: chatUser.NickName,
|
Name: sender.NickName,
|
||||||
Avatar: "",
|
Avatar: sender.Avatar,
|
||||||
MsgType: request.MsgType,
|
MsgType: request.MsgType,
|
||||||
Content: request.Message.Text,
|
Content: request.Message.Text,
|
||||||
LocalStamp: request.LocalStamp,
|
LocalStamp: request.LocalStamp,
|
||||||
Medias: nil,
|
Medias: nil,
|
||||||
|
Role: sender.Role,
|
||||||
}
|
}
|
||||||
if len(request.Message.Media) > 0 {
|
if len(request.Message.Media) > 0 {
|
||||||
for _, media := range request.Message.Media {
|
for _, media := range request.Message.Media {
|
||||||
@ -64,20 +65,22 @@ func NewMessage(ctx context.Context, cache *chatCache.ChatCache, chatUser *accou
|
|||||||
return errors.New("消息发送失败")
|
return errors.New("消息发送失败")
|
||||||
}
|
}
|
||||||
fmt.Println("NewMessage 5 消息数量+1")
|
fmt.Println("NewMessage 5 消息数量+1")
|
||||||
|
if sender.Role != 3 {
|
||||||
//新消息数量统计+1
|
//新消息数量统计+1
|
||||||
noticeUserId := consts.ChatRoom.GetUserIdInSession(request.SessionId, chatUser.ID)
|
noticeUserId := consts.ChatRoom.GetUserIdInSession(request.SessionId, sender.ID)
|
||||||
fmt.Println("NewMessage 5.1 消息数量配置结束")
|
fmt.Println("NewMessage 5.1 消息数量配置结束")
|
||||||
fmt.Printf("noticeUserId %+v\n", noticeUserId)
|
fmt.Printf("noticeUserId %+v\n", noticeUserId)
|
||||||
for _, userId := range noticeUserId {
|
for _, userId := range noticeUserId {
|
||||||
fmt.Println("userId")
|
fmt.Println("userId")
|
||||||
cache.IncreaseNewMessageTotal(userId, request.SessionId)
|
_ = cache.IncreaseNewMessageTotal(userId, request.SessionId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("NewMessage 6")
|
fmt.Println("NewMessage 6")
|
||||||
//发送websocket消息提醒通知
|
//发送websocket消息提醒通知
|
||||||
var notice = dto.MessageListType{}
|
var notice = dto.MessageListType{}
|
||||||
notice.BuildMessage(resp.Data, chatUser.Role)
|
notice.BuildMessage(resp.Data)
|
||||||
fmt.Printf("ws消息提醒:%+v\n", notice)
|
fmt.Printf("ws消息提醒:%+v\n", notice)
|
||||||
_, err = consts.ChatRoom.SendSessionMessage(chatUser, request.SessionId, ws.NewChatMsgType, notice)
|
_, err = consts.ChatRoom.SendSessionMessage(sender, request.SessionId, ws.NewChatMsgType, notice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("发送新消息通知失败", zap.Error(err), zap.Any("notice", notice))
|
log.Print("发送新消息通知失败", zap.Error(err), zap.Any("notice", notice))
|
||||||
}
|
}
|
||||||
|
@ -1 +1,28 @@
|
|||||||
# robot 聊天机器人
|
# robot 聊天机器人
|
||||||
|
|
||||||
|
web端和后端交互式时,增删改查的规则配置是存放在rules对象中的。在数据库中,rules字段是作为json字符串存放的。
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"title": "1",
|
||||||
|
"response": "11",
|
||||||
|
"rules": {
|
||||||
|
"keywords": {
|
||||||
|
"enable": true,
|
||||||
|
"content": "什么,为什么,怎么办,不是"
|
||||||
|
},
|
||||||
|
"joinSession": {
|
||||||
|
"enable": true
|
||||||
|
},
|
||||||
|
"noReplyAfter": {
|
||||||
|
"enable": false,
|
||||||
|
"secondDuration": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"status": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
如果有新增的规则,直接在rules对象中添加字段即可。然后去 [./rulerList.go](./rulerList.go) 中,增加规则的解析方法。
|
||||||
|
目前,在[./rulerList.go](./rulerList.go)定义了三种回复规则的解析方式:
|
||||||
|
- keywords :关键字回复
|
||||||
|
- joinSession:用户打开聊天窗口后
|
||||||
|
- noReplyAfter:客服指定时间没有回复后
|
||||||
|
@ -51,7 +51,10 @@ func (k ReplyWhenHitKeywords) Hit(event ws.ListenEventData, robotInfo *accountFi
|
|||||||
if event.EventType != ws.EventChatMessage || event.Msg == "" || event.Client == nil || event.ChatUser == nil {
|
if event.EventType != ws.EventChatMessage || event.Msg == "" || event.Client == nil || event.ChatUser == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//客服的消息不需要处理
|
||||||
|
if event.ChatUser.Role == 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, v := range k.Keywords {
|
for _, v := range k.Keywords {
|
||||||
if strings.Contains(event.Msg, v) {
|
if strings.Contains(event.Msg, v) {
|
||||||
hit = true
|
hit = true
|
||||||
@ -195,10 +198,15 @@ func NewReplyWhenWaiterNoAction(delaySecond time.Duration) *ReplyWhenWaiterNoAct
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, chatUser *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
||||||
if event.Client == nil || event.EventType != ws.EventChatMessage {
|
if event.Client == nil || event.EventType != ws.EventChatMessage {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//客服的消息不需要处理
|
||||||
|
if event.ChatUser.Role == 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hit = true
|
||||||
task = RobotTask{
|
task = RobotTask{
|
||||||
RunTime: time.Now().Add(k.DelaySecond * time.Second),
|
RunTime: time.Now().Add(k.DelaySecond * time.Second),
|
||||||
Run: func(content string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
Run: func(content string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
||||||
@ -232,13 +240,12 @@ func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, chatUser *accoun
|
|||||||
//notice.BuildMessage(newRecord)
|
//notice.BuildMessage(newRecord)
|
||||||
//_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
//_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
||||||
//return err
|
//return err
|
||||||
|
err = logic.NewMessage(context.Background(), cache, sender, dto.NewMessageRequest{
|
||||||
err = logic.NewMessage(context.Background(), cache, chatUser, dto.NewMessageRequest{
|
|
||||||
Waiter: true,
|
Waiter: true,
|
||||||
SessionId: event.Client.SessionId,
|
SessionId: event.Client.SessionId,
|
||||||
Message: dto.Message{
|
Message: dto.Message{
|
||||||
MsgType: 1,
|
MsgType: 1,
|
||||||
Text: event.Msg,
|
Text: content,
|
||||||
LocalStamp: time.Now().Unix(),
|
LocalStamp: time.Now().Unix(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -147,7 +147,9 @@ func (r *Robot) Run() {
|
|||||||
go func() {
|
go func() {
|
||||||
err := task.Run(task.Response, r.cache, task.ChatUser)
|
err := task.Run(task.Response, r.cache, task.ChatUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("聊天机器人[%d]回复消息失败:%v", r.Info.ID, err)
|
log.Printf("聊天机器人[%d]延时回复消息失败:%v", r.Info.ID, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("聊天机器人[%d]延时回复消息成功", r.Info.ID)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
@ -170,6 +172,7 @@ func (r *Robot) Run() {
|
|||||||
log.Printf("robot 执行任务失败:%v\n", err)
|
log.Printf("robot 执行任务失败:%v\n", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
ruleResponse := ruleResponse
|
||||||
task.Response = ruleResponse.Response
|
task.Response = ruleResponse.Response
|
||||||
r.RegisterDelayTask(task)
|
r.RegisterDelayTask(task)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user