Skip to content

Commit

Permalink
feat: add middlewares for handler (#105)
Browse files Browse the repository at this point in the history
* feat: each handler can add middleware.

* fix: singleFlight middleware example
  • Loading branch information
TBXark authored Sep 16, 2024
1 parent 581c533 commit db32473
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 20 additions & 0 deletions examples/middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"sync"

"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
Expand All @@ -28,6 +29,10 @@ func main() {
panic(err)
}

b.RegisterHandler(bot.HandlerTypeCallbackQueryData, "", bot.MatchTypeExact, func(ctx context.Context, b *bot.Bot, update *models.Update) {
log.Printf("callback query data: %s", update.CallbackQuery.Data)
}, singleFlight)

b.Start(ctx)
}

Expand All @@ -49,6 +54,21 @@ func showMessageWithUserName(next bot.HandlerFunc) bot.HandlerFunc {
}
}

// singleFlight is a middleware that ensures that only one callback query is processed at a time.
func singleFlight(next bot.HandlerFunc) bot.HandlerFunc {
sf := sync.Map{}
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.CallbackQuery != nil {
key := update.CallbackQuery.Message.Message.ID
if _, loaded := sf.LoadOrStore(key, struct{}{}); loaded {
return
}
defer sf.Delete(key)
next(ctx, b, update)
}
}
}

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Expand Down
12 changes: 6 additions & 6 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (h handler) match(update *models.Update) bool {
return false
}

func (b *Bot) RegisterHandlerMatchFunc(matchFunc MatchFunc, f HandlerFunc) string {
func (b *Bot) RegisterHandlerMatchFunc(matchFunc MatchFunc, f HandlerFunc, m ...Middleware) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()

Expand All @@ -79,15 +79,15 @@ func (b *Bot) RegisterHandlerMatchFunc(matchFunc MatchFunc, f HandlerFunc) strin
h := handler{
matchType: matchTypeFunc,
matchFunc: matchFunc,
handler: f,
handler: applyMiddlewares(f, m...),
}

b.handlers[id] = h

return id
}

func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp, f HandlerFunc) string {
func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp, f HandlerFunc, m ...Middleware) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()

Expand All @@ -97,15 +97,15 @@ func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp,
handlerType: handlerType,
matchType: matchTypeRegexp,
re: re,
handler: f,
handler: applyMiddlewares(f, m...),
}

b.handlers[id] = h

return id
}

func (b *Bot) RegisterHandler(handlerType HandlerType, pattern string, matchType MatchType, f HandlerFunc) string {
func (b *Bot) RegisterHandler(handlerType HandlerType, pattern string, matchType MatchType, f HandlerFunc, m ...Middleware) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()

Expand All @@ -115,7 +115,7 @@ func (b *Bot) RegisterHandler(handlerType HandlerType, pattern string, matchType
handlerType: handlerType,
matchType: matchType,
pattern: pattern,
handler: f,
handler: applyMiddlewares(f, m...),
}

b.handlers[id] = h
Expand Down

0 comments on commit db32473

Please sign in to comment.