Skip to content

Commit

Permalink
Added RegisterHandlerMatchFunc (#9)
Browse files Browse the repository at this point in the history
which registers handler that can be matched based on
arbitrary `func(update *models.Update) bool`
  • Loading branch information
dir01 authored Dec 26, 2022
1 parent fb8a5e1 commit bf34945
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type HttpClient interface {
type ErrorsHandler func(err error)
type Middleware func(next HandlerFunc) HandlerFunc
type HandlerFunc func(ctx context.Context, bot *Bot, update *models.Update)
type MatchFunc func(update *models.Update) bool

// Bot represents Telegram Bot main object
type Bot struct {
Expand Down
41 changes: 38 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package bot
import (
"regexp"
"strings"

"github.com/go-telegram/bot/models"
)

type HandlerType int
Expand All @@ -20,17 +22,32 @@ const (
MatchTypeContains

matchTypeRegexp
matchTypeFunc
)

type handler struct {
handlerType HandlerType
matchType MatchType
pattern string
handler HandlerFunc
re *regexp.Regexp

pattern string
re *regexp.Regexp
matchFunc MatchFunc
}

func (h handler) match(data string) bool {
func (h handler) match(update *models.Update) bool {
if h.matchType == matchTypeFunc {
return h.matchFunc(update)
}

var data string
switch h.handlerType {
case HandlerTypeMessageText:
data = update.Message.Text
case HandlerTypeCallbackQueryData:
data = update.CallbackQuery.Data
}

if h.matchType == MatchTypeExact {
return data == h.pattern
}
Expand All @@ -46,6 +63,24 @@ func (h handler) match(data string) bool {
return false
}

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

id := RandomString(16)

h := handler{
handlerType: handlerType,
matchType: matchTypeFunc,
matchFunc: matchFunc,
handler: f,
}

b.handlers[id] = h

return id
}

func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp, f HandlerFunc) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()
Expand Down
9 changes: 5 additions & 4 deletions process_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bot

import (
"context"

"github.com/go-telegram/bot/models"
)

Expand All @@ -24,22 +25,22 @@ func (b *Bot) processUpdate(ctx context.Context, upd *models.Update) {
}()

if upd.Message != nil {
h = b.findHandler(HandlerTypeMessageText, upd.Message.Text)
h = b.findHandler(HandlerTypeMessageText, upd)
return
}
if upd.CallbackQuery != nil {
h = b.findHandler(HandlerTypeCallbackQueryData, upd.CallbackQuery.Data)
h = b.findHandler(HandlerTypeCallbackQueryData, upd)
return
}
}

func (b *Bot) findHandler(handlerType HandlerType, pattern string) HandlerFunc {
func (b *Bot) findHandler(handlerType HandlerType, upd *models.Update) HandlerFunc {
b.handlersMx.RLock()
defer b.handlersMx.RUnlock()

for _, h := range b.handlers {
if h.handlerType == handlerType {
if h.match(pattern) {
if h.match(upd) {
return h.handler
}
}
Expand Down

0 comments on commit bf34945

Please sign in to comment.