feat: updated filters, implemented fluent api for message filter
This commit is contained in:
1 parent
bec2cfd017
commit
fbf716c7bc
2 files changed
+63
-51
No files matched your search
@@ -7,8 +7,6 @@ import (
|
|||||||
|
|
||||||
type Middleware func(next HandlerFunc) HandlerFunc
|
type Middleware func(next HandlerFunc) HandlerFunc
|
||||||
|
|
||||||
type Filter func(c *mcontext.Context) bool
|
|
||||||
|
|
||||||
type route struct {
|
type route struct {
|
||||||
h UpdateHandler
|
h UpdateHandler
|
||||||
filters []func(c *mcontext.Context) bool
|
filters []func(c *mcontext.Context) bool
|
||||||
|
|||||||
+63
-49
@@ -2,16 +2,43 @@ package filters
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
mcontext "github.com/abelith/gomax/context"
|
mcontext "github.com/abelith/gomax/context"
|
||||||
"github.com/abelith/gomax/core"
|
|
||||||
"github.com/abelith/gomax/state"
|
"github.com/abelith/gomax/state"
|
||||||
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"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 {
|
return func(c *mcontext.Context) bool {
|
||||||
upd := c.Update()
|
upd := c.Update()
|
||||||
if upd.UpdateType == model.UpdateBotStarted {
|
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 {
|
return func(c *mcontext.Context) bool {
|
||||||
upd := c.Update()
|
upd := c.Update()
|
||||||
return upd.UpdateType == model.UpdateMessageCreated && strings.HasPrefix(upd.GetCommand().Command, cmd)
|
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 func(c *mcontext.Context) bool {
|
||||||
return c.State().State() == state
|
return c.State().State() == state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageText(s string) core.Filter {
|
func BotStopped() Filter {
|
||||||
return func(c *mcontext.Context) bool {
|
return func(c *mcontext.Context) bool {
|
||||||
if msg := c.Update().Message; msg != nil {
|
return c.Update().UpdateType == model.UpdateBotStopped
|
||||||
return s == msg.Body.Text
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageTextRe(re *regexp.Regexp) core.Filter {
|
func Message() MessageFilter {
|
||||||
return func(c *mcontext.Context) bool {
|
return func(c *mcontext.Context) bool {
|
||||||
if msg := c.Update().Message; msg != nil {
|
return c.Update().UpdateType == model.UpdateMessageCreated && c.Update().Message != nil
|
||||||
s := strings.TrimSpace(msg.Body.Text)
|
|
||||||
return re.MatchString(s)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageGeo() core.Filter {
|
func (mf MessageFilter) Text(s string) MessageFilter {
|
||||||
return func(c *mcontext.Context) bool {
|
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
|
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 {
|
func Callback() CallbackFilter {
|
||||||
return func(c *mcontext.Context) bool {
|
return func(c *mcontext.Context) bool {
|
||||||
return c.Update().UpdateType == model.UpdateMessageCallback
|
return c.Update().UpdateType == model.UpdateMessageCallback
|
||||||
@@ -104,15 +121,12 @@ func (cf CallbackFilter) StartsWith(prefix string) CallbackFilter {
|
|||||||
if !cf(c) {
|
if !cf(c) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
payload, ok := getCallbackPayload(c)
|
|
||||||
return ok && strings.HasPrefix(payload, prefix)
|
upd := c.Update().Callback
|
||||||
}
|
if upd == nil {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCallbackPayload(c *mcontext.Context) (string, bool) {
|
return strings.HasPrefix(upd.Payload, prefix)
|
||||||
upd := c.Update()
|
|
||||||
if upd.Callback == nil {
|
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
return upd.Callback.Payload, true
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user