From fbf716c7bcbb7a97d22042e128d245aafa01ea1c Mon Sep 17 00:00:00 2001 From: Kirillkriv Date: Sat, 3 Oct 2026 13:27:57 +0300 Subject: [PATCH] feat: updated filters, implemented fluent api for message filter --- core/router.go | 2 - filters/filters.go | 114 +++++++++++++++++++++++++-------------------- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/core/router.go b/core/router.go index 9194287..4c3147a 100644 --- a/core/router.go +++ b/core/router.go @@ -7,8 +7,6 @@ import ( type Middleware func(next HandlerFunc) HandlerFunc -type Filter func(c *mcontext.Context) bool - type route struct { h UpdateHandler filters []func(c *mcontext.Context) bool diff --git a/filters/filters.go b/filters/filters.go index 5cc490d..ee7af05 100644 --- a/filters/filters.go +++ b/filters/filters.go @@ -2,16 +2,43 @@ package filters import ( mcontext "github.com/abelith/gomax/context" - "github.com/abelith/gomax/core" "github.com/abelith/gomax/state" "github.com/max-messenger/max-bot-api-client-go/v2/model" "regexp" "strings" ) -type CallbackFilter core.Filter +type Filter func(c *mcontext.Context) bool -func StartCommand() core.Filter { +type CallbackFilter Filter + +type MessageFilter Filter + +func And(filters ...func(c *mcontext.Context) bool) func(c *mcontext.Context) bool { + return func(c *mcontext.Context) bool { + for _, filter := range filters { + if !filter(c) { + return false + } + } + + return true + } +} + +func Or(filters ...func(c *mcontext.Context) bool) func(c *mcontext.Context) bool { + return func(c *mcontext.Context) bool { + for _, filter := range filters { + if filter(c) { + return true + } + } + + return false + } +} + +func StartCommand() Filter { return func(c *mcontext.Context) bool { upd := c.Update() if upd.UpdateType == model.UpdateBotStarted { @@ -22,41 +49,54 @@ func StartCommand() core.Filter { } } -func Command(cmd string) core.Filter { +func Command(cmd string) Filter { return func(c *mcontext.Context) bool { upd := c.Update() return upd.UpdateType == model.UpdateMessageCreated && strings.HasPrefix(upd.GetCommand().Command, cmd) } } -func State(state state.State) core.Filter { +func State(state state.State) Filter { return func(c *mcontext.Context) bool { return c.State().State() == state } } -func MessageText(s string) core.Filter { +func BotStopped() Filter { return func(c *mcontext.Context) bool { - if msg := c.Update().Message; msg != nil { - return s == msg.Body.Text - } - return false + return c.Update().UpdateType == model.UpdateBotStopped } } -func MessageTextRe(re *regexp.Regexp) core.Filter { +func Message() MessageFilter { return func(c *mcontext.Context) bool { - if msg := c.Update().Message; msg != nil { - s := strings.TrimSpace(msg.Body.Text) - return re.MatchString(s) - } - return false + return c.Update().UpdateType == model.UpdateMessageCreated && c.Update().Message != nil } } -func MessageGeo() core.Filter { +func (mf MessageFilter) Text(s string) MessageFilter { return func(c *mcontext.Context) bool { - if c.Update().Message == nil { + if !mf(c) { + return false + } + + return c.Update().Message.Body.Text == s + } +} + +func (mf MessageFilter) Regex(re *regexp.Regexp) MessageFilter { + return func(c *mcontext.Context) bool { + if !mf(c) { + return false + } + + return re.MatchString(c.Update().Message.Body.Text) + } +} + +func (mf MessageFilter) Geo() MessageFilter { + return func(c *mcontext.Context) bool { + if !mf(c) { return false } @@ -70,29 +110,6 @@ func MessageGeo() core.Filter { } } -func BotStopped() core.Filter { - return func(c *mcontext.Context) bool { - return c.Update().UpdateType == model.UpdateBotStopped - } -} - -func Or(filters ...core.Filter) core.Filter { - return func(c *mcontext.Context) bool { - for _, filter := range filters { - if filter(c) { - return true - } - } - return false - } -} - -func Message() core.Filter { - return func(c *mcontext.Context) bool { - return c.Update().UpdateType == model.UpdateMessageCreated && c.Update().Message != nil - } -} - func Callback() CallbackFilter { return func(c *mcontext.Context) bool { return c.Update().UpdateType == model.UpdateMessageCallback @@ -104,15 +121,12 @@ func (cf CallbackFilter) StartsWith(prefix string) CallbackFilter { if !cf(c) { return false } - payload, ok := getCallbackPayload(c) - return ok && strings.HasPrefix(payload, prefix) - } -} -func getCallbackPayload(c *mcontext.Context) (string, bool) { - upd := c.Update() - if upd.Callback == nil { - return "", false + upd := c.Update().Callback + if upd == nil { + return false + } + + return strings.HasPrefix(upd.Payload, prefix) } - return upd.Callback.Payload, true }