68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
|
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
|
||
|
}
|