2023-01-22 23:54:54 +03:00
|
|
|
package nechotron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2023-04-13 11:14:37 +03:00
|
|
|
|
|
|
|
"github.com/NicoNex/echotron/v3"
|
2023-01-22 23:54:54 +03:00
|
|
|
)
|
|
|
|
|
2023-01-24 00:37:08 +03:00
|
|
|
type UpdateHandler func(u *Update) error
|
2023-01-22 23:54:54 +03:00
|
|
|
type dispatchHandler func(u *Update) (bool, error)
|
|
|
|
|
|
|
|
type Dispatcher struct {
|
|
|
|
handlers []dispatchHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDispatcher() *Dispatcher {
|
|
|
|
return &Dispatcher{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dispatcher) Run(u *Update) error {
|
|
|
|
for _, h := range d.handlers {
|
|
|
|
executed, err := h(u)
|
|
|
|
if executed {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-24 01:12:19 +03:00
|
|
|
func (d *Dispatcher) HandleCommand(command *Command, handler UpdateHandler) *Dispatcher {
|
2023-01-22 23:54:54 +03:00
|
|
|
newHandler := func(u *Update) (bool, error) {
|
|
|
|
if !strings.HasPrefix(u.Text(), command.String()) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2023-01-24 02:25:08 +03:00
|
|
|
if command.IsAdminOnly && !IsUserAdmin(u) && u.ChatID() < 0 {
|
2023-01-22 23:54:54 +03:00
|
|
|
return false, nil
|
|
|
|
}
|
2023-01-24 01:12:19 +03:00
|
|
|
err := handler(u)
|
2023-01-22 23:54:54 +03:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
d.handlers = append(d.handlers, newHandler)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2023-01-24 00:37:08 +03:00
|
|
|
func (d *Dispatcher) HandleFilter(filter FilterFn, handler UpdateHandler) *Dispatcher {
|
2023-01-22 23:54:54 +03:00
|
|
|
newHandler := func(u *Update) (bool, error) {
|
|
|
|
if !filter(u) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
err := handler(u)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
d.handlers = append(d.handlers, newHandler)
|
|
|
|
return d
|
|
|
|
}
|
2023-01-24 01:08:24 +03:00
|
|
|
|
2023-04-13 11:14:37 +03:00
|
|
|
func (d *Dispatcher) HandleCallback(callback string, 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 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
err := handler(u)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
d.handlers = append(d.handlers, newHandler)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2023-01-24 01:08:24 +03:00
|
|
|
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
|
|
|
|
}
|
2023-01-24 23:16:58 +03:00
|
|
|
|
|
|
|
func ChainRun(u *Update, disps ...*Dispatcher) error {
|
|
|
|
for _, d := range disps {
|
|
|
|
if err := d.Run(u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|