Skip to content

Commit 7065335

Browse files
committed
layout: add new reply flag for the buttons
1 parent 3ba2966 commit 7065335

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

layout/example.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ buttons:
4848
- '{{ .Amount }}'
4949
- '{{ .Currency }}'
5050

51+
web_app:
52+
text: This is a web app
53+
web_app:
54+
url: https://google.com
55+
5156
markups:
5257
reply_shortened:
5358
- [ help ]
@@ -58,6 +63,8 @@ markups:
5863
one_time_keyboard: true
5964
inline:
6065
- [ stop ]
66+
web_app:
67+
- [ web_app ]
6168

6269
results:
6370
article:

layout/layout.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ type (
3232
}
3333

3434
// Button is a shortcut for tele.Btn.
35-
Button = tele.Btn
35+
Button struct {
36+
tele.Btn `yaml:",inline"`
37+
Data interface{} `yaml:"data"`
38+
IsReply bool `yaml:"reply"`
39+
}
3640

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

345-
return &btn
349+
return &btn.Btn
346350
}
347351

348352
// Markup returns a markup, which locale is dependent on the context.

layout/layout_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,24 @@ func TestLayout(t *testing.T) {
8484
}, lt.MarkupLocale("en", "reply_extended"))
8585

8686
assert.Equal(t, &tele.ReplyMarkup{
87-
InlineKeyboard: [][]tele.InlineButton{{{Unique: "stop", Text: "Stop", Data: "1"}}},
87+
InlineKeyboard: [][]tele.InlineButton{{
88+
{
89+
Unique: "stop",
90+
Text: "Stop",
91+
Data: "1",
92+
},
93+
}},
8894
}, lt.MarkupLocale("en", "inline", 1))
8995

96+
assert.Equal(t, &tele.ReplyMarkup{
97+
InlineKeyboard: [][]tele.InlineButton{{
98+
{
99+
Text: "This is a web app",
100+
WebApp: &tele.WebApp{URL: "https://google.com"},
101+
},
102+
}},
103+
}, lt.MarkupLocale("en", "web_app"))
104+
90105
assert.Equal(t, &tele.ArticleResult{
91106
ResultBase: tele.ResultBase{
92107
ID: "1853",

layout/parser.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
7474
// 1. Shortened reply button
7575

7676
if v, ok := v.(string); ok {
77-
lt.buttons[k] = Button{Text: v}
77+
btn := tele.Btn{Text: v}
78+
lt.buttons[k] = Button{Btn: btn}
7879
continue
7980
}
8081

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

88-
var btn struct {
89-
Button `yaml:",inline"`
90-
Data interface{} `yaml:"data"`
91-
}
89+
var btn Button
9290
if err := yaml.Unmarshal(data, &btn); err != nil {
9391
return err
9492
}
9593

96-
if btn.Data != nil {
94+
if !btn.IsReply && btn.Data != nil {
9795
if a, ok := btn.Data.([]interface{}); ok {
9896
s := make([]string, len(a))
9997
for i, v := range a {
10098
s[i] = fmt.Sprint(v)
10199
}
102-
btn.Button.Data = strings.Join(s, "|")
100+
btn.Btn.Data = strings.Join(s, "|")
103101
} else if s, ok := btn.Data.(string); ok {
104-
btn.Button.Data = s
102+
btn.Btn.Data = s
105103
} else {
106104
return fmt.Errorf("telebot/layout: invalid callback_data for %s button", k)
107105
}
108106
}
109107

110-
lt.buttons[k] = btn.Button
108+
lt.buttons[k] = btn
111109
}
112110

113111
lt.markups = make(map[string]Markup, len(aux.Markups))
@@ -152,7 +150,10 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
152150
inline := btn.URL != "" ||
153151
btn.Unique != "" ||
154152
btn.InlineQuery != "" ||
155-
btn.InlineQueryChat != ""
153+
btn.InlineQueryChat != "" ||
154+
btn.Login != nil ||
155+
btn.WebApp != nil
156+
inline = !btn.IsReply && inline
156157

157158
if markup.inline == nil {
158159
markup.inline = &inline

0 commit comments

Comments
 (0)