Middleware

This commit is contained in:
nefrace 2023-01-24 00:37:08 +03:00
parent b074189d31
commit 599584ea58
6 changed files with 49 additions and 19 deletions

16
bot.go
View File

@ -10,12 +10,21 @@ import (
) )
type bot struct { type bot struct {
echo.API
chatID int64 chatID int64
me *echo.User me *echo.User
echo.API
data StateData data StateData
lock sync.Mutex lock sync.Mutex
state Runnable 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)
}
} }

View File

@ -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)

View File

@ -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

3
middleware.go Normal file
View File

@ -0,0 +1,3 @@
package nechotron
type Middleware func(next UpdateHandler) UpdateHandler

View File

@ -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,
} }
} }
@ -34,6 +36,7 @@ func (n *Nechotron) newBot(chatID int64) echo.Bot {
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
}

View File

@ -14,7 +14,7 @@ type U echotron.Update
var emptyOpts = echotron.MessageOptions{} var emptyOpts = echotron.MessageOptions{}
type Event interface { type Event interface {
Update Upd() *echotron.Update
} }
type Update struct { type Update struct {