diff --git a/README.md b/README.md index 77667df..e1b99b0 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,7 @@ b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...) - `WithMiddlewares(middlewares ...Middleware)` - add middlewares - `WithMessageTextHandler(pattern string, matchType MatchType, handler HandlerFunc)` - add handler for Message.Text field - `WithCallbackQueryDataHandler(pattern string, matchType MatchType, handler HandlerFunc)` - add handler for CallbackQuery.Data field +- `WithPhotoCaptionHandler` - add handler for Message.Caption field - `WithDefaultHandler(handler HandlerFunc)` - add default handler - `WithDebug()` - enable debug mode - `WithErrorsHandler(handler ErrorsHandler)` - add errors handler @@ -189,7 +190,7 @@ b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...) ## Message.Text and CallbackQuery.Data handlers -For your convenience, you can use `Message.Text` and `CallbackQuery.Data` handlers. +For your convenience, you can use `Message.Text`, `CallbackQuery.Data` and `Message.Caption` handlers. An example: @@ -209,6 +210,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. diff --git a/handlers.go b/handlers.go index eb4fefa..29a7974 100644 --- a/handlers.go +++ b/handlers.go @@ -13,6 +13,7 @@ const ( HandlerTypeMessageText HandlerType = iota HandlerTypeCallbackQueryData HandlerTypeCallbackQueryGameShortName + HandlerTypePhotoCaption ) type MatchType int @@ -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 { diff --git a/handlers_test.go b/handlers_test.go index 804e8ac..18e2d85 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -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{} diff --git a/options.go b/options.go index 8a843f3..720129e 100644 --- a/options.go +++ b/options.go @@ -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) { diff --git a/process_update_test.go b/process_update_test.go index 6f3dc46..117f4c9 100644 --- a/process_update_test.go +++ b/process_update_test.go @@ -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) {