|
|
|
@ -38,8 +38,9 @@ func NewBot(pref Settings) (*Bot, error) {
|
|
|
|
|
Poller: pref.Poller,
|
|
|
|
|
OnError: pref.OnError,
|
|
|
|
|
|
|
|
|
|
Updates: make(chan Update, pref.Updates),
|
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
|
Updates: make(chan Update, pref.Updates),
|
|
|
|
|
handlers: make(map[string]HandlerFunc),
|
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
|
|
|
|
|
|
synchronous: pref.Synchronous,
|
|
|
|
|
verbose: pref.Verbose,
|
|
|
|
@ -71,6 +72,7 @@ type Bot struct {
|
|
|
|
|
OnError func(error, Context)
|
|
|
|
|
|
|
|
|
|
group *Group
|
|
|
|
|
handlers map[string]HandlerFunc
|
|
|
|
|
synchronous bool
|
|
|
|
|
verbose bool
|
|
|
|
|
parseMode ParseMode
|
|
|
|
@ -144,7 +146,7 @@ type Command struct {
|
|
|
|
|
|
|
|
|
|
// Group returns a new group.
|
|
|
|
|
func (b *Bot) Group() *Group {
|
|
|
|
|
return &Group{handlers: make(map[string]HandlerFunc)}
|
|
|
|
|
return &Group{b: b}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use adds middleware to the global bot chain.
|
|
|
|
@ -172,7 +174,22 @@ func (b *Bot) Use(middleware ...MiddlewareFunc) {
|
|
|
|
|
// b.Handle("/ban", onBan, protected)
|
|
|
|
|
//
|
|
|
|
|
func (b *Bot) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
|
b.group.Handle(endpoint, h, m...)
|
|
|
|
|
if len(b.group.middleware) > 0 {
|
|
|
|
|
m = append(b.group.middleware, m...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handler := func(c Context) error {
|
|
|
|
|
return applyMiddleware(h, m...)(c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch end := endpoint.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
b.handlers[end] = handler
|
|
|
|
|
case CallbackEndpoint:
|
|
|
|
|
b.handlers[end.CallbackUnique()] = handler
|
|
|
|
|
default:
|
|
|
|
|
panic("telebot: unsupported endpoint")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
@ -365,7 +382,7 @@ func (b *Bot) ProcessUpdate(upd Update) {
|
|
|
|
|
if match != nil {
|
|
|
|
|
unique, payload := match[0][1], match[0][3]
|
|
|
|
|
|
|
|
|
|
if handler, ok := b.group.handlers["\f"+unique]; ok {
|
|
|
|
|
if handler, ok := b.handlers["\f"+unique]; ok {
|
|
|
|
|
upd.Callback.Data = payload
|
|
|
|
|
b.runHandler(handler, c)
|
|
|
|
|
return
|
|
|
|
@ -410,7 +427,7 @@ func (b *Bot) ProcessUpdate(upd Update) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Bot) handle(end string, c Context) bool {
|
|
|
|
|
if handler, ok := b.group.handlers[end]; ok {
|
|
|
|
|
if handler, ok := b.handlers[end]; ok {
|
|
|
|
|
b.runHandler(handler, c)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|