Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: keep order of commands, better docs #738

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions layout/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ settings:
commands:
/start: Start the bot
/help: How to use the bot
/settings: "{{ text `cmd_settings` }}"

config:
str: string
Expand Down
26 changes: 12 additions & 14 deletions layout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/fs"
"log"
"os"
"strings"
"sync"
"text/template"

Expand All @@ -23,7 +22,7 @@ type (
ctxs map[tele.Context]string
funcs template.FuncMap

commands map[string]string
commands []tele.Command
buttons map[string]Button
markups map[string]Markup
results map[string]Result
Expand Down Expand Up @@ -174,19 +173,16 @@ func (lt *Layout) SetLocale(c tele.Context, locale string) {
lt.mu.Unlock()
}

// Commands returns a list of telebot commands, which can be
// Commands returns a list of telebot commands
// in the order they were defined in config, which can be
// used in b.SetCommands later.
// "commands" must be not templates
func (lt *Layout) Commands() (cmds []tele.Command) {
for k, v := range lt.commands {
cmds = append(cmds, tele.Command{
Text: strings.TrimLeft(k, "/"),
Description: v,
})
}
return
return lt.commands
}

// CommandsLocale returns a list of telebot commands and localized description, which can be
// CommandsLocale returns a list of telebot commands and localized descriptions
// in the order they were defined in config, which can be
// used in b.SetCommands later.
//
// Example of bot.yml:
Expand All @@ -206,14 +202,16 @@ func (lt *Layout) Commands() (cmds []tele.Command) {
//
// b.SetCommands(lt.CommandsLocale("en"), "en")
// b.SetCommands(lt.CommandsLocale("ru"), "ru")
// b.SetCommands(lt.CommandsLocale("en"))
func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tele.Command) {
var arg interface{}
if len(args) > 0 {
arg = args[0]
}

for k, v := range lt.commands {
tmpl, err := lt.template(template.New(k).Funcs(lt.funcs), locale).Parse(v)
for _, cmd := range lt.commands {
tmpl, err := lt.template(template.New(cmd.Text).Funcs(lt.funcs), locale).
Parse(cmd.Description)
if err != nil {
log.Println("telebot/layout:", err)
return nil
Expand All @@ -226,7 +224,7 @@ func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tel
}

cmds = append(cmds, tele.Command{
Text: strings.TrimLeft(k, "/"),
Text: cmd.Text,
Description: buf.String(),
})
}
Expand Down
15 changes: 14 additions & 1 deletion layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ func TestLayout(t *testing.T) {
assert.Equal(t, &tele.LongPoller{}, pref.Poller)
assert.Equal(t, pref, ltfs.Settings())

assert.ElementsMatch(t, []tele.Command{{
assert.Equal(t, []tele.Command{{
Text: "start",
Description: "Start the bot",
}, {
Text: "help",
Description: "How to use the bot",
}, {
Text: "settings",
Description: "{{ text `cmd_settings` }}",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previous pr had a question about testing this part of template
#681 (comment)

The reasons to keep lt.Commands in tests:

  1. make it clear what happens when you use templates with lt.Commands()
  2. the test already contained testing of both Commands and CommandsLocale functions, kept it here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demget should I check anything else before you run CI workflow?

}}, lt.Commands())
assert.Equal(t, []tele.Command{{
Text: "start",
Description: "Start the bot",
}, {
Text: "help",
Description: "How to use the bot",
}, {
Text: "settings",
Description: "Settings",
}}, lt.CommandsLocale("en"))

assert.Equal(t, "string", lt.String("str"))
assert.Equal(t, 123, lt.Int("num"))
Expand Down
2 changes: 2 additions & 0 deletions layout/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ nested:
another:
example: |-
This is {{ . }}.

cmd_settings: Settings
19 changes: 17 additions & 2 deletions layout/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
var aux struct {
Settings *Settings
Config map[string]interface{}
Commands map[string]string
Commands yaml.MapSlice
Buttons yaml.MapSlice
Markups yaml.MapSlice
Results yaml.MapSlice
Expand All @@ -46,7 +46,22 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
}

lt.Config = Config{v: v}
lt.commands = aux.Commands
lt.commands = make([]tele.Command, 0, len(aux.Commands))
for _, cmd := range aux.Commands {
var (
text string
description string
ok bool
)

if text, ok = cmd.Key.(string); !ok {
continue
}
if description, ok = cmd.Value.(string); !ok {
continue
}
lt.commands = append(lt.commands, tele.Command{Text: strings.TrimLeft(text, "/"), Description: description})
}

if pref := aux.Settings; pref != nil {
lt.pref = &tele.Settings{
Expand Down
Loading