Added state interface for customizing
This commit is contained in:
parent
1a8decf4d4
commit
3a06ae5f0e
2
bot.go
2
bot.go
|
@ -15,7 +15,7 @@ type bot struct {
|
||||||
echo.API
|
echo.API
|
||||||
data StateData
|
data StateData
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
state *State
|
state Runnable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bot) Update(u *echo.Update) {
|
func (b *bot) Update(u *echo.Update) {
|
||||||
|
|
8
state.go
8
state.go
|
@ -9,18 +9,18 @@ import (
|
||||||
type StateData map[string]interface{}
|
type StateData map[string]interface{}
|
||||||
|
|
||||||
type Runnable interface {
|
type Runnable interface {
|
||||||
Call(*Update) (*State, error)
|
Call(*Update) (Runnable, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
fn stateFn
|
fn stateFn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) Call(u *Update) (*State, error) {
|
func (s *State) Call(u *Update) (Runnable, error) {
|
||||||
return s.fn(u)
|
return s.fn(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
type stateFn func(*Update) (*State, error)
|
type stateFn func(*Update) (Runnable, error)
|
||||||
|
|
||||||
func (d StateData) Set(name string, data interface{}) {
|
func (d StateData) Set(name string, data interface{}) {
|
||||||
d[name] = data
|
d[name] = data
|
||||||
|
@ -49,7 +49,7 @@ var EchoState = State{
|
||||||
fn: EchoFunc,
|
fn: EchoFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
func EchoFunc(u *Update) (*State, error) {
|
func EchoFunc(u *Update) (Runnable, error) {
|
||||||
u.AnswerText(u.Text(), &echotron.MessageOptions{})
|
u.AnswerText(u.Text(), &echotron.MessageOptions{})
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue