Skip to content

Commit

Permalink
feat: add handler type for photo captions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidramiro committed Dec 22, 2024
1 parent dda4073 commit a3c6f8b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ In this example, the handler will be called when the user sends `/start` message
Handler Types:
- `HandlerTypeMessageText` - for Update.Message.Text field
- `HandlerTypeCallbackQueryData` - for Update.CallbackQuery.Data field
- `HandlerTypePhotoCaption` - for Update.Message.Caption field

RegisterHandler returns a handler ID string. You can use it to remove the handler later.

Expand Down
3 changes: 3 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
HandlerTypeMessageText HandlerType = iota
HandlerTypeCallbackQueryData
HandlerTypeCallbackQueryGameShortName
HandlerTypePhotoCaption
)

type MatchType int
Expand Down Expand Up @@ -57,6 +58,8 @@ func (h handler) match(update *models.Update) bool {
data = update.CallbackQuery.Data
case HandlerTypeCallbackQueryGameShortName:
data = update.CallbackQuery.GameShortName
case HandlerTypePhotoCaption:
data = update.Message.Caption
}

if h.matchType == MatchTypeExact {
Expand Down
18 changes: 18 additions & 0 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ func Test_match_exact(t *testing.T) {
}
}

func Test_match_caption_exact(t *testing.T) {
b := &Bot{}

id := b.RegisterHandler(HandlerTypePhotoCaption, "xxx", MatchTypeExact, nil)

h := findHandler(b, id)

res := h.match(&models.Update{Message: &models.Message{Caption: "zzz"}})
if res {
t.Error("unexpected true result")
}

res = h.match(&models.Update{Message: &models.Message{Caption: "xxx"}})
if !res {
t.Error("unexpected false result")
}
}

func Test_match_prefix(t *testing.T) {
b := &Bot{}

Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func WithCallbackQueryDataHandler(pattern string, matchType MatchType, handler H
}
}

// WithPhotoCaptionHandler allows to set handler for incoming photos with caption
// Also you can use *bot.RegisterHandler function after bot creation
func WithPhotoCaptionHandler(pattern string, matchType MatchType, handler HandlerFunc) Option {
return func(b *Bot) {
b.RegisterHandler(HandlerTypePhotoCaption, pattern, matchType, handler)
}
}

// WithDefaultHandler allows to set default handler for incoming updates
func WithDefaultHandler(handler HandlerFunc) Option {
return func(b *Bot) {
Expand Down
30 changes: 30 additions & 0 deletions process_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,36 @@ func Test_findHandler(t *testing.T) {
}
}

func Test_findPhotoCaptionHandler(t *testing.T) {
var called bool
h := func(ctx context.Context, bot *Bot, update *models.Update) {
called = true
}

bot := &Bot{
defaultHandlerFunc: h,
}

// Register a handler
bot.handlers = append(bot.handlers, handler{
id: "test",
handlerType: HandlerTypePhotoCaption,
matchType: MatchTypeExact,
pattern: "test",
handler: h,
})

ctx := context.Background()
upd := &models.Update{Message: &models.Message{Caption: "test"}}

handler := bot.findHandler(upd)
handler(ctx, bot, upd)

if !called {
t.Fatal("Expected registered handler to be called")
}
}

func Test_findHandler_Default(t *testing.T) {
var called bool
h := func(ctx context.Context, bot *Bot, update *models.Update) {
Expand Down

0 comments on commit a3c6f8b

Please sign in to comment.