Skip to content

Commit

Permalink
FIX: keep commands order, make example more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
dvordrova committed Aug 21, 2024
1 parent cc61a96 commit 5a355da
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
30 changes: 15 additions & 15 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 All @@ -22,7 +23,6 @@ config:
- <<: *obj
- <<: *obj


buttons:
# Shortened reply buttons
help: Help
Expand All @@ -37,16 +37,16 @@ buttons:
stop:
unique: stop
text: Stop
data: '{{.}}'
data: "{{.}}"

# Callback data
pay:
unique: pay
text: Pay
data:
- '{{ .UserID }}'
- '{{ .Amount }}'
- '{{ .Currency }}'
- "{{ .UserID }}"
- "{{ .Amount }}"
- "{{ .Currency }}"

web_app:
text: This is a web app
Expand All @@ -55,22 +55,22 @@ buttons:

markups:
reply_shortened:
- [ help ]
- [ settings ]
- [help]
- [settings]
reply_extended:
keyboard:
- [ contact ]
- [contact]
one_time_keyboard: true
inline:
- [ stop ]
- [stop]
web_app:
- [ web_app ]
- [web_app]

results:
article:
type: article
id: '{{ .ID }}'
title: '{{ .Title }}'
description: '{{ .Description }}'
thumbnail_url: '{{ .PreviewURL }}'
message_text: '{{ text `article_message` }}'
id: "{{ .ID }}"
title: "{{ .Title }}"
description: "{{ .Description }}"
thumbnail_url: "{{ .PreviewURL }}"
message_text: "{{ text `article_message` }}"
30 changes: 14 additions & 16 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 @@ -204,16 +200,18 @@ func (lt *Layout) Commands() (cmds []tele.Command) {
//
// Usage:
//
// b.SetCommands(lt.CommandsLocale("en"), "en")
// b.SetCommands(lt.CommandsLocale("ru"), "ru")
// 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` }}",
}}, 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

0 comments on commit 5a355da

Please sign in to comment.