Added state interface for customizing

This commit is contained in:
nefrace 2023-01-20 01:44:35 +03:00
parent 1a8decf4d4
commit 3a06ae5f0e
2 changed files with 5 additions and 5 deletions

2
bot.go
View File

@ -15,7 +15,7 @@ type bot struct {
echo.API
data StateData
lock sync.Mutex
state *State
state Runnable
}
func (b *bot) Update(u *echo.Update) {

View File

@ -9,18 +9,18 @@ import (
type StateData map[string]interface{}
type Runnable interface {
Call(*Update) (*State, error)
Call(*Update) (Runnable, error)
}
type State struct {
fn stateFn
}
func (s *State) Call(u *Update) (*State, error) {
func (s *State) Call(u *Update) (Runnable, error) {
return s.fn(u)
}
type stateFn func(*Update) (*State, error)
type stateFn func(*Update) (Runnable, error)
func (d StateData) Set(name string, data interface{}) {
d[name] = data
@ -49,7 +49,7 @@ var EchoState = State{
fn: EchoFunc,
}
func EchoFunc(u *Update) (*State, error) {
func EchoFunc(u *Update) (Runnable, error) {
u.AnswerText(u.Text(), &echotron.MessageOptions{})
return nil, nil
}