diff --git a/bot/replyKeyboards.go b/bot/replyKeyboards.go index e4c540c..4276536 100644 --- a/bot/replyKeyboards.go +++ b/bot/replyKeyboards.go @@ -1,6 +1,7 @@ package bot import ( + "github.com/LucaBernstein/beancount-bot-tg/helpers" tb "gopkg.in/telebot.v3" ) @@ -13,6 +14,9 @@ func ReplyKeyboard(buttons []string) *tb.ReplyMarkup { for _, label := range buttons { buttonsCreated = append(buttonsCreated, kb.Row(kb.Text(label))) } + if len(buttonsCreated) > helpers.MAX_REPLY_KEYBOARD_ENTRIES { + buttonsCreated = buttonsCreated[:helpers.MAX_REPLY_KEYBOARD_ENTRIES] + } kb.Reply(buttonsCreated...) return kb } diff --git a/bot/replyKeyboards_test.go b/bot/replyKeyboards_test.go new file mode 100644 index 0000000..2cebb64 --- /dev/null +++ b/bot/replyKeyboards_test.go @@ -0,0 +1,19 @@ +package bot_test + +import ( + "strings" + "testing" + + "github.com/LucaBernstein/beancount-bot-tg/bot" + "github.com/LucaBernstein/beancount-bot-tg/helpers" +) + +func TestReplyKeyboardMaximumOptions(t *testing.T) { + options := []string{"some", "few", "options"} + reply := bot.ReplyKeyboard(options) + helpers.TestExpect(t, len(reply.ReplyKeyboard), 3, "reply keyboard options count") + + options = strings.Split(strings.Repeat("more_options ", 50), " ") + reply = bot.ReplyKeyboard(options) + helpers.TestExpect(t, len(reply.ReplyKeyboard), helpers.MAX_REPLY_KEYBOARD_ENTRIES, "reply keyboard options count") +} diff --git a/helpers/constants.go b/helpers/constants.go index 3d64394..7cb41a9 100644 --- a/helpers/constants.go +++ b/helpers/constants.go @@ -26,6 +26,8 @@ const ( DEFAULT_CURRENCY = "EUR" TG_MAX_MSG_CHAR_LEN = 4096 + + MAX_REPLY_KEYBOARD_ENTRIES = 40 ) func AllowedSuggestionTypes() []string {