Skip to content

Commit

Permalink
Limit reply keyboard options
Browse files Browse the repository at this point in the history
Resolves #219
  • Loading branch information
LucaBernstein committed May 20, 2023
1 parent 6d442e6 commit c4f4030
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bot/replyKeyboards.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bot

import (
"github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/telebot.v3"
)

Expand All @@ -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
}
19 changes: 19 additions & 0 deletions bot/replyKeyboards_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 2 additions & 0 deletions helpers/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
DEFAULT_CURRENCY = "EUR"

TG_MAX_MSG_CHAR_LEN = 4096

MAX_REPLY_KEYBOARD_ENTRIES = 40
)

func AllowedSuggestionTypes() []string {
Expand Down

0 comments on commit c4f4030

Please sign in to comment.