layout: add commands section

pull/447/head
Demian 3 years ago
parent f8ff8053ab
commit fe3f4aa533

@ -3,6 +3,10 @@ settings:
parse_mode: html
long_poller: {}
commands:
/start: Start the bot
/help: How to use the bot
config:
str: string
num: 123

@ -21,10 +21,11 @@ type (
ctxs map[tele.Context]string
funcs template.FuncMap
buttons map[string]Button
markups map[string]Markup
results map[string]Result
locales map[string]*template.Template
commands map[string]string
buttons map[string]Button
markups map[string]Markup
results map[string]Result
locales map[string]*template.Template
*Config
}
@ -156,6 +157,18 @@ func (lt *Layout) SetLocale(c tele.Context, locale string) {
lt.mu.Unlock()
}
// Commands returns a list of telebot commands, which can be
// used in b.SetCommands later.
func (lt *Layout) Commands() (cmds []tele.Command) {
for k, v := range lt.commands {
cmds = append(cmds, tele.Command{
Text: k,
Description: v,
})
}
return
}
// Text returns a text, which locale is dependent on the context.
// The given optional argument will be passed to the template engine.
//

@ -22,6 +22,14 @@ func TestLayout(t *testing.T) {
assert.Equal(t, "html", pref.ParseMode)
assert.Equal(t, &tele.LongPoller{}, pref.Poller)
assert.Equal(t, []tele.Command{{
Text: "/start",
Description: "Start the bot",
}, {
Text: "/help",
Description: "How to use the bot",
}}, lt.Commands())
assert.Equal(t, "string", lt.String("str"))
assert.Equal(t, 123, lt.Int("num"))
assert.Equal(t, int64(123), lt.Int64("num"))

@ -29,6 +29,7 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
var aux struct {
Settings *Settings
Config map[string]interface{}
Commands map[string]string
Buttons yaml.MapSlice
Markups yaml.MapSlice
Results yaml.MapSlice
@ -38,9 +39,8 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
return err
}
lt.Config = &Config{
v: aux.Config,
}
lt.Config = &Config{v: aux.Config}
lt.commands = aux.Commands
if pref := aux.Settings; pref != nil {
lt.pref = &tele.Settings{

Loading…
Cancel
Save