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
|
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) {
|
newHandler := func(u *Update) (bool, error) {
|
||||||
if !u.IsCallback() {
|
if !u.IsCallback() {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
defer u.Bot.AnswerCallbackQuery(u.CallbackQuery.ID, &echotron.CallbackQueryOptions{})
|
defer u.Bot.AnswerCallbackQuery(u.CallbackQuery.ID, &echotron.CallbackQueryOptions{})
|
||||||
if u.CallbackQuery.Data != callback {
|
if !filter(u) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
err := handler(u)
|
err := handler(u)
|
||||||
|
|
|
@ -230,6 +230,13 @@ func (u *Update) Text() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *Update) Callback() string {
|
||||||
|
if u.IsCallback() {
|
||||||
|
return u.CallbackQuery.Data
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (u *Update) Entities() []*echotron.MessageEntity {
|
func (u *Update) Entities() []*echotron.MessageEntity {
|
||||||
if u.IsText() {
|
if u.IsText() {
|
||||||
return u.Message.Entities
|
return u.Message.Entities
|
||||||
|
|
Loading…
Reference in New Issue