bbgo_origin/pkg/interact/interact.go

225 lines
4.9 KiB
Go
Raw Normal View History

2022-01-12 13:45:12 +00:00
package interact
2022-01-11 18:54:13 +00:00
import (
2022-01-13 14:15:05 +00:00
"context"
"fmt"
"time"
2022-01-11 18:54:13 +00:00
2022-01-13 14:15:05 +00:00
log "github.com/sirupsen/logrus"
2022-01-11 18:54:13 +00:00
)
2022-01-13 14:15:05 +00:00
type CustomInteraction interface {
Commands(interact *Interact)
}
2022-01-13 14:24:51 +00:00
type Messenger interface {
TextMessageResponder
CommandResponder
2022-01-13 17:58:04 +00:00
Start(ctx context.Context)
}
2022-01-11 18:54:13 +00:00
// Interact implements the interaction between bot and message software.
type Interact struct {
startTime time.Time
2022-01-13 15:41:22 +00:00
// commands is the default public command map
commands map[string]*Command
2022-01-13 15:41:22 +00:00
// privateCommands is the private command map, need auth
privateCommands map[string]*Command
2022-01-13 14:15:05 +00:00
states map[State]State
statesFunc map[State]interface{}
originState, currentState State
messenger Messenger
}
func New() *Interact {
return &Interact{
startTime: time.Now(),
2022-01-13 14:15:05 +00:00
commands: make(map[string]*Command),
originState: StatePublic,
currentState: StatePublic,
states: make(map[State]State),
statesFunc: make(map[State]interface{}),
}
}
func (it *Interact) SetOriginState(s State) {
it.originState = s
2022-01-13 14:15:05 +00:00
}
func (it *Interact) AddCustomInteraction(custom CustomInteraction) {
custom.Commands(it)
2022-01-13 14:15:05 +00:00
}
func (it *Interact) PrivateCommand(command string, f interface{}) *Command {
2022-01-13 18:57:39 +00:00
cmd := NewCommand(command, "", f)
it.privateCommands[command] = cmd
2022-01-13 15:41:22 +00:00
return cmd
}
2022-01-13 18:57:39 +00:00
func (it *Interact) Command(command string, desc string, f interface{}) *Command {
cmd := NewCommand(command, desc, f)
it.commands[command] = cmd
return cmd
}
func (it *Interact) getNextState(currentState State) (nextState State, final bool) {
var ok bool
2022-01-13 14:15:05 +00:00
final = false
nextState, ok = it.states[currentState]
if ok {
2022-01-13 14:15:05 +00:00
// check if it's the final state
if _, hasTransition := it.statesFunc[nextState]; !hasTransition {
2022-01-13 14:15:05 +00:00
final = true
}
return nextState, final
}
2022-01-13 14:15:05 +00:00
// state not found, return to the origin state
return it.originState, final
2022-01-11 18:54:13 +00:00
}
func (it *Interact) setState(s State) {
2022-01-13 17:58:04 +00:00
log.Infof("[interact] transiting state from %s -> %s", it.currentState, s)
it.currentState = s
2022-01-13 14:15:05 +00:00
}
func (it *Interact) handleResponse(text string, ctxObjects ...interface{}) error {
// we only need response when executing a command
switch it.currentState {
case StatePublic, StateAuthenticated:
return nil
}
args := parseCommand(text)
f, ok := it.statesFunc[it.currentState]
if !ok {
return fmt.Errorf("state function of %s is not defined", it.currentState)
}
_, err := parseFuncArgsAndCall(f, args, ctxObjects...)
if err != nil {
return err
}
nextState, end := it.getNextState(it.currentState)
if end {
it.setState(it.originState)
return nil
}
it.setState(nextState)
return nil
}
func (it *Interact) getCommand(command string) (*Command, error) {
switch it.currentState {
2022-01-13 16:17:41 +00:00
case StateAuthenticated:
if cmd, ok := it.privateCommands[command]; ok {
2022-01-13 16:17:41 +00:00
return cmd, nil
}
case StatePublic:
if _, ok := it.privateCommands[command]; ok {
2022-01-13 16:17:41 +00:00
return nil, fmt.Errorf("private command can not be executed in the public mode")
}
}
if cmd, ok := it.commands[command]; ok {
2022-01-13 16:17:41 +00:00
return cmd, nil
}
return nil, fmt.Errorf("command %s not found", command)
}
func (it *Interact) runCommand(command string, args []string, ctxObjects ...interface{}) error {
cmd, err := it.getCommand(command)
2022-01-13 16:17:41 +00:00
if err != nil {
return err
}
it.setState(cmd.initState)
if _, err := parseFuncArgsAndCall(cmd.F, args, ctxObjects...); err != nil {
return err
}
// if we can successfully execute the command, then we can go to the next state.
nextState, end := it.getNextState(it.currentState)
if end {
it.setState(it.originState)
return nil
}
it.setState(nextState)
return nil
2022-01-12 13:45:12 +00:00
}
2022-01-11 18:54:13 +00:00
func (it *Interact) SetMessenger(messenger Messenger) {
2022-01-13 17:58:04 +00:00
// pass Responder function
messenger.SetTextMessageResponder(func(message string, reply Reply, ctxObjects ...interface{}) error {
return it.handleResponse(message, append(ctxObjects, reply)...)
2022-01-13 14:24:51 +00:00
})
it.messenger = messenger
2022-01-13 14:15:05 +00:00
}
2022-01-13 16:17:41 +00:00
// builtin initializes the built-in commands
func (it *Interact) builtin() error {
2022-01-13 18:57:39 +00:00
it.Command("/uptime", "show bot uptime", func(reply Reply) error {
uptime := time.Since(it.startTime)
reply.Message(fmt.Sprintf("uptime %s", uptime))
2022-01-13 16:17:41 +00:00
return nil
})
return nil
}
func (it *Interact) init() error {
if err := it.builtin(); err != nil {
2022-01-13 16:17:41 +00:00
return err
}
for n, cmd := range it.commands {
for s1, s2 := range cmd.states {
if _, exist := it.states[s1]; exist {
return fmt.Errorf("state %s already exists", s1)
}
it.states[s1] = s2
}
for s, f := range cmd.statesFunc {
it.statesFunc[s] = f
}
2022-01-13 14:15:05 +00:00
// register commands to the service
if it.messenger == nil {
2022-01-13 14:15:05 +00:00
return fmt.Errorf("messenger is not set")
}
2022-01-13 16:17:41 +00:00
commandName := n
2022-01-13 18:57:39 +00:00
it.messenger.AddCommand(cmd, func(message string, reply Reply, ctxObjects ...interface{}) error {
2022-01-13 17:58:04 +00:00
args := parseCommand(message)
return it.runCommand(commandName, args, append(ctxObjects, reply)...)
2022-01-13 14:15:05 +00:00
})
}
return nil
2022-01-12 13:45:12 +00:00
}
func (it *Interact) Start(ctx context.Context) error {
if err := it.init(); err != nil {
2022-01-13 14:15:05 +00:00
return err
}
// TODO: use go routine and context
2022-01-13 17:58:04 +00:00
it.messenger.Start(ctx)
2022-01-13 14:15:05 +00:00
return nil
2022-01-11 18:54:13 +00:00
}