1
bot/process_update.go

46 lines
760 B
Go
Raw Permalink Normal View History

2022-04-26 00:02:51 +08:00
package bot
import (
"context"
2022-04-26 00:02:51 +08:00
"github.com/go-telegram/bot/models"
)
func applyMiddlewares(h HandlerFunc, m ...Middleware) HandlerFunc {
if len(m) < 1 {
return h
}
wrapped := h
for i := len(m) - 1; i >= 0; i-- {
wrapped = m[i](wrapped)
}
return wrapped
}
2023-04-05 17:58:39 +08:00
// ProcessUpdate allows you to process update
func (b *Bot) ProcessUpdate(ctx context.Context, upd *models.Update) {
2024-10-17 16:17:15 +08:00
h := b.findHandler(upd)
2022-04-26 00:02:51 +08:00
2024-10-17 16:17:15 +08:00
r := applyMiddlewares(h, b.middlewares...)
2022-04-26 00:02:51 +08:00
2024-10-17 16:17:15 +08:00
if b.notAsyncHandlers {
r(ctx, b, upd)
return
}
go r(ctx, b, upd)
2022-04-26 00:02:51 +08:00
}
func (b *Bot) findHandler(upd *models.Update) HandlerFunc {
2022-04-26 00:02:51 +08:00
b.handlersMx.RLock()
defer b.handlersMx.RUnlock()
for _, h := range b.handlers {
if h.match(upd) {
return h.handler
2022-04-26 00:02:51 +08:00
}
}
return b.defaultHandlerFunc
}