Skip to content

Commit

Permalink
layout: add new reply flag for the buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Oct 9, 2022
1 parent 3ba2966 commit 7065335
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
7 changes: 7 additions & 0 deletions layout/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ buttons:
- '{{ .Amount }}'
- '{{ .Currency }}'

web_app:
text: This is a web app
web_app:
url: https://google.com

markups:
reply_shortened:
- [ help ]
Expand All @@ -58,6 +63,8 @@ markups:
one_time_keyboard: true
inline:
- [ stop ]
web_app:
- [ web_app ]

results:
article:
Expand Down
8 changes: 6 additions & 2 deletions layout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ type (
}

// Button is a shortcut for tele.Btn.
Button = tele.Btn
Button struct {
tele.Btn `yaml:",inline"`
Data interface{} `yaml:"data"`
IsReply bool `yaml:"reply"`
}

// Markup represents layout-specific markup to be parsed.
Markup struct {
Expand Down Expand Up @@ -342,7 +346,7 @@ func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn
return nil
}

return &btn
return &btn.Btn
}

// Markup returns a markup, which locale is dependent on the context.
Expand Down
17 changes: 16 additions & 1 deletion layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,24 @@ func TestLayout(t *testing.T) {
}, lt.MarkupLocale("en", "reply_extended"))

assert.Equal(t, &tele.ReplyMarkup{
InlineKeyboard: [][]tele.InlineButton{{{Unique: "stop", Text: "Stop", Data: "1"}}},
InlineKeyboard: [][]tele.InlineButton{{
{
Unique: "stop",
Text: "Stop",
Data: "1",
},
}},
}, lt.MarkupLocale("en", "inline", 1))

assert.Equal(t, &tele.ReplyMarkup{
InlineKeyboard: [][]tele.InlineButton{{
{
Text: "This is a web app",
WebApp: &tele.WebApp{URL: "https://google.com"},
},
}},
}, lt.MarkupLocale("en", "web_app"))

assert.Equal(t, &tele.ArticleResult{
ResultBase: tele.ResultBase{
ID: "1853",
Expand Down
21 changes: 11 additions & 10 deletions layout/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
// 1. Shortened reply button

if v, ok := v.(string); ok {
lt.buttons[k] = Button{Text: v}
btn := tele.Btn{Text: v}
lt.buttons[k] = Button{Btn: btn}
continue
}

Expand All @@ -85,29 +86,26 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
return err
}

var btn struct {
Button `yaml:",inline"`
Data interface{} `yaml:"data"`
}
var btn Button
if err := yaml.Unmarshal(data, &btn); err != nil {
return err
}

if btn.Data != nil {
if !btn.IsReply && btn.Data != nil {
if a, ok := btn.Data.([]interface{}); ok {
s := make([]string, len(a))
for i, v := range a {
s[i] = fmt.Sprint(v)
}
btn.Button.Data = strings.Join(s, "|")
btn.Btn.Data = strings.Join(s, "|")
} else if s, ok := btn.Data.(string); ok {
btn.Button.Data = s
btn.Btn.Data = s
} else {
return fmt.Errorf("telebot/layout: invalid callback_data for %s button", k)
}
}

lt.buttons[k] = btn.Button
lt.buttons[k] = btn
}

lt.markups = make(map[string]Markup, len(aux.Markups))
Expand Down Expand Up @@ -152,7 +150,10 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
inline := btn.URL != "" ||
btn.Unique != "" ||
btn.InlineQuery != "" ||
btn.InlineQueryChat != ""
btn.InlineQueryChat != "" ||
btn.Login != nil ||
btn.WebApp != nil
inline = !btn.IsReply && inline

if markup.inline == nil {
markup.inline = &inline
Expand Down

0 comments on commit 7065335

Please sign in to comment.