forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
callbacks.go
136 lines (115 loc) · 4.18 KB
/
callbacks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package telebot
import "encoding/json"
// CallbackEndpoint is an interface any element capable
// of responding to a callback `\f<unique>`.
type CallbackEndpoint interface {
CallbackUnique() string
}
// 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"`
}
// IsInline says whether message is an inline message.
func (c *Callback) IsInline() bool {
return c.MessageID != ""
}
// 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.
//
// Note: Telebot sets this field automatically!
CallbackID string `json:"callback_query_id"`
// Text of the notification. If not specified, nothing will be
// shown to the user.
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.
// 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
URL string `json:"url,omitempty"`
}
// 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"`
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"`
Login *Login `json:"login_url,omitempty"`
}
// 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.URL,
InlineQuery: t.InlineQuery,
InlineQueryChat: t.InlineQueryChat,
Login: t.Login,
Data: data,
}
}
// CallbackUnique returns InlineButton.Unique.
func (t *InlineButton) CallbackUnique() string {
return "\f" + t.Unique
}
// CallbackUnique returns KeyboardButton.Text.
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.
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 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) {
type InlineButtonJSON InlineButton
if t.Login != nil {
return json.Marshal(struct {
InlineButtonJSON
InlineQueryChat string `json:"switch_inline_query_current_chat,omitempty"`
}{
InlineButtonJSON: InlineButtonJSON(*t),
})
}
return json.Marshal(InlineButtonJSON(*t))
}