keyboards: simple constructors added

pull/293/head
Ian Patrick Badtrousers 4 years ago
parent eb74dcb35a
commit d7c155f97e

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

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

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

Loading…
Cancel
Save