Callbacks
This commit is contained in:
parent
f4346d8859
commit
01d04199fd
|
@ -0,0 +1,25 @@
|
|||
package nechotron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CallbackFilter func(u *Update) bool
|
||||
|
||||
func CallbackExact(text string) CallbackFilter {
|
||||
return func(u *Update) bool {
|
||||
data := u.Callback()
|
||||
return text == data
|
||||
}
|
||||
}
|
||||
|
||||
func CallbackPrefix(text string) CallbackFilter {
|
||||
return func(u *Update) bool {
|
||||
data := u.Callback()
|
||||
if strings.HasPrefix(data, text) {
|
||||
u.Ctx = context.WithValue(u.Ctx, FilteredValue("cb_"+text), strings.TrimPrefix(data, text+":"))
|
||||
}
|
||||
return strings.HasPrefix(data, text)
|
||||
}
|
||||
}
|
|
@ -54,13 +54,13 @@ func (d *Dispatcher) HandleFilter(filter FilterFn, handler UpdateHandler) *Dispa
|
|||
return d
|
||||
}
|
||||
|
||||
func (d *Dispatcher) HandleCallback(callback string, handler UpdateHandler) *Dispatcher {
|
||||
func (d *Dispatcher) HandleCallback(filter CallbackFilter, handler UpdateHandler) *Dispatcher {
|
||||
newHandler := func(u *Update) (bool, error) {
|
||||
if !u.IsCallback() {
|
||||
return false, nil
|
||||
}
|
||||
defer u.Bot.AnswerCallbackQuery(u.CallbackQuery.ID, &echotron.CallbackQueryOptions{})
|
||||
if u.CallbackQuery.Data != callback {
|
||||
if !filter(u) {
|
||||
return false, nil
|
||||
}
|
||||
err := handler(u)
|
||||
|
|
Loading…
Reference in New Issue