feat: updated filters, implemented fluent api for message filter
This commit is contained in:
1 parent
bec2cfd017
commit
fbf716c7bc
2 files changed
+64
-52
No files matched your search
@@ -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
|
||||
|
||||
+64
-50
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user