fonchain-fiee/pkg/service/asChat/robot/ruler_replyWhenUserJoinSession.go

86 lines
1.9 KiB
Go
Raw Normal View History

2025-06-18 11:47:43 +00:00
package robot
import (
"context"
"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"
"strconv"
2025-06-18 11:47:43 +00:00
"time"
)
func NewReplyWhenUserJoinSession() IRobotTask {
return &ReplyWhenUserJoinSession{}
}
type ReplyWhenUserJoinSession struct {
Response string
Sender *accountFiee.ChatUserData
Msg string
Resp string
sessionId string
atUserId int
2025-06-18 11:47:43 +00:00
}
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.sessionId = event.Client.SessionId
r.atUserId, _ = strconv.Atoi(event.Client.SessionId)
2025-06-18 11:47:43 +00:00
return
}
func (r ReplyWhenUserJoinSession) Run(cache *chatCache.ChatCache) (err error) {
err = logic.NewMessage(context.Background(), cache, r.Sender, dto.NewMessageRequest{
Waiter: true,
Robot: true,
AtUserId: int64(r.atUserId),
SessionId: r.sessionId,
2025-06-18 11:47:43 +00:00
Message: dto.Message{
MsgType: 1,
Text: r.Resp,
LocalStamp: time.Now().Unix(),
},
})
return
}
func (r *ReplyWhenUserJoinSession) RunTime() time.Time {
return time.Time{}
2025-06-18 11:47:43 +00:00
}
func (r *ReplyWhenUserJoinSession) SetResponse(response string) {
r.Resp = response
2025-06-18 11:47:43 +00:00
}