Moved logging away for handling extarnally

This commit is contained in:
nefrace 2023-01-24 01:08:24 +03:00
parent 599584ea58
commit d797a52029
3 changed files with 39 additions and 3 deletions

3
bot.go
View File

@ -21,9 +21,6 @@ type bot struct {
func DefaultHandler(u *Update) error { func DefaultHandler(u *Update) error {
err := u.Bot.state.Call(u) err := u.Bot.state.Call(u)
if err != nil {
u.LogError("", err, true)
}
return err return err
} }

View File

@ -56,3 +56,18 @@ func (d *Dispatcher) HandleFilter(filter FilterFn, handler UpdateHandler) *Dispa
d.handlers = append(d.handlers, newHandler) d.handlers = append(d.handlers, newHandler)
return d return d
} }
func (d *Dispatcher) HandleReply(handler UpdateHandler) *Dispatcher {
newHandler := func(u *Update) (bool, error) {
if !u.IsMessage() {
return false, nil
}
if u.Message.ReplyToMessage == nil {
return false, nil
}
err := handler(u)
return true, err
}
d.handlers = append(d.handlers, newHandler)
return d
}

View File

@ -10,8 +10,32 @@ func TextStartsWith(text string) FilterFn {
} }
} }
func TextStartsWithAny(subs ...string) FilterFn {
return func(u *Update) bool {
text := u.Text()
for _, sub := range subs {
if strings.HasPrefix(text, sub) {
return true
}
}
return false
}
}
func TextHas(text string) FilterFn { func TextHas(text string) FilterFn {
return func(u *Update) bool { return func(u *Update) bool {
return strings.Contains(u.Text(), text) return strings.Contains(u.Text(), text)
} }
} }
func TextHasAny(subs ...string) FilterFn {
return func(u *Update) bool {
text := u.Text()
for _, sub := range subs {
if strings.Contains(text, sub) {
return true
}
}
return false
}
}