Types of callbacks
This commit is contained in:
parent
9298c6402b
commit
b04a779236
|
@ -7,6 +7,9 @@ import (
|
|||
)
|
||||
|
||||
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 Dispatcher struct {
|
||||
|
@ -27,7 +30,12 @@ func (d *Dispatcher) Run(u *Update) error {
|
|||
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) {
|
||||
if !strings.HasPrefix(u.Text(), command.String()) {
|
||||
return false, nil
|
||||
|
@ -35,7 +43,7 @@ func (d *Dispatcher) HandleCommand(command *Command, handler UpdateHandler) *Dis
|
|||
if command.IsAdminOnly && !IsUserAdmin(u) && u.ChatID() < 0 {
|
||||
return false, nil
|
||||
}
|
||||
err := handler(u)
|
||||
err := handler(u, command.Param(u.Text()))
|
||||
return true, err
|
||||
}
|
||||
d.handlers = append(d.handlers, newHandler)
|
||||
|
@ -85,7 +93,8 @@ func (d *Dispatcher) HandleReply(handler UpdateHandler) *Dispatcher {
|
|||
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 {
|
||||
if err := d.Run(u); err != nil {
|
||||
return err
|
||||
|
|
|
@ -55,6 +55,10 @@ func IsReply(u *Update) bool {
|
|||
return u.IsReply()
|
||||
}
|
||||
|
||||
func IsCallback(u *Update) bool {
|
||||
return u.IsCallback()
|
||||
}
|
||||
|
||||
func IsUserAdmin(u *Update) bool {
|
||||
if IsPrivate(u) {
|
||||
return true
|
||||
|
|
Loading…
Reference in New Issue