Compare commits
No commits in common. "ac5fe7a2669a029f0a7a1c2b818acd3cf2178449" and "cd2d8863a74d38788eeb34f10ee0bc0ff7e3e560" have entirely different histories.
ac5fe7a266
...
cd2d8863a7
File diff suppressed because it is too large
Load Diff
@ -886,7 +886,6 @@ message ChatRecordData{
|
||||
int32 waiterRead=12;//客服是否已读 1=已读 2=未读 (被任意客服读取过均为已读)
|
||||
int64 localStamp = 13; //本地时间戳 用户端的消息唯一值,用于用户本地的一些逻辑处理
|
||||
string domain =14;//域
|
||||
int32 role=15;//用户角色: 1=用户 2=客服 3=机器人
|
||||
}
|
||||
message CreateChatRecordResp{
|
||||
ChatRecordData data=1;
|
||||
@ -1027,7 +1026,7 @@ message ChatUserData{
|
||||
int64 deletedAt = 4; //
|
||||
string nickName = 5; //昵称
|
||||
string account = 6; //账号
|
||||
int32 role = 7; //聊天角色 1=用户 2=客服 3=机器人
|
||||
int32 role = 7; //聊天角色 1=用户 2=客服
|
||||
string origin = 8; //数据来源
|
||||
int64 originId = 9; //数据来源对应的用户ID
|
||||
string avatar = 10; //头像
|
||||
|
@ -110,9 +110,6 @@ func (a *ChatAutoReplyRulerHandler) GetChatAutoReplyRulerList(c *gin.Context) {
|
||||
}
|
||||
var protoReq = accountFiee.GetChatAutoReplyRulerListRequest{Query: &accountFiee.ChatAutoReplyRulerData{}}
|
||||
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)
|
||||
if err != nil {
|
||||
service.Error(c, err)
|
||||
|
@ -60,12 +60,12 @@ type MessageListType struct {
|
||||
Message Message `json:"message"`
|
||||
}
|
||||
|
||||
func (m *MessageListType) BuildMessage(data *accountFiee.ChatRecordData) {
|
||||
func (m *MessageListType) BuildMessage(data *accountFiee.ChatRecordData, role int32) {
|
||||
m.ID = data.ID
|
||||
m.CreatedAt = data.CreatedAt
|
||||
m.UserId = data.UserId
|
||||
m.Name = data.Name
|
||||
m.Role = data.Role
|
||||
m.Role = role
|
||||
switch data.MsgType {
|
||||
default:
|
||||
m.Message.MsgType = data.MsgType
|
||||
@ -152,7 +152,6 @@ type GetChatAutoReplyRulerListRequest struct {
|
||||
Page int64 `json:"page"`
|
||||
PageSize int64 `json:"pageSize"`
|
||||
accountFiee.ChatAutoReplyRulerData
|
||||
RuleType string `json:"ruleType"`
|
||||
}
|
||||
type ErpLoginDemoReq struct {
|
||||
TelNum string `json:"telNum"`
|
||||
|
@ -322,7 +322,7 @@ func (cr ChatHandler) MessageList(c *gin.Context) {
|
||||
}
|
||||
returnDataIdList = append(returnDataIdList, message.ID)
|
||||
var msg = &dto.MessageListType{}
|
||||
msg.BuildMessage(message)
|
||||
msg.BuildMessage(message, 0)
|
||||
resp = append(resp, msg)
|
||||
}
|
||||
} else {
|
||||
@ -354,7 +354,7 @@ func (cr ChatHandler) MessageList(c *gin.Context) {
|
||||
total++
|
||||
returnDataIdList = append(returnDataIdList, message.ID)
|
||||
var msg = &dto.MessageListType{}
|
||||
msg.BuildMessage(message)
|
||||
msg.BuildMessage(message, 0)
|
||||
resp = append(resp, msg)
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func NewMessage(ctx context.Context, cache *chatCache.ChatCache, sender *accountFiee.ChatUserData, request dto.NewMessageRequest) (err error) {
|
||||
func NewMessage(ctx context.Context, cache *chatCache.ChatCache, chatUser *accountFiee.ChatUserData, request dto.NewMessageRequest) (err error) {
|
||||
if request.SessionId == "" {
|
||||
return errors.New("sessionId不能为空")
|
||||
}
|
||||
@ -30,20 +30,19 @@ func NewMessage(ctx context.Context, cache *chatCache.ChatCache, sender *account
|
||||
fmt.Println("NewMessage 1111111111111111111111111111111")
|
||||
fmt.Println("NewMessage 22222222222222222222222222222222222")
|
||||
//存储入库
|
||||
if sender.NickName == "" {
|
||||
sender.NickName = fmt.Sprintf("未知用户(%d)", sender.ID)
|
||||
if chatUser.NickName == "" {
|
||||
chatUser.NickName = fmt.Sprintf("未知用户(%d)", chatUser.ID)
|
||||
}
|
||||
fmt.Println("NewMessage 3333333333333333333333333333333333")
|
||||
var data = accountFiee.ChatRecordData{
|
||||
SessionId: request.SessionId,
|
||||
UserId: sender.ID,
|
||||
Name: sender.NickName,
|
||||
Avatar: sender.Avatar,
|
||||
UserId: chatUser.ID,
|
||||
Name: chatUser.NickName,
|
||||
Avatar: "",
|
||||
MsgType: request.MsgType,
|
||||
Content: request.Message.Text,
|
||||
LocalStamp: request.LocalStamp,
|
||||
Medias: nil,
|
||||
Role: sender.Role,
|
||||
}
|
||||
if len(request.Message.Media) > 0 {
|
||||
for _, media := range request.Message.Media {
|
||||
@ -65,22 +64,20 @@ func NewMessage(ctx context.Context, cache *chatCache.ChatCache, sender *account
|
||||
return errors.New("消息发送失败")
|
||||
}
|
||||
fmt.Println("NewMessage 5 消息数量+1")
|
||||
if sender.Role != 3 {
|
||||
//新消息数量统计+1
|
||||
noticeUserId := consts.ChatRoom.GetUserIdInSession(request.SessionId, sender.ID)
|
||||
noticeUserId := consts.ChatRoom.GetUserIdInSession(request.SessionId, chatUser.ID)
|
||||
fmt.Println("NewMessage 5.1 消息数量配置结束")
|
||||
fmt.Printf("noticeUserId %+v\n", noticeUserId)
|
||||
for _, userId := range noticeUserId {
|
||||
fmt.Println("userId")
|
||||
_ = cache.IncreaseNewMessageTotal(userId, request.SessionId)
|
||||
}
|
||||
cache.IncreaseNewMessageTotal(userId, request.SessionId)
|
||||
}
|
||||
fmt.Println("NewMessage 6")
|
||||
//发送websocket消息提醒通知
|
||||
var notice = dto.MessageListType{}
|
||||
notice.BuildMessage(resp.Data)
|
||||
notice.BuildMessage(resp.Data, chatUser.Role)
|
||||
fmt.Printf("ws消息提醒:%+v\n", notice)
|
||||
_, err = consts.ChatRoom.SendSessionMessage(sender, request.SessionId, ws.NewChatMsgType, notice)
|
||||
_, err = consts.ChatRoom.SendSessionMessage(chatUser, request.SessionId, ws.NewChatMsgType, notice)
|
||||
if err != nil {
|
||||
log.Print("发送新消息通知失败", zap.Error(err), zap.Any("notice", notice))
|
||||
}
|
||||
|
@ -1,28 +1 @@
|
||||
# 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,10 +51,7 @@ func (k ReplyWhenHitKeywords) Hit(event ws.ListenEventData, robotInfo *accountFi
|
||||
if event.EventType != ws.EventChatMessage || event.Msg == "" || event.Client == nil || event.ChatUser == nil {
|
||||
return
|
||||
}
|
||||
//客服的消息不需要处理
|
||||
if event.ChatUser.Role == 2 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range k.Keywords {
|
||||
if strings.Contains(event.Msg, v) {
|
||||
hit = true
|
||||
@ -198,15 +195,10 @@ func NewReplyWhenWaiterNoAction(delaySecond time.Duration) *ReplyWhenWaiterNoAct
|
||||
}
|
||||
}
|
||||
|
||||
func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
||||
func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, chatUser *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
||||
if event.Client == nil || event.EventType != ws.EventChatMessage {
|
||||
return
|
||||
}
|
||||
//客服的消息不需要处理
|
||||
if event.ChatUser.Role == 2 {
|
||||
return
|
||||
}
|
||||
hit = true
|
||||
task = RobotTask{
|
||||
RunTime: time.Now().Add(k.DelaySecond * time.Second),
|
||||
Run: func(content string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
||||
@ -240,12 +232,13 @@ func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountF
|
||||
//notice.BuildMessage(newRecord)
|
||||
//_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
||||
//return err
|
||||
err = logic.NewMessage(context.Background(), cache, sender, dto.NewMessageRequest{
|
||||
|
||||
err = logic.NewMessage(context.Background(), cache, chatUser, dto.NewMessageRequest{
|
||||
Waiter: true,
|
||||
SessionId: event.Client.SessionId,
|
||||
Message: dto.Message{
|
||||
MsgType: 1,
|
||||
Text: content,
|
||||
Text: event.Msg,
|
||||
LocalStamp: time.Now().Unix(),
|
||||
},
|
||||
})
|
||||
|
@ -147,9 +147,7 @@ func (r *Robot) Run() {
|
||||
go func() {
|
||||
err := task.Run(task.Response, r.cache, task.ChatUser)
|
||||
if err != nil {
|
||||
log.Printf("聊天机器人[%d]延时回复消息失败:%v", r.Info.ID, err)
|
||||
} else {
|
||||
log.Printf("聊天机器人[%d]延时回复消息成功", r.Info.ID)
|
||||
log.Printf("聊天机器人[%d]回复消息失败:%v", r.Info.ID, err)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
@ -172,7 +170,6 @@ func (r *Robot) Run() {
|
||||
log.Printf("robot 执行任务失败:%v\n", err)
|
||||
}
|
||||
} else {
|
||||
ruleResponse := ruleResponse
|
||||
task.Response = ruleResponse.Response
|
||||
r.RegisterDelayTask(task)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user