diff --git a/callbacks.go b/callbacks.go index dbfbf7b..fcf3c75 100644 --- a/callbacks.go +++ b/callbacks.go @@ -81,6 +81,20 @@ type InlineButton struct { Action func(*Callback) `json:"-"` } +// With returns a copy of the button with data. +func (t *InlineButton) With(data string) *InlineButton { + return &InlineButton{ + Unique: t.Unique, + Text: t.Text, + URL: t.Text, + InlineQuery: t.InlineQuery, + InlineQueryChat: t.InlineQueryChat, + Login: t.Login, + + Data: data, + } +} + // CallbackUnique returns InlineButto.Unique. func (t *InlineButton) CallbackUnique() string { return "\f" + t.Unique diff --git a/errors.go b/errors.go index 878cdeb..309883f 100644 --- a/errors.go +++ b/errors.go @@ -13,7 +13,7 @@ type APIError struct { } // ʔ returns description of error. -// A tiny shortcut to make code clearier. +// A tiny shortcut to make code clearer. func (err *APIError) ʔ() string { return err.Description } diff --git a/options.go b/options.go index 055fe9c..5d5b498 100644 --- a/options.go +++ b/options.go @@ -1,6 +1,9 @@ package telebot -import "encoding/json" +import ( + "encoding/json" + "fmt" +) // Option is a shorcut flag type for certain message features // (so-called options). It means that instead of passing @@ -156,3 +159,89 @@ func (pt PollType) MarshalJSON() ([]byte, error) { } return json.Marshal(&aux) } + +type row []Btn +func (r *ReplyMarkup) Row(many ...Btn) row { + return many +} + +func (r *ReplyMarkup) Inline(rows ...row) { + inlineKeys := make([][]InlineButton, 0, len(rows)) + for i, row := range rows { + keys := make([]InlineButton, 0, len(row)) + for j, btn := range row { + btn := btn.Inline() + if btn == nil { + panic(fmt.Sprintf( + "telebot: button row %d column %d is not an inline button", + i, j)) + } + keys = append(keys, *btn) + } + inlineKeys = append(inlineKeys, keys) + } + + r.InlineKeyboard = inlineKeys +} + +func (r *ReplyMarkup) Reply(rows ...row) { + replyKeys := make([][]ReplyButton, 0, len(rows)) + for i, row := range rows { + keys := make([]ReplyButton, 0, len(row)) + for j, btn := range row { + btn := btn.Reply() + if btn == nil { + panic(fmt.Sprintf( + "telebot: button row %d column %d is not a reply button", + i, j)) + } + keys = append(keys, *btn) + } + replyKeys = append(replyKeys, keys) + } + + r.ReplyKeyboard = replyKeys +} + +// Btn is a constructor button, which will later become either a reply, or an inline button. +type Btn struct { + Unique string + Text string + URL string + Data string + InlineQuery string + InlineQueryChat string + Contact bool + Location bool + Poll PollType + Login *Login +} + +func (b Btn) Inline() *InlineButton { + if b.Unique == "" { + return nil + } + + return &InlineButton{ + Unique: b.Unique, + Text: b.Text, + URL: b.URL, + Data: b.Data, + InlineQuery: b.InlineQuery, + InlineQueryChat: b.InlineQueryChat, + Login: nil, + } +} + +func (b Btn) Reply() *ReplyButton { + if b.Unique != "" { + return nil + } + + return &ReplyButton{ + Text: b.Text, + Contact: b.Contact, + Location: b.Location, + Poll: b.Poll, + } +}