fix: 重构回复规则
This commit is contained in:
parent
42b6c1262d
commit
e1fbbfce04
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
type AutoReply struct {
|
type AutoReply struct {
|
||||||
Response string `json:"response"`
|
Response string `json:"response"`
|
||||||
Rules map[string]IRule `json:"rules"`
|
Rules map[string]IRobotTask `json:"rules"`
|
||||||
}
|
}
|
||||||
type AutoReplyRule struct {
|
type AutoReplyRule struct {
|
||||||
Enable bool `json:"enable"`
|
Enable bool `json:"enable"`
|
||||||
|
@ -26,3 +26,6 @@ web端和后端交互式时,增删改查的规则配置是存放在rules对象
|
|||||||
- keywords :关键字回复
|
- keywords :关键字回复
|
||||||
- joinSession:用户打开聊天窗口后
|
- joinSession:用户打开聊天窗口后
|
||||||
- noReplyAfter:客服指定时间没有回复后
|
- noReplyAfter:客服指定时间没有回复后
|
||||||
|
|
||||||
|
## 注意
|
||||||
|
- 目前不支持用户多端登录,会导致用户收到重复消息
|
@ -7,28 +7,20 @@
|
|||||||
package robot
|
package robot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"fonchain-fiee/api/accountFiee"
|
"fonchain-fiee/api/accountFiee"
|
||||||
"fonchain-fiee/pkg/common/ws"
|
"fonchain-fiee/pkg/common/ws"
|
||||||
"fonchain-fiee/pkg/service"
|
|
||||||
"fonchain-fiee/pkg/service/asChat/chatCache"
|
|
||||||
"fonchain-fiee/pkg/service/asChat/dto"
|
|
||||||
"fonchain-fiee/pkg/service/asChat/logic"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 回复规则
|
// 回复规则
|
||||||
type Reply struct {
|
type Reply struct {
|
||||||
Title string
|
Title string
|
||||||
Response string
|
Response string
|
||||||
Rules []IRule
|
Rules []IRobotTask
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reply) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
func (r *Reply) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, rule IRobotTask) {
|
||||||
for _, rule := range r.Rules {
|
for _, rule = range r.Rules {
|
||||||
hit, task = rule.Hit(event, robotInfo)
|
hit = rule.Hit(event, robotInfo)
|
||||||
if hit {
|
if hit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -36,232 +28,219 @@ func (r *Reply) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserDat
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 规则接口
|
//
|
||||||
type IRule interface {
|
//// 规则接口
|
||||||
Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask)
|
//type IRule interface {
|
||||||
}
|
// Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask)
|
||||||
|
//}
|
||||||
// KeywordsRuleChecker 关键字回复
|
//
|
||||||
type ReplyWhenHitKeywords struct {
|
//// KeywordsRuleChecker 关键字回复
|
||||||
Keywords []string `json:"keywords"`
|
//type ReplyWhenHitKeywords struct {
|
||||||
}
|
// Keywords []string `json:"keywords"`
|
||||||
|
//}
|
||||||
func NewReplyWhenHitKeywords(keywords []string) IRule {
|
//
|
||||||
return &ReplyWhenHitKeywords{Keywords: keywords}
|
//func NewReplyWhenHitKeywords(keywords []string) IRule {
|
||||||
}
|
// return &ReplyWhenHitKeywords{Keywords: keywords}
|
||||||
func (k ReplyWhenHitKeywords) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
//}
|
||||||
if event.EventType != ws.EventChatMessage || event.Msg == "" || event.Client == nil || event.ChatUser == nil {
|
//func (k ReplyWhenHitKeywords) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
||||||
return
|
// if event.EventType != ws.EventChatMessage || event.Msg == "" || event.Client == nil || event.ChatUser == nil {
|
||||||
}
|
// return
|
||||||
if event.ChatUser.Role != 1 {
|
// }
|
||||||
return
|
// if event.ChatUser.Role != 1 {
|
||||||
}
|
// return
|
||||||
for _, v := range k.Keywords {
|
// }
|
||||||
if strings.Contains(event.Msg, v) {
|
// for _, v := range k.Keywords {
|
||||||
hit = true
|
// if strings.Contains(event.Msg, v) {
|
||||||
break
|
// hit = true
|
||||||
}
|
// break
|
||||||
}
|
// }
|
||||||
task = RobotTask{
|
// }
|
||||||
ChatUser: event.ChatUser,
|
// atUserId := event.Client.UserId
|
||||||
Run: func(msg string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
// task = RobotTask{
|
||||||
return logic.NewMessage(context.Background(), cache, Sender, dto.NewMessageRequest{
|
// ChatUser: event.ChatUser,
|
||||||
Waiter: true,
|
// Run: func(msg string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
||||||
SessionId: event.Client.SessionId,
|
// return logic.NewMessage(context.Background(), cache, Sender, dto.NewMessageRequest{
|
||||||
Message: dto.Message{
|
// Waiter: true,
|
||||||
MsgType: 1,
|
// Robot: true,
|
||||||
Text: msg,
|
// AtUserId: atUserId,
|
||||||
LocalStamp: time.Now().Unix(),
|
// SessionId: event.Client.SessionId,
|
||||||
},
|
// Message: dto.Message{
|
||||||
})
|
// MsgType: 1,
|
||||||
},
|
// Text: msg,
|
||||||
}
|
// LocalStamp: time.Now().Unix(),
|
||||||
//logicFunc = func(content string, cache *chatCache.ChatCache, chatUser *accountFiee.ChatUserData) error {
|
// },
|
||||||
// //var notice = dto.MessageListType{}
|
// })
|
||||||
// //newRecord := &accountFiee.ChatRecordData{
|
// },
|
||||||
// // SessionId: wsClient.SessionId,
|
// }
|
||||||
// // UserId: wsClient.UserId,
|
// return
|
||||||
// // Name: chatUser.NickName,
|
//}
|
||||||
// // Avatar: robotInfo.Avatar,
|
//
|
||||||
// // MsgType: 1,
|
//// 用户打开聊天会话直接发送
|
||||||
// // Content: content,
|
//type ReplyWhenUserJoinSession struct {
|
||||||
// //}
|
//}
|
||||||
// //notice.BuildMessage(newRecord)
|
//
|
||||||
// //_, err := consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
//func NewReplyWhenUserJoinSession() IRule {
|
||||||
// //return err
|
// return &ReplyWhenUserJoinSession{}
|
||||||
// err := logic.NewMessage(context.Background(), cache, chatUser, dto.NewMessageRequest{
|
//}
|
||||||
// Waiter: true,
|
//
|
||||||
// SessionId: wsClient.SessionId,
|
//func (k ReplyWhenUserJoinSession) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
||||||
// Message: dto.Message{
|
// if event.EventType != ws.EventUserJoin {
|
||||||
// MsgType: 1,
|
// return
|
||||||
// Text: msg,
|
// }
|
||||||
// LocalStamp: time.Now().Unix(),
|
// if event.Client == nil {
|
||||||
// },
|
// return
|
||||||
// })
|
// }
|
||||||
// return err
|
// clientSessionId := event.Client.SessionId
|
||||||
//}
|
// atUserId := event.Client.UserId
|
||||||
return
|
// ctx := context.Background()
|
||||||
}
|
// queryRes, err := service.AccountFieeProvider.GetChatRecordList(ctx, &accountFiee.GetChatRecordListRequest{
|
||||||
|
// Query: &accountFiee.ChatRecordData{
|
||||||
// 用户打开聊天会话直接发送
|
// SessionId: event.Client.SessionId,
|
||||||
type ReplyWhenUserJoinSession struct {
|
// },
|
||||||
}
|
// Page: 1,
|
||||||
|
// PageSize: 1,
|
||||||
func NewReplyWhenUserJoinSession() IRule {
|
// Order: "created_at desc",
|
||||||
return &ReplyWhenUserJoinSession{}
|
// })
|
||||||
}
|
// if err != nil {
|
||||||
|
// return
|
||||||
func (k ReplyWhenUserJoinSession) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
// }
|
||||||
if event.EventType != ws.EventUserJoin {
|
// //如果最近一次的消息也是机器人发送的,就不再发送了
|
||||||
return
|
// for i, v := range queryRes.List {
|
||||||
}
|
// if i == 0 {
|
||||||
if event.Client == nil {
|
// if v.UserId == robotInfo.ID {
|
||||||
return
|
// return
|
||||||
}
|
// } else {
|
||||||
ctx := context.Background()
|
// break
|
||||||
queryRes, err := service.AccountFieeProvider.GetChatRecordList(ctx, &accountFiee.GetChatRecordListRequest{
|
// }
|
||||||
Query: &accountFiee.ChatRecordData{
|
// }
|
||||||
SessionId: event.Client.SessionId,
|
// }
|
||||||
},
|
// hit = true
|
||||||
Page: 1,
|
// if event.ChatUser == nil {
|
||||||
PageSize: 1,
|
// event.ChatUser, err = service.AccountFieeProvider.GetChatUserDetail(context.Background(), &accountFiee.GetChatUserByIdRequest{Id: event.Client.UserId})
|
||||||
Order: "created_at desc",
|
// if err != nil {
|
||||||
})
|
// return
|
||||||
if err != nil {
|
// }
|
||||||
return
|
// }
|
||||||
}
|
// task = RobotTask{
|
||||||
//如果最近一次的消息也是机器人发送的,就不再发送了
|
// ChatUser: event.ChatUser,
|
||||||
for i, v := range queryRes.List {
|
// Run: func(msg string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
||||||
if i == 0 {
|
// return logic.NewMessage(ctx, cache, Sender, dto.NewMessageRequest{
|
||||||
if v.UserId == robotInfo.ID {
|
// Waiter: true,
|
||||||
return
|
// Robot: true,
|
||||||
} else {
|
// AtUserId: atUserId,
|
||||||
break
|
// SessionId: clientSessionId,
|
||||||
}
|
// Message: dto.Message{
|
||||||
}
|
// MsgType: 1,
|
||||||
}
|
// Text: msg,
|
||||||
hit = true
|
// LocalStamp: time.Now().Unix(),
|
||||||
if event.ChatUser == nil {
|
// },
|
||||||
event.ChatUser, err = service.AccountFieeProvider.GetChatUserDetail(context.Background(), &accountFiee.GetChatUserByIdRequest{Id: event.Client.UserId})
|
// })
|
||||||
if err != nil {
|
// },
|
||||||
return
|
// }
|
||||||
}
|
// //logicFunc = func(msg string, cache *chatCache.ChatCache, chatUser *accountFiee.ChatUserData) error {
|
||||||
}
|
// // //var notice = dto.MessageListType{}
|
||||||
task = RobotTask{
|
// // //newRecord := &accountFiee.ChatRecordData{
|
||||||
ChatUser: event.ChatUser,
|
// // // SessionId: wsClient.SessionId,
|
||||||
Run: func(msg string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
// // // UserId: wsClient.UserId,
|
||||||
return logic.NewMessage(ctx, cache, Sender, dto.NewMessageRequest{
|
// // // Name: wsClient.SessionId,
|
||||||
Waiter: true,
|
// // // Avatar: robotInfo.Avatar,
|
||||||
SessionId: event.Client.SessionId,
|
// // // MsgType: 1,
|
||||||
Message: dto.Message{
|
// // // Content: msg,
|
||||||
MsgType: 1,
|
// // //}
|
||||||
Text: msg,
|
// // //notice.BuildMessage(newRecord)
|
||||||
LocalStamp: time.Now().Unix(),
|
// // //_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
||||||
},
|
// // err = logic.NewMessage(ctx, cache, chatUser, dto.NewMessageRequest{
|
||||||
})
|
// // Waiter: true,
|
||||||
},
|
// // SessionId: wsClient.SessionId,
|
||||||
}
|
// // Message: dto.Message{
|
||||||
//logicFunc = func(msg string, cache *chatCache.ChatCache, chatUser *accountFiee.ChatUserData) error {
|
// // MsgType: 1,
|
||||||
// //var notice = dto.MessageListType{}
|
// // Text: msg,
|
||||||
// //newRecord := &accountFiee.ChatRecordData{
|
// // LocalStamp: time.Now().Unix(),
|
||||||
// // SessionId: wsClient.SessionId,
|
// // },
|
||||||
// // UserId: wsClient.UserId,
|
// // })
|
||||||
// // Name: wsClient.SessionId,
|
// // return err
|
||||||
// // Avatar: robotInfo.Avatar,
|
// //}
|
||||||
// // MsgType: 1,
|
// return
|
||||||
// // Content: msg,
|
//}
|
||||||
// //}
|
//
|
||||||
// //notice.BuildMessage(newRecord)
|
//// 客服指定时间不回复则自动回复
|
||||||
// //_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
//
|
||||||
// err = logic.NewMessage(ctx, cache, chatUser, dto.NewMessageRequest{
|
//type ReplyWhenWaiterNoAction struct {
|
||||||
// Waiter: true,
|
// DelaySecond time.Duration
|
||||||
// SessionId: wsClient.SessionId,
|
//}
|
||||||
// Message: dto.Message{
|
//
|
||||||
// MsgType: 1,
|
////func NewReplyWhenWaiterNoAction(delaySecond time.Duration) *ReplyWhenWaiterNoAction {
|
||||||
// Text: msg,
|
//// return &ReplyWhenWaiterNoAction{
|
||||||
// LocalStamp: time.Now().Unix(),
|
//// DelaySecond: delaySecond,
|
||||||
// },
|
//// }
|
||||||
// })
|
////}
|
||||||
// return err
|
//
|
||||||
//}
|
//func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
||||||
return
|
// if event.Client == nil || event.EventType != ws.EventChatMessage {
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
// 客服指定时间不回复则自动回复
|
// //客服和机器人的的消息不需要处理
|
||||||
|
// if event.ChatUser.Role != 1 {
|
||||||
type ReplyWhenWaiterNoAction struct {
|
// return
|
||||||
DelaySecond time.Duration
|
// }
|
||||||
}
|
// hit = true // 立即保存SessionId的值
|
||||||
|
//
|
||||||
func NewReplyWhenWaiterNoAction(delaySecond time.Duration) *ReplyWhenWaiterNoAction {
|
// clientSessionId := event.Client.SessionId
|
||||||
return &ReplyWhenWaiterNoAction{
|
// atUserId := event.Client.UserId
|
||||||
DelaySecond: delaySecond,
|
// fmt.Printf("闭包前: clientSessionId=%s\n", clientSessionId)
|
||||||
}
|
// task = RobotTask{
|
||||||
}
|
// RunTime: time.Now().Add(k.DelaySecond * time.Second),
|
||||||
|
// Run: func(content string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
||||||
func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
// // 记录闭包执行时的Client状态
|
||||||
if event.Client == nil || event.EventType != ws.EventChatMessage {
|
// fmt.Printf("闭包执行: clientSessionId=%s\n", clientSessionId)
|
||||||
return
|
//
|
||||||
}
|
// //如果客服已经回复则不发送消息
|
||||||
//客服和机器人的的消息不需要处理
|
// chatRecordListRes, err := service.AccountFieeProvider.GetChatRecordList(context.Background(), &accountFiee.GetChatRecordListRequest{
|
||||||
if event.ChatUser.Role != 1 {
|
// Query: &accountFiee.ChatRecordData{
|
||||||
return
|
// SessionId: event.Client.SessionId,
|
||||||
}
|
// },
|
||||||
hit = true // 立即保存SessionId的值
|
// Page: 1,
|
||||||
clientSessionId := event.Client.SessionId
|
// PageSize: 1,
|
||||||
|
// Order: "created_at desc",
|
||||||
fmt.Printf("闭包前: clientSessionId=%s\n", clientSessionId)
|
// })
|
||||||
task = RobotTask{
|
// if err != nil || chatRecordListRes.Total == 0 {
|
||||||
RunTime: time.Now().Add(k.DelaySecond * time.Second),
|
// return err
|
||||||
Run: func(content string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
// }
|
||||||
// 记录闭包执行时的Client状态
|
// checkUserId := chatRecordListRes.List[0].UserId
|
||||||
fmt.Printf("闭包执行: clientSessionId=%s\n", clientSessionId)
|
// checkChatUser, err := service.AccountFieeProvider.GetChatUserDetail(context.Background(), &accountFiee.GetChatUserByIdRequest{Id: checkUserId})
|
||||||
|
// if err != nil || checkChatUser.Role != 1 {
|
||||||
//如果客服已经回复则不发送消息
|
// return err
|
||||||
chatRecordListRes, err := service.AccountFieeProvider.GetChatRecordList(context.Background(), &accountFiee.GetChatRecordListRequest{
|
// }
|
||||||
Query: &accountFiee.ChatRecordData{
|
//
|
||||||
SessionId: event.Client.SessionId,
|
// //var notice = dto.MessageListType{}
|
||||||
},
|
// //newRecord := &accountFiee.ChatRecordData{
|
||||||
Page: 1,
|
// // SessionId: wsClient.SessionId,
|
||||||
PageSize: 1,
|
// // UserId: wsClient.UserId,
|
||||||
Order: "created_at desc",
|
// // Name: chatUser.NickName,
|
||||||
})
|
// // Avatar: robotInfo.Avatar,
|
||||||
if err != nil || chatRecordListRes.Total == 0 {
|
// // MsgType: 1,
|
||||||
return err
|
// // Content: content,
|
||||||
}
|
// //}
|
||||||
checkUserId := chatRecordListRes.List[0].UserId
|
// //notice.BuildMessage(newRecord)
|
||||||
checkChatUser, err := service.AccountFieeProvider.GetChatUserDetail(context.Background(), &accountFiee.GetChatUserByIdRequest{Id: checkUserId})
|
// //_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
||||||
if err != nil || checkChatUser.Role != 1 {
|
// //return err
|
||||||
return err
|
// fmt.Println("延时回复 sessionID:", clientSessionId)
|
||||||
}
|
// err = logic.NewMessage(context.Background(), cache, sender, dto.NewMessageRequest{
|
||||||
|
// Waiter: true,
|
||||||
//var notice = dto.MessageListType{}
|
// Robot: true,
|
||||||
//newRecord := &accountFiee.ChatRecordData{
|
// AtUserId: atUserId,
|
||||||
// SessionId: wsClient.SessionId,
|
// SessionId: clientSessionId,
|
||||||
// UserId: wsClient.UserId,
|
// Message: dto.Message{
|
||||||
// Name: chatUser.NickName,
|
// MsgType: 1,
|
||||||
// Avatar: robotInfo.Avatar,
|
// Text: content,
|
||||||
// MsgType: 1,
|
// LocalStamp: time.Now().Unix(),
|
||||||
// Content: content,
|
// },
|
||||||
//}
|
// })
|
||||||
//notice.BuildMessage(newRecord)
|
// return err
|
||||||
//_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
// },
|
||||||
//return err
|
// Response: "",
|
||||||
fmt.Println("延时回复 sessionID:", clientSessionId)
|
// ChatUser: event.ChatUser,
|
||||||
err = logic.NewMessage(context.Background(), cache, sender, dto.NewMessageRequest{
|
// }
|
||||||
Waiter: true,
|
// return
|
||||||
SessionId: clientSessionId,
|
//
|
||||||
Message: dto.Message{
|
//}
|
||||||
MsgType: 1,
|
|
||||||
Text: content,
|
|
||||||
LocalStamp: time.Now().Unix(),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
Response: "",
|
|
||||||
ChatUser: event.ChatUser,
|
|
||||||
}
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -57,7 +57,7 @@ func NewRobot(cache *chatCache.ChatCache) *Robot {
|
|||||||
cache: cache,
|
cache: cache,
|
||||||
}
|
}
|
||||||
err = r.ReloadRules(ctx)
|
err = r.ReloadRules(ctx)
|
||||||
fmt.Println("机器人规则加载失败")
|
fmt.Println("机器人规则加载完成,结果:", err)
|
||||||
consts.ChatRoom.RegisterEventListener(r.EventListener)
|
consts.ChatRoom.RegisterEventListener(r.EventListener)
|
||||||
go r.Run()
|
go r.Run()
|
||||||
return r
|
return r
|
||||||
@ -66,7 +66,7 @@ func NewRobot(cache *chatCache.ChatCache) *Robot {
|
|||||||
type Robot struct {
|
type Robot struct {
|
||||||
Info *accountFiee.ChatUserData //机器人信息
|
Info *accountFiee.ChatUserData //机器人信息
|
||||||
Rules []Reply //回复规则
|
Rules []Reply //回复规则
|
||||||
DelayTask []RobotTask //演示任务
|
DelayTask []IRobotTask //演示任务
|
||||||
ticker *time.Ticker //定时器
|
ticker *time.Ticker //定时器
|
||||||
stopChan chan struct{} //停止管道
|
stopChan chan struct{} //停止管道
|
||||||
isRunning bool //运行状态
|
isRunning bool //运行状态
|
||||||
@ -140,12 +140,12 @@ func (r *Robot) Run() {
|
|||||||
//return // 没有任务时退出
|
//return // 没有任务时退出
|
||||||
}
|
}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
var remainingTasks []RobotTask
|
var remainingTasks []IRobotTask
|
||||||
for _, task := range r.DelayTask {
|
for _, task := range r.DelayTask {
|
||||||
if now.After(task.RunTime) {
|
if now.After(task.RunTime()) {
|
||||||
// 执行任务
|
// 执行任务
|
||||||
go func() {
|
go func() {
|
||||||
err := task.Run(task.Response, r.cache, task.ChatUser)
|
err := task.Run(r.cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("聊天机器人[%d]延时回复消息失败:%v", r.Info.ID, err)
|
log.Printf("聊天机器人[%d]延时回复消息失败:%v", r.Info.ID, err)
|
||||||
} else {
|
} else {
|
||||||
@ -167,16 +167,16 @@ func (r *Robot) Run() {
|
|||||||
hit, task := ruleResponse.Hit(event, r.Info)
|
hit, task := ruleResponse.Hit(event, r.Info)
|
||||||
if hit {
|
if hit {
|
||||||
fmt.Println("命中规则:", ruleResponse.Title)
|
fmt.Println("命中规则:", ruleResponse.Title)
|
||||||
if task.RunTime.IsZero() {
|
if task.RunTime().IsZero() {
|
||||||
err := task.Run(ruleResponse.Response, r.cache, r.Info)
|
task.SetResponse(ruleResponse.Response)
|
||||||
|
err := task.Run(r.cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("robot 执行任务失败:%v\n", err)
|
log.Printf("robot 执行任务失败:%v\n", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ruleResponse := ruleResponse
|
ruleResponse := ruleResponse
|
||||||
task.Response = ruleResponse.Response
|
task.SetResponse(ruleResponse.Response)
|
||||||
r.RegisterDelayTask(task)
|
r.RegisterDelayTask(task)
|
||||||
|
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -193,7 +193,7 @@ func (r *Robot) Stop() {
|
|||||||
}
|
}
|
||||||
r.mu.Unlock()
|
r.mu.Unlock()
|
||||||
}
|
}
|
||||||
func (r *Robot) RegisterDelayTask(task RobotTask) {
|
func (r *Robot) RegisterDelayTask(task IRobotTask) {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
if task.Run == nil {
|
if task.Run == nil {
|
||||||
|
98
pkg/service/asChat/robot/ruler_ReplyWhenWaiterNoAction.go
Normal file
98
pkg/service/asChat/robot/ruler_ReplyWhenWaiterNoAction.go
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
// Package robot -----------------------------
|
||||||
|
// @file : ruler_ReplyWhenWaiterNoAction.go
|
||||||
|
// @author : JJXu
|
||||||
|
// @contact : wavingbear@163.com
|
||||||
|
// @time : 2025/6/13 18:02
|
||||||
|
// -------------------------------------------
|
||||||
|
package robot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"fonchain-fiee/api/accountFiee"
|
||||||
|
"fonchain-fiee/pkg/common/ws"
|
||||||
|
"fonchain-fiee/pkg/service"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/chatCache"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/dto"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/logic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type IRobotTask interface {
|
||||||
|
Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool)
|
||||||
|
Run(cache *chatCache.ChatCache) error
|
||||||
|
RunTime() time.Time
|
||||||
|
SetResponse(response string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 客服指定时间不回复则自动回复
|
||||||
|
func NewReplyWhenWaiterNoAction(delaySecond time.Duration) IRobotTask {
|
||||||
|
return &RobotTaskReplyWhenWaiterNoAction{
|
||||||
|
delaySecond: delaySecond,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type RobotTaskReplyWhenWaiterNoAction struct {
|
||||||
|
runTime time.Time
|
||||||
|
Response string
|
||||||
|
Receiver *accountFiee.ChatUserData
|
||||||
|
Sender *accountFiee.ChatUserData
|
||||||
|
Msg string
|
||||||
|
Resp string
|
||||||
|
delaySecond time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RobotTaskReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool) {
|
||||||
|
if event.Client == nil || event.EventType != ws.EventChatMessage {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//客服和机器人的的消息不需要处理
|
||||||
|
if event.ChatUser.Role != 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hit = true // 立即保存SessionId的值
|
||||||
|
r.Sender = sender
|
||||||
|
r.Receiver = event.ChatUser
|
||||||
|
r.runTime = time.Now().Add(r.delaySecond * time.Second)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RobotTaskReplyWhenWaiterNoAction) Run(cache *chatCache.ChatCache) error {
|
||||||
|
clientSessionId := fmt.Sprintf("%d", r.Receiver.ID)
|
||||||
|
fmt.Println("延时回复 sessionID:", clientSessionId)
|
||||||
|
//如果客服已经回复则不发送消息
|
||||||
|
chatRecordListRes, err := service.AccountFieeProvider.GetChatRecordList(context.Background(), &accountFiee.GetChatRecordListRequest{
|
||||||
|
Query: &accountFiee.ChatRecordData{
|
||||||
|
SessionId: clientSessionId,
|
||||||
|
},
|
||||||
|
Page: 1,
|
||||||
|
PageSize: 1,
|
||||||
|
Order: "created_at desc",
|
||||||
|
})
|
||||||
|
if err != nil || chatRecordListRes.Total == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
checkUserId := chatRecordListRes.List[0].UserId
|
||||||
|
checkChatUser, err := service.AccountFieeProvider.GetChatUserDetail(context.Background(), &accountFiee.GetChatUserByIdRequest{Id: checkUserId})
|
||||||
|
if err != nil || checkChatUser.Role != 1 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = logic.NewMessage(context.Background(), cache, r.Sender, dto.NewMessageRequest{
|
||||||
|
Waiter: true,
|
||||||
|
Robot: true,
|
||||||
|
AtUserId: r.Receiver.ID,
|
||||||
|
SessionId: clientSessionId,
|
||||||
|
Message: dto.Message{
|
||||||
|
MsgType: 1,
|
||||||
|
Text: r.Resp,
|
||||||
|
LocalStamp: time.Now().Unix(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
func (r *RobotTaskReplyWhenWaiterNoAction) RunTime() time.Time {
|
||||||
|
return r.runTime
|
||||||
|
}
|
||||||
|
func (r *RobotTaskReplyWhenWaiterNoAction) SetResponse(response string) {
|
||||||
|
r.Resp = response
|
||||||
|
}
|
67
pkg/service/asChat/robot/ruler_keywords.go
Normal file
67
pkg/service/asChat/robot/ruler_keywords.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package robot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"fonchain-fiee/api/accountFiee"
|
||||||
|
"fonchain-fiee/pkg/common/ws"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/chatCache"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/dto"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/logic"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RobotTaskWithKeyworkds struct {
|
||||||
|
runTime time.Time
|
||||||
|
Response string
|
||||||
|
Receiver *accountFiee.ChatUserData
|
||||||
|
Sender *accountFiee.ChatUserData
|
||||||
|
Msg string
|
||||||
|
Resp string
|
||||||
|
keywords []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReplyWhenHitKeywords(keywords []string) IRobotTask {
|
||||||
|
return &RobotTaskWithKeyworkds{keywords: keywords}
|
||||||
|
}
|
||||||
|
func (r *RobotTaskWithKeyworkds) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool) {
|
||||||
|
if event.EventType != ws.EventChatMessage || event.Msg == "" || event.Client == nil || event.ChatUser == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if event.ChatUser.Role != 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.Sender = sender
|
||||||
|
r.Receiver = event.ChatUser
|
||||||
|
for _, v := range r.keywords {
|
||||||
|
if strings.Contains(event.Msg, v) {
|
||||||
|
hit = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RobotTaskWithKeyworkds) Run(cache *chatCache.ChatCache) (err error) {
|
||||||
|
err = logic.NewMessage(context.Background(), cache, r.Sender, dto.NewMessageRequest{
|
||||||
|
Waiter: true,
|
||||||
|
Robot: true,
|
||||||
|
AtUserId: r.Receiver.ID,
|
||||||
|
SessionId: fmt.Sprintf("%d", r.Receiver.ID),
|
||||||
|
Message: dto.Message{
|
||||||
|
MsgType: 1,
|
||||||
|
Text: r.Resp,
|
||||||
|
LocalStamp: time.Now().Unix(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RobotTaskWithKeyworkds) RunTime() time.Time {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RobotTaskWithKeyworkds) SetResponse(response string) {
|
||||||
|
r.Resp = response
|
||||||
|
}
|
86
pkg/service/asChat/robot/ruler_replyWhenUserJoinSession.go
Normal file
86
pkg/service/asChat/robot/ruler_replyWhenUserJoinSession.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package robot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"fonchain-fiee/api/accountFiee"
|
||||||
|
"fonchain-fiee/pkg/common/ws"
|
||||||
|
"fonchain-fiee/pkg/service"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/chatCache"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/dto"
|
||||||
|
"fonchain-fiee/pkg/service/asChat/logic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewReplyWhenUserJoinSession() IRobotTask {
|
||||||
|
return &ReplyWhenUserJoinSession{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReplyWhenUserJoinSession struct {
|
||||||
|
Response string
|
||||||
|
Receiver *accountFiee.ChatUserData
|
||||||
|
Sender *accountFiee.ChatUserData
|
||||||
|
Msg string
|
||||||
|
Resp string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ReplyWhenUserJoinSession) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool) {
|
||||||
|
if event.EventType != ws.EventUserJoin {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if event.Client == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
queryRes, err := service.AccountFieeProvider.GetChatRecordList(ctx, &accountFiee.GetChatRecordListRequest{
|
||||||
|
Query: &accountFiee.ChatRecordData{
|
||||||
|
SessionId: event.Client.SessionId,
|
||||||
|
},
|
||||||
|
Page: 1,
|
||||||
|
PageSize: 1,
|
||||||
|
Order: "created_at desc",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//如果最近一次的消息也是机器人发送的,就不再发送了
|
||||||
|
for i, v := range queryRes.List {
|
||||||
|
if i == 0 {
|
||||||
|
if v.UserId == sender.ID {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hit = true
|
||||||
|
r.Sender = sender
|
||||||
|
r.Receiver = event.ChatUser
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ReplyWhenUserJoinSession) Run(cache *chatCache.ChatCache) (err error) {
|
||||||
|
clientSessionId := fmt.Sprintf("%d", r.Receiver.ID)
|
||||||
|
err = logic.NewMessage(context.Background(), cache, r.Sender, dto.NewMessageRequest{
|
||||||
|
Waiter: true,
|
||||||
|
Robot: true,
|
||||||
|
AtUserId: r.Receiver.ID,
|
||||||
|
SessionId: clientSessionId,
|
||||||
|
Message: dto.Message{
|
||||||
|
MsgType: 1,
|
||||||
|
Text: r.Resp,
|
||||||
|
LocalStamp: time.Now().Unix(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ReplyWhenUserJoinSession) RunTime() time.Time {
|
||||||
|
//TODO implement me
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ReplyWhenUserJoinSession) SetResponse(response string) {
|
||||||
|
//TODO implement me
|
||||||
|
panic("implement me")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user