2025-06-14 06:46:01 +00:00
|
|
|
// Package robot -----------------------------
|
|
|
|
// @file : replyRuler.go
|
|
|
|
// @author : JJXu
|
|
|
|
// @contact : wavingbear@163.com
|
|
|
|
// @time : 2025/6/13 17:39
|
|
|
|
// -------------------------------------------
|
|
|
|
package robot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fonchain-fiee/api/accountFiee"
|
|
|
|
"fonchain-fiee/pkg/common/ws"
|
|
|
|
)
|
|
|
|
|
2025-06-16 06:02:16 +00:00
|
|
|
// 回复规则
|
2025-06-14 06:46:01 +00:00
|
|
|
type Reply struct {
|
2025-06-18 09:51:32 +00:00
|
|
|
Title string
|
2025-06-14 06:46:01 +00:00
|
|
|
Response string
|
2025-06-18 11:47:43 +00:00
|
|
|
Rules []IRobotTask
|
2025-06-14 06:46:01 +00:00
|
|
|
}
|
|
|
|
|
2025-06-18 11:47:43 +00:00
|
|
|
func (r *Reply) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, rule IRobotTask) {
|
|
|
|
for _, rule = range r.Rules {
|
|
|
|
hit = rule.Hit(event, robotInfo)
|
2025-06-15 08:01:55 +00:00
|
|
|
if hit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-06-18 11:47:43 +00:00
|
|
|
//
|
|
|
|
//// 规则接口
|
|
|
|
//type IRule interface {
|
|
|
|
// Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask)
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// KeywordsRuleChecker 关键字回复
|
|
|
|
//type ReplyWhenHitKeywords struct {
|
|
|
|
// Keywords []string `json:"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 {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// if event.ChatUser.Role != 1 {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// for _, v := range k.Keywords {
|
|
|
|
// if strings.Contains(event.Msg, v) {
|
|
|
|
// hit = true
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// atUserId := event.Client.UserId
|
|
|
|
// task = RobotTask{
|
|
|
|
// ChatUser: event.ChatUser,
|
|
|
|
// Run: func(msg string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
|
|
|
// return logic.NewMessage(context.Background(), cache, Sender, dto.NewMessageRequest{
|
|
|
|
// Waiter: true,
|
|
|
|
// Robot: true,
|
|
|
|
// AtUserId: atUserId,
|
|
|
|
// SessionId: event.Client.SessionId,
|
|
|
|
// Message: dto.Message{
|
|
|
|
// MsgType: 1,
|
|
|
|
// Text: msg,
|
|
|
|
// LocalStamp: time.Now().Unix(),
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// 用户打开聊天会话直接发送
|
|
|
|
//type ReplyWhenUserJoinSession struct {
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func NewReplyWhenUserJoinSession() IRule {
|
|
|
|
// return &ReplyWhenUserJoinSession{}
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (k ReplyWhenUserJoinSession) Hit(event ws.ListenEventData, robotInfo *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
|
|
|
// if event.EventType != ws.EventUserJoin {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// if event.Client == nil {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// clientSessionId := event.Client.SessionId
|
|
|
|
// atUserId := event.Client.UserId
|
|
|
|
// 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 == robotInfo.ID {
|
|
|
|
// return
|
|
|
|
// } else {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// hit = true
|
|
|
|
// if event.ChatUser == nil {
|
|
|
|
// event.ChatUser, err = service.AccountFieeProvider.GetChatUserDetail(context.Background(), &accountFiee.GetChatUserByIdRequest{Id: event.Client.UserId})
|
|
|
|
// if err != nil {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// task = RobotTask{
|
|
|
|
// ChatUser: event.ChatUser,
|
|
|
|
// Run: func(msg string, cache *chatCache.ChatCache, Sender *accountFiee.ChatUserData) error {
|
|
|
|
// return logic.NewMessage(ctx, cache, Sender, dto.NewMessageRequest{
|
|
|
|
// Waiter: true,
|
|
|
|
// Robot: true,
|
|
|
|
// AtUserId: atUserId,
|
|
|
|
// SessionId: clientSessionId,
|
|
|
|
// Message: dto.Message{
|
|
|
|
// MsgType: 1,
|
|
|
|
// Text: msg,
|
|
|
|
// LocalStamp: time.Now().Unix(),
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// //logicFunc = func(msg string, cache *chatCache.ChatCache, chatUser *accountFiee.ChatUserData) error {
|
|
|
|
// // //var notice = dto.MessageListType{}
|
|
|
|
// // //newRecord := &accountFiee.ChatRecordData{
|
|
|
|
// // // SessionId: wsClient.SessionId,
|
|
|
|
// // // UserId: wsClient.UserId,
|
|
|
|
// // // Name: wsClient.SessionId,
|
|
|
|
// // // Avatar: robotInfo.Avatar,
|
|
|
|
// // // MsgType: 1,
|
|
|
|
// // // Content: msg,
|
|
|
|
// // //}
|
|
|
|
// // //notice.BuildMessage(newRecord)
|
|
|
|
// // //_, 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{
|
|
|
|
// // MsgType: 1,
|
|
|
|
// // Text: msg,
|
|
|
|
// // LocalStamp: time.Now().Unix(),
|
|
|
|
// // },
|
|
|
|
// // })
|
|
|
|
// // return err
|
|
|
|
// //}
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// 客服指定时间不回复则自动回复
|
|
|
|
//
|
|
|
|
//type ReplyWhenWaiterNoAction struct {
|
|
|
|
// DelaySecond time.Duration
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
////func NewReplyWhenWaiterNoAction(delaySecond time.Duration) *ReplyWhenWaiterNoAction {
|
|
|
|
//// return &ReplyWhenWaiterNoAction{
|
|
|
|
//// DelaySecond: delaySecond,
|
|
|
|
//// }
|
|
|
|
////}
|
|
|
|
//
|
|
|
|
//func (k *ReplyWhenWaiterNoAction) Hit(event ws.ListenEventData, sender *accountFiee.ChatUserData) (hit bool, task RobotTask) {
|
|
|
|
// if event.Client == nil || event.EventType != ws.EventChatMessage {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// //客服和机器人的的消息不需要处理
|
|
|
|
// if event.ChatUser.Role != 1 {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// hit = true // 立即保存SessionId的值
|
|
|
|
//
|
|
|
|
// clientSessionId := event.Client.SessionId
|
|
|
|
// atUserId := event.Client.UserId
|
|
|
|
// 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 {
|
|
|
|
// // 记录闭包执行时的Client状态
|
|
|
|
// fmt.Printf("闭包执行: clientSessionId=%s\n", clientSessionId)
|
|
|
|
//
|
|
|
|
// //如果客服已经回复则不发送消息
|
|
|
|
// chatRecordListRes, err := service.AccountFieeProvider.GetChatRecordList(context.Background(), &accountFiee.GetChatRecordListRequest{
|
|
|
|
// Query: &accountFiee.ChatRecordData{
|
|
|
|
// SessionId: event.Client.SessionId,
|
|
|
|
// },
|
|
|
|
// 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
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// //var notice = dto.MessageListType{}
|
|
|
|
// //newRecord := &accountFiee.ChatRecordData{
|
|
|
|
// // SessionId: wsClient.SessionId,
|
|
|
|
// // UserId: wsClient.UserId,
|
|
|
|
// // Name: chatUser.NickName,
|
|
|
|
// // Avatar: robotInfo.Avatar,
|
|
|
|
// // MsgType: 1,
|
|
|
|
// // Content: content,
|
|
|
|
// //}
|
|
|
|
// //notice.BuildMessage(newRecord)
|
|
|
|
// //_, err = consts.ChatRoom.SendSessionMessage(robotInfo, wsClient.SessionId, ws.NewChatMsgType, notice)
|
|
|
|
// //return err
|
|
|
|
// fmt.Println("延时回复 sessionID:", clientSessionId)
|
|
|
|
// err = logic.NewMessage(context.Background(), cache, sender, dto.NewMessageRequest{
|
|
|
|
// Waiter: true,
|
|
|
|
// Robot: true,
|
|
|
|
// AtUserId: atUserId,
|
|
|
|
// SessionId: clientSessionId,
|
|
|
|
// Message: dto.Message{
|
|
|
|
// MsgType: 1,
|
|
|
|
// Text: content,
|
|
|
|
// LocalStamp: time.Now().Unix(),
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// return err
|
|
|
|
// },
|
|
|
|
// Response: "",
|
|
|
|
// ChatUser: event.ChatUser,
|
|
|
|
// }
|
|
|
|
// return
|
|
|
|
//
|
|
|
|
//}
|