Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add middlewares for handler #105

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading