initial commit

This commit is contained in:
abelith committed 2026-10-02 00:48:55 +03:00
commit bec2cfd017
18 files changed
+781

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
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
func StartCommand() core.Filter {
return func(c *mcontext.Context) bool {
upd := c.Update()
if upd.UpdateType == model.UpdateBotStarted {
return true
}
return strings.HasPrefix(upd.GetCommand().Command, "/start")
}
}
func Command(cmd string) core.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 {
return func(c *mcontext.Context) bool {
return c.State().State() == state
}
}
func MessageText(s string) core.Filter {
return func(c *mcontext.Context) bool {
if msg := c.Update().Message; msg != nil {
return s == msg.Body.Text
}
return false
}
}
func MessageTextRe(re *regexp.Regexp) core.Filter {
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
}
}
func MessageGeo() core.Filter {
return func(c *mcontext.Context) bool {
if c.Update().Message == nil {
return false
}
for _, attachment := range c.Update().Message.Body.Attachments {
if attachment.Type == model.AttachLocation {
return true
}
}
return false
}
}
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
}
}
func (cf CallbackFilter) StartsWith(prefix string) CallbackFilter {
return func(c *mcontext.Context) bool {
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
}
return upd.Callback.Payload, true
}