Skip to content

Commit

Permalink
Add login_url field support to InlineButton
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Dec 28, 2019
1 parent baa616b commit 40dabc4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions callbacks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package telebot

import "encoding/json"

// CallbackEndpoint is an interface any element capable
// of responding to a callback `\f<unique>`.
type CallbackEndpoint interface {
Expand Down Expand Up @@ -72,6 +74,7 @@ type InlineButton struct {
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
InlineQueryChat string `json:"switch_inline_query_current_chat"`
Login *Login `json:"login_url,omitempty"`

Action func(*Callback) `json:"-"`
}
Expand All @@ -85,3 +88,30 @@ func (t *InlineButton) CallbackUnique() string {
func (t *ReplyButton) CallbackUnique() string {
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.
type Login struct {
URL string `json:"url"`
Text string `json:"forward_text,omitempty"`
Username string `json:"bot_username,omitempty"`
WriteAccess bool `json:"request_write_access,omitempty"`
}

// MarshalJSON implements 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) {
type InineButtonJSON InlineButton

if t.Login != nil {
return json.Marshal(struct {
InineButtonJSON
InlineQueryChat string `json:"switch_inline_query_current_chat,omitempty"`
}{
InineButtonJSON: InineButtonJSON(*t),
})
}
return json.Marshal(InineButtonJSON(*t))
}

0 comments on commit 40dabc4

Please sign in to comment.