From 7065335e1dcb16d9c57b0d534157e755f32e0137 Mon Sep 17 00:00:00 2001 From: Demian Date: Sun, 9 Oct 2022 20:08:33 +0300 Subject: [PATCH] layout: add new reply flag for the buttons --- layout/example.yml | 7 +++++++ layout/layout.go | 8 ++++++-- layout/layout_test.go | 17 ++++++++++++++++- layout/parser.go | 21 +++++++++++---------- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/layout/example.yml b/layout/example.yml index d0949ce..69c507e 100644 --- a/layout/example.yml +++ b/layout/example.yml @@ -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: diff --git a/layout/layout.go b/layout/layout.go index 044a7eb..da6ad94 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -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. diff --git a/layout/layout_test.go b/layout/layout_test.go index cda8e32..b34e3d1 100644 --- a/layout/layout_test.go +++ b/layout/layout_test.go @@ -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", diff --git a/layout/parser.go b/layout/parser.go index c0b40a3..6f0a659 100644 --- a/layout/parser.go +++ b/layout/parser.go @@ -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