nechotron/state.go

56 lines
825 B
Go
Raw Normal View History

2023-01-19 23:08:59 +03:00
package nechotron
import (
2023-01-20 00:27:29 +03:00
"errors"
2023-01-19 23:08:59 +03:00
"github.com/NicoNex/echotron/v3"
)
2023-01-20 00:27:29 +03:00
type StateData map[string]interface{}
type Runnable interface {
Call(*Update) error
2023-01-20 00:27:29 +03:00
}
2023-01-19 23:08:59 +03:00
type State struct {
2023-01-20 16:32:13 +03:00
Fn stateFn
2023-01-20 00:27:29 +03:00
}
func (s *State) Call(u *Update) error {
2023-01-20 16:32:13 +03:00
return s.Fn(u)
2023-01-19 23:08:59 +03:00
}
type stateFn func(*Update) error
2023-01-19 23:08:59 +03:00
2023-01-20 00:27:29 +03:00
func (d StateData) Set(name string, data interface{}) {
d[name] = data
}
func (d StateData) Get(name string) (interface{}, error) {
data, ok := d[name]
if !ok {
return nil, errors.New("can't get data from statedata")
}
return data, nil
}
func (d StateData) Has(name string) bool {
_, ok := d[name]
return ok
}
func (d StateData) Clear() {
for k := range d {
delete(d, k)
}
}
2023-01-19 23:08:59 +03:00
var EchoState = State{
2023-01-20 16:32:13 +03:00
Fn: EchoFunc,
2023-01-19 23:08:59 +03:00
}
func EchoFunc(u *Update) error {
2023-01-19 23:08:59 +03:00
u.AnswerText(u.Text(), &echotron.MessageOptions{})
return nil
2023-01-19 23:08:59 +03:00
}