2017-11-18 13:06:20 +00:00
|
|
|
package telebot
|
|
|
|
|
2019-12-28 11:01:17 +00:00
|
|
|
import "encoding/json"
|
|
|
|
|
2017-11-26 02:33:28 +00:00
|
|
|
// CallbackEndpoint is an interface any element capable
|
|
|
|
// of responding to a callback `\f<unique>`.
|
|
|
|
type CallbackEndpoint interface {
|
|
|
|
CallbackUnique() string
|
|
|
|
}
|
|
|
|
|
2017-11-18 13:06:20 +00:00
|
|
|
// Callback object represents a query from a callback button in an
|
|
|
|
// inline keyboard.
|
|
|
|
type Callback struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
// For message sent to channels, Sender may be empty
|
|
|
|
Sender *User `json:"from"`
|
|
|
|
|
|
|
|
// Message will be set if the button that originated the query
|
|
|
|
// was attached to a message sent by a bot.
|
|
|
|
Message *Message `json:"message"`
|
|
|
|
|
|
|
|
// MessageID will be set if the button was attached to a message
|
|
|
|
// sent via the bot in inline mode.
|
|
|
|
MessageID string `json:"inline_message_id"`
|
|
|
|
|
|
|
|
// Data associated with the callback button. Be aware that
|
|
|
|
// a bad client can send arbitrary data in this field.
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:57:24 +00:00
|
|
|
// IsInline says whether message is an inline message.
|
|
|
|
func (c *Callback) IsInline() bool {
|
|
|
|
return c.MessageID != ""
|
|
|
|
}
|
|
|
|
|
2017-11-18 13:06:20 +00:00
|
|
|
// CallbackResponse builds a response to a Callback query.
|
|
|
|
//
|
|
|
|
// See also: https://core.telegram.org/bots/api#answerCallbackQuery
|
|
|
|
type CallbackResponse struct {
|
|
|
|
// The ID of the callback to which this is a response.
|
2017-11-24 14:08:23 +00:00
|
|
|
//
|
|
|
|
// Note: Telebot sets this field automatically!
|
2017-11-18 13:06:20 +00:00
|
|
|
CallbackID string `json:"callback_query_id"`
|
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
// Text of the notification. If not specified, nothing will be
|
|
|
|
// shown to the user.
|
2017-11-18 13:06:20 +00:00
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
|
|
|
|
// (Optional) If true, an alert will be shown by the client instead
|
|
|
|
// of a notification at the top of the chat screen. Defaults to false.
|
|
|
|
ShowAlert bool `json:"show_alert,omitempty"`
|
|
|
|
|
|
|
|
// (Optional) URL that will be opened by the user's client.
|
2017-11-24 14:08:23 +00:00
|
|
|
// If you have created a Game and accepted the conditions via
|
|
|
|
// @BotFather, specify the URL that opens your game.
|
|
|
|
//
|
|
|
|
// Note: this will only work if the query comes from a game
|
|
|
|
// callback button. Otherwise, you may use deep-linking:
|
|
|
|
// https://telegram.me/your_bot?start=XXXX
|
2017-11-18 13:06:20 +00:00
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
|
|
|
// InlineButton represents a button displayed in the message.
|
|
|
|
type InlineButton struct {
|
|
|
|
// Unique slagish name for this kind of button,
|
|
|
|
// try to be as specific as possible.
|
|
|
|
//
|
|
|
|
// It will be used as a callback endpoint.
|
|
|
|
Unique string `json:"unique,omitempty"`
|
|
|
|
|
2019-09-30 15:37:19 +00:00
|
|
|
Text string `json:"text"`
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
Data string `json:"callback_data,omitempty"`
|
|
|
|
InlineQuery string `json:"switch_inline_query,omitempty"`
|
|
|
|
InlineQueryChat string `json:"switch_inline_query_current_chat"`
|
2019-12-28 11:01:17 +00:00
|
|
|
Login *Login `json:"login_url,omitempty"`
|
2017-11-26 02:33:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 01:33:12 +00:00
|
|
|
// With returns a copy of the button with data.
|
|
|
|
func (t *InlineButton) With(data string) *InlineButton {
|
|
|
|
return &InlineButton{
|
|
|
|
Unique: t.Unique,
|
|
|
|
Text: t.Text,
|
2020-10-01 20:08:18 +00:00
|
|
|
URL: t.URL,
|
2020-05-13 01:33:12 +00:00
|
|
|
InlineQuery: t.InlineQuery,
|
|
|
|
InlineQueryChat: t.InlineQueryChat,
|
|
|
|
Login: t.Login,
|
2020-05-22 11:52:25 +00:00
|
|
|
Data: data,
|
2020-05-13 01:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 09:02:35 +00:00
|
|
|
// CallbackUnique returns InlineButton.Unique.
|
2017-11-26 02:33:28 +00:00
|
|
|
func (t *InlineButton) CallbackUnique() string {
|
2017-12-10 18:51:43 +00:00
|
|
|
return "\f" + t.Unique
|
2017-11-26 02:33:28 +00:00
|
|
|
}
|
2017-11-27 14:19:42 +00:00
|
|
|
|
|
|
|
// CallbackUnique returns KeyboardButton.Text.
|
2017-11-28 22:15:50 +00:00
|
|
|
func (t *ReplyButton) CallbackUnique() string {
|
2017-11-27 14:19:42 +00:00
|
|
|
return t.Text
|
|
|
|
}
|
2019-12-28 11:01:17 +00:00
|
|
|
|
2020-05-22 11:52:25 +00:00
|
|
|
// CallbackUnique implements CallbackEndpoint.
|
|
|
|
func (t *Btn) CallbackUnique() string {
|
|
|
|
if t.Unique != "" {
|
|
|
|
return "\f" + t.Unique
|
|
|
|
}
|
|
|
|
return t.Text
|
|
|
|
}
|
|
|
|
|
2019-12-28 11:01:17 +00:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:52:25 +00:00
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
2019-12-28 11:01:17 +00:00
|
|
|
// 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) {
|
2020-05-21 09:02:35 +00:00
|
|
|
type InlineButtonJSON InlineButton
|
2019-12-28 11:01:17 +00:00
|
|
|
|
|
|
|
if t.Login != nil {
|
|
|
|
return json.Marshal(struct {
|
2020-05-21 09:02:35 +00:00
|
|
|
InlineButtonJSON
|
2019-12-28 11:01:17 +00:00
|
|
|
InlineQueryChat string `json:"switch_inline_query_current_chat,omitempty"`
|
|
|
|
}{
|
2020-05-21 09:02:35 +00:00
|
|
|
InlineButtonJSON: InlineButtonJSON(*t),
|
2019-12-28 11:01:17 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-21 09:02:35 +00:00
|
|
|
return json.Marshal(InlineButtonJSON(*t))
|
2019-12-28 11:01:17 +00:00
|
|
|
}
|