Types of callbacks
This commit is contained in:
parent
9298c6402b
commit
b04a779236
|
@ -7,6 +7,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateHandler func(u *Update) error
|
type UpdateHandler func(u *Update) error
|
||||||
|
type CommandHandler func(u *Update, arg string) error
|
||||||
|
type CallbackHandler func(u *Update, data string) error
|
||||||
|
|
||||||
type dispatchHandler func(u *Update) (bool, error)
|
type dispatchHandler func(u *Update) (bool, error)
|
||||||
|
|
||||||
type Dispatcher struct {
|
type Dispatcher struct {
|
||||||
|
@ -27,7 +30,12 @@ func (d *Dispatcher) Run(u *Update) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dispatcher) HandleCommand(command *Command, handler UpdateHandler) *Dispatcher {
|
func (d *Dispatcher) Handle(handler func(u *Update) (bool, error)) *Dispatcher {
|
||||||
|
d.handlers = append(d.handlers, handler)
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Dispatcher) HandleCommand(command *Command, handler CommandHandler) *Dispatcher {
|
||||||
newHandler := func(u *Update) (bool, error) {
|
newHandler := func(u *Update) (bool, error) {
|
||||||
if !strings.HasPrefix(u.Text(), command.String()) {
|
if !strings.HasPrefix(u.Text(), command.String()) {
|
||||||
return false, nil
|
return false, nil
|
||||||
|
@ -35,7 +43,7 @@ func (d *Dispatcher) HandleCommand(command *Command, handler UpdateHandler) *Dis
|
||||||
if command.IsAdminOnly && !IsUserAdmin(u) && u.ChatID() < 0 {
|
if command.IsAdminOnly && !IsUserAdmin(u) && u.ChatID() < 0 {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
err := handler(u)
|
err := handler(u, command.Param(u.Text()))
|
||||||
return true, err
|
return true, err
|
||||||
}
|
}
|
||||||
d.handlers = append(d.handlers, newHandler)
|
d.handlers = append(d.handlers, newHandler)
|
||||||
|
@ -85,7 +93,8 @@ func (d *Dispatcher) HandleReply(handler UpdateHandler) *Dispatcher {
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func ChainRun(u *Update, disps ...*Dispatcher) error {
|
// Runs each dispatcher or handled listed here until error is returned from any
|
||||||
|
func RunEach(u *Update, disps ...*Dispatcher) error {
|
||||||
for _, d := range disps {
|
for _, d := range disps {
|
||||||
if err := d.Run(u); err != nil {
|
if err := d.Run(u); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -55,6 +55,10 @@ func IsReply(u *Update) bool {
|
||||||
return u.IsReply()
|
return u.IsReply()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsCallback(u *Update) bool {
|
||||||
|
return u.IsCallback()
|
||||||
|
}
|
||||||
|
|
||||||
func IsUserAdmin(u *Update) bool {
|
func IsUserAdmin(u *Update) bool {
|
||||||
if IsPrivate(u) {
|
if IsPrivate(u) {
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Reference in New Issue