|
|
@ -38,9 +38,8 @@ func NewBot(pref Settings) (*Bot, error) {
|
|
|
|
Poller: pref.Poller,
|
|
|
|
Poller: pref.Poller,
|
|
|
|
OnError: pref.OnError,
|
|
|
|
OnError: pref.OnError,
|
|
|
|
|
|
|
|
|
|
|
|
Updates: make(chan Update, pref.Updates),
|
|
|
|
Updates: make(chan Update, pref.Updates),
|
|
|
|
handlers: make(map[string]HandlerFunc),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronous: pref.Synchronous,
|
|
|
|
synchronous: pref.Synchronous,
|
|
|
|
verbose: pref.Verbose,
|
|
|
|
verbose: pref.Verbose,
|
|
|
@ -72,7 +71,6 @@ type Bot struct {
|
|
|
|
OnError func(error, Context)
|
|
|
|
OnError func(error, Context)
|
|
|
|
|
|
|
|
|
|
|
|
group *Group
|
|
|
|
group *Group
|
|
|
|
handlers map[string]HandlerFunc
|
|
|
|
|
|
|
|
synchronous bool
|
|
|
|
synchronous bool
|
|
|
|
verbose bool
|
|
|
|
verbose bool
|
|
|
|
parseMode ParseMode
|
|
|
|
parseMode ParseMode
|
|
|
@ -144,16 +142,16 @@ type Command struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Group returns a new group.
|
|
|
|
|
|
|
|
func (b *Bot) Group() *Group {
|
|
|
|
|
|
|
|
return &Group{handlers: make(map[string]HandlerFunc)}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Use adds middleware to the global bot chain.
|
|
|
|
// Use adds middleware to the global bot chain.
|
|
|
|
func (b *Bot) Use(middleware ...MiddlewareFunc) {
|
|
|
|
func (b *Bot) Use(middleware ...MiddlewareFunc) {
|
|
|
|
b.group.Use(middleware...)
|
|
|
|
b.group.Use(middleware...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Group returns a new group.
|
|
|
|
|
|
|
|
func (b *Bot) Group() *Group {
|
|
|
|
|
|
|
|
return &Group{b: b}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle lets you set the handler for some command name or
|
|
|
|
// Handle lets you set the handler for some command name or
|
|
|
|
// one of the supported endpoints. It also applies middleware
|
|
|
|
// one of the supported endpoints. It also applies middleware
|
|
|
|
// if such passed to the function.
|
|
|
|
// if such passed to the function.
|
|
|
@ -174,20 +172,7 @@ func (b *Bot) Group() *Group {
|
|
|
|
// b.Handle("/ban", onBan, protected)
|
|
|
|
// b.Handle("/ban", onBan, protected)
|
|
|
|
//
|
|
|
|
//
|
|
|
|
func (b *Bot) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
func (b *Bot) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
if m != nil {
|
|
|
|
b.group.Handle(endpoint, h, m...)
|
|
|
|
h = func(c Context) error {
|
|
|
|
|
|
|
|
return applyMiddleware(h, m...)(c)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch end := endpoint.(type) {
|
|
|
|
|
|
|
|
case string:
|
|
|
|
|
|
|
|
b.handlers[end] = h
|
|
|
|
|
|
|
|
case CallbackEndpoint:
|
|
|
|
|
|
|
|
b.handlers[end.CallbackUnique()] = h
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
panic("telebot: unsupported endpoint")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
@ -380,7 +365,7 @@ func (b *Bot) ProcessUpdate(upd Update) {
|
|
|
|
if match != nil {
|
|
|
|
if match != nil {
|
|
|
|
unique, payload := match[0][1], match[0][3]
|
|
|
|
unique, payload := match[0][1], match[0][3]
|
|
|
|
|
|
|
|
|
|
|
|
if handler, ok := b.handlers["\f"+unique]; ok {
|
|
|
|
if handler, ok := b.group.handlers["\f"+unique]; ok {
|
|
|
|
upd.Callback.Data = payload
|
|
|
|
upd.Callback.Data = payload
|
|
|
|
b.runHandler(handler, c)
|
|
|
|
b.runHandler(handler, c)
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -425,7 +410,7 @@ func (b *Bot) ProcessUpdate(upd Update) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (b *Bot) handle(end string, c Context) bool {
|
|
|
|
func (b *Bot) handle(end string, c Context) bool {
|
|
|
|
if handler, ok := b.handlers[end]; ok {
|
|
|
|
if handler, ok := b.group.handlers[end]; ok {
|
|
|
|
b.runHandler(handler, c)
|
|
|
|
b.runHandler(handler, c)
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|