Middleware
This commit is contained in:
parent
b074189d31
commit
599584ea58
24
bot.go
24
bot.go
|
@ -10,12 +10,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type bot struct {
|
type bot struct {
|
||||||
chatID int64
|
|
||||||
me *echo.User
|
|
||||||
echo.API
|
echo.API
|
||||||
data StateData
|
chatID int64
|
||||||
lock sync.Mutex
|
me *echo.User
|
||||||
state Runnable
|
data StateData
|
||||||
|
lock sync.Mutex
|
||||||
|
state Runnable
|
||||||
|
handler UpdateHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultHandler(u *Update) error {
|
||||||
|
err := u.Bot.state.Call(u)
|
||||||
|
if err != nil {
|
||||||
|
u.LogError("", err, true)
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bot) Update(u *echo.Update) {
|
func (b *bot) Update(u *echo.Update) {
|
||||||
|
@ -30,8 +39,5 @@ func (b *bot) Update(u *echo.Update) {
|
||||||
|
|
||||||
b.lock.Lock()
|
b.lock.Lock()
|
||||||
defer b.lock.Unlock()
|
defer b.lock.Unlock()
|
||||||
err := b.state.Call(upd)
|
b.handler(upd)
|
||||||
if err != nil {
|
|
||||||
upd.LogError("", err, true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
17
command.go
17
command.go
|
@ -1,16 +1,21 @@
|
||||||
package nechotron
|
package nechotron
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Body string
|
Body string
|
||||||
IsAdminOnly bool
|
IsAdminOnly bool
|
||||||
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommand(body string, isAdminOnly bool) *Command {
|
func NewCommand(body string, desc string, isAdminOnly bool) *Command {
|
||||||
return &Command{
|
return &Command{
|
||||||
Body: body,
|
Body: body,
|
||||||
IsAdminOnly: isAdminOnly,
|
IsAdminOnly: isAdminOnly,
|
||||||
|
Description: desc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (c *Command) String() string {
|
func (c *Command) String() string {
|
||||||
|
@ -21,4 +26,12 @@ func (c *Command) Param(text string) string {
|
||||||
return strings.TrimPrefix(text, "/"+c.Body+" ")
|
return strings.TrimPrefix(text, "/"+c.Body+" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeCommandList(commands []*Command, format string) string {
|
||||||
|
result := ""
|
||||||
|
for _, command := range commands {
|
||||||
|
result += fmt.Sprintf(format, command.String(), command.Description)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// func HandleCommand(command *Command, handler cmdFunc) (bool, error)
|
// func HandleCommand(command *Command, handler cmdFunc) (bool, error)
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type UpdateHandler func(u *Update) error
|
||||||
type dispatchHandler func(u *Update) (bool, error)
|
type dispatchHandler func(u *Update) (bool, error)
|
||||||
type updateHandler func(u *Update) error
|
|
||||||
type commandHandler func(u *UpdateCommand) error
|
type commandHandler func(u *UpdateCommand) error
|
||||||
|
|
||||||
type Dispatcher struct {
|
type Dispatcher struct {
|
||||||
|
@ -45,7 +45,7 @@ func (d *Dispatcher) HandleCommand(command *Command, handler commandHandler) *Di
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dispatcher) HandleFilter(filter FilterFn, handler updateHandler) *Dispatcher {
|
func (d *Dispatcher) HandleFilter(filter FilterFn, handler UpdateHandler) *Dispatcher {
|
||||||
newHandler := func(u *Update) (bool, error) {
|
newHandler := func(u *Update) (bool, error) {
|
||||||
if !filter(u) {
|
if !filter(u) {
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
package nechotron
|
||||||
|
|
||||||
|
type Middleware func(next UpdateHandler) UpdateHandler
|
18
nechotron.go
18
nechotron.go
|
@ -10,6 +10,7 @@ type Nechotron struct {
|
||||||
Token string
|
Token string
|
||||||
DefaultState Runnable
|
DefaultState Runnable
|
||||||
ApiServer string
|
ApiServer string
|
||||||
|
Handler UpdateHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTron(token string, defaultState *State) *Nechotron {
|
func NewTron(token string, defaultState *State) *Nechotron {
|
||||||
|
@ -20,6 +21,7 @@ func NewTron(token string, defaultState *State) *Nechotron {
|
||||||
return &Nechotron{
|
return &Nechotron{
|
||||||
Token: token,
|
Token: token,
|
||||||
DefaultState: state,
|
DefaultState: state,
|
||||||
|
Handler: DefaultHandler,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,11 +31,12 @@ func (n *Nechotron) newBot(chatID int64) echo.Bot {
|
||||||
me, _ := a.GetMe()
|
me, _ := a.GetMe()
|
||||||
// log.Println("New bot active: ", chatID, me.Result)
|
// log.Println("New bot active: ", chatID, me.Result)
|
||||||
b := &bot{
|
b := &bot{
|
||||||
chatID: chatID,
|
chatID: chatID,
|
||||||
me: me.Result,
|
me: me.Result,
|
||||||
API: a,
|
API: a,
|
||||||
state: n.DefaultState,
|
state: n.DefaultState,
|
||||||
data: make(StateData),
|
data: make(StateData),
|
||||||
|
handler: n.Handler,
|
||||||
}
|
}
|
||||||
b.state = n.DefaultState
|
b.state = n.DefaultState
|
||||||
return b
|
return b
|
||||||
|
@ -44,3 +47,8 @@ func (n *Nechotron) DispatchPoll() error {
|
||||||
log.Println("Nechotron poll dispatcher started")
|
log.Println("Nechotron poll dispatcher started")
|
||||||
return dispatcher.Poll()
|
return dispatcher.Poll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Nechotron) Use(mid Middleware) *Nechotron {
|
||||||
|
n.Handler = mid(n.Handler)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue