layout: add new reply flag for the buttons

This commit is contained in:
Demian 2022-10-09 20:08:33 +03:00
parent 3ba2966f52
commit 9fca5ecf63
4 changed files with 40 additions and 13 deletions

View File

@ -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 ]
@ -58,6 +63,8 @@ markups:
one_time_keyboard: true
inline:
- [ stop ]
web_app:
- [ web_app ]
results:
article:

View File

@ -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 {
@ -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.

View File

@ -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",

View File

@ -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
}
@ -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))
@ -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