Skip to content

Commit

Permalink
options: implement CallbackEndpoint for Btn
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed May 22, 2020
1 parent f5f0b4b commit d02458b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
39 changes: 28 additions & 11 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,22 @@ func TestBotHandle(t *testing.T) {
b.Handle("/start", func(m *Message) {})
assert.Contains(t, b.handlers, "/start")

btn := &InlineButton{Unique: "test"}
b.Handle(btn, func(c *Callback) {})
assert.Contains(t, b.handlers, btn.CallbackUnique())
reply := ReplyButton{Text: "reply"}
b.Handle(&reply, func(m *Message) {})

inline := InlineButton{Unique: "inline"}
b.Handle(&inline, func(c *Callback) {})

btnReply := (&ReplyMarkup{}).Text("btnReply")
b.Handle(&btnReply, func(m *Message) {})

btnInline := (&ReplyMarkup{}).Data("", "btnInline")
b.Handle(&btnInline, func(c *Callback) {})

assert.Contains(t, b.handlers, btnReply.CallbackUnique())
assert.Contains(t, b.handlers, btnInline.CallbackUnique())
assert.Contains(t, b.handlers, reply.CallbackUnique())
assert.Contains(t, b.handlers, inline.CallbackUnique())

assert.Panics(t, func() { b.Handle(1, func() {}) })
}
Expand Down Expand Up @@ -359,16 +372,20 @@ func TestBot(t *testing.T) {

t.Run("EditReplyMarkup()", func(t *testing.T) {
markup := &ReplyMarkup{
InlineKeyboard: [][]InlineButton{{{
Data: "btn",
Text: "Hi Telebot!",
}}},
InlineKeyboard: [][]InlineButton{
{{
Data: "btn",
Text: "Hi Telebot!",
}},
},
}
badMarkup := &ReplyMarkup{
InlineKeyboard: [][]InlineButton{{{
Data: strings.Repeat("*", 65),
Text: "Bad Button",
}}},
InlineKeyboard: [][]InlineButton{
{{
Data: strings.Repeat("*", 65),
Text: "Bad Button",
}},
},
}

msg, err := b.EditReplyMarkup(msg, markup)
Expand Down
13 changes: 10 additions & 3 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func (t *InlineButton) With(data string) *InlineButton {
InlineQuery: t.InlineQuery,
InlineQueryChat: t.InlineQueryChat,
Login: t.Login,

Data: data,
Data: data,
}
}

Expand All @@ -101,6 +100,14 @@ func (t *ReplyButton) CallbackUnique() string {
return t.Text
}

// CallbackUnique implements CallbackEndpoint.
func (t *Btn) CallbackUnique() string {
if t.Unique != "" {
return "\f" + t.Unique
}
return t.Text
}

// Login represents a parameter of the inline keyboard button
// used to automatically authorize a user. Serves as a great replacement
// for the Telegram Login Widget when the user is coming from Telegram.
Expand All @@ -111,7 +118,7 @@ type Login struct {
WriteAccess bool `json:"request_write_access,omitempty"`
}

// MarshalJSON implements Marshaler interface.
// MarshalJSON implements json.Marshaler interface.
// It needed to avoid InlineQueryChat and Login fields conflict.
// If you have Login field in your button, InlineQueryChat must be skipped.
func (t *InlineButton) MarshalJSON() ([]byte, error) {
Expand Down

0 comments on commit d02458b

Please sign in to comment.