forked from SakoDroid/telego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.go
227 lines (178 loc) · 10.8 KB
/
keyboard.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package telego
import (
objs "github.com/SakoDroid/telego/objects"
upp "github.com/SakoDroid/telego/parser"
)
//MarkUps is the interface used for creating normal keyboards and inline keyboards.
type MarkUps interface {
toMarkUp() objs.ReplyMarkup
}
//keyboard is a normal keyboard.
type keyboard struct {
keys [][]*objs.KeyboardButton
resizeKeyBoard, oneTimeKeyboard, selective bool
inputFieldPlaceHolder string
}
func (kb *keyboard) fixRows(row int) {
dif := (row) - len(kb.keys)
for i := 0; i < dif; i++ {
kb.keys = append(kb.keys, make([]*objs.KeyboardButton, 0))
}
}
/*AddButton adds a new button holding the given text to the specified row. According to telegram bot api if this button is pressed the text inside the button will be sent to the bot as a message.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added*/
func (kb *keyboard) AddButton(text string, row int) {
kb.addButton(text, row, false, false, nil, nil)
}
/*AddButtonHandler adds a new button holding the given text to the specified row. This method also adds a handler for that button so everytime this button is pressed the handler will be called. You can read the documentation of "AddHandler" for better understanding on handlers.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added*/
func (kb *keyboard) AddButtonHandler(text string, row int, handler func(*objs.Update), chatTypes ...string) {
kb.addButton(text, row, false, false, nil, nil)
upp.AddHandler(text, handler, chatTypes...)
}
/*AddContactButton adds a new contact button. According to telegram bot api when this button is pressed,the user's phone number will be sent as a contact. Available in private chats only.
Note: ContactButtons and LocationButtons will only work in Telegram versions released after 9 April, 2016. Older clients will display unsupported message.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added*/
func (kb *keyboard) AddContactButton(text string, row int) {
kb.addButton(text, row, true, false, nil, nil)
}
/*AddLocationButton adds a new location button. According to telegram bot api when this button is pressed,the user's location will be sent. Available in private chats only.
Note: ContactButtons and LocationButtons will only work in Telegram versions released after 9 April, 2016. Older clients will display unsupported message.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added*/
func (kb *keyboard) AddLocationButton(text string, row int) {
kb.addButton(text, row, false, true, nil, nil)
}
/*AddPollButton adds a new poll button. According to telegram bot api, the user will be asked to create a poll and send it to the bot when this button is pressed. Available in private chats only.
Note: PollButton will only work in Telegram versions released after 23 January, 2020. Older clients will display unsupported message.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
Note : poll type can be "regular" or "quiz". Any other value will cause the button not to be added.*/
func (kb *keyboard) AddPollButton(text string, row int, pollType string) {
if pollType == "regular" || pollType == "quiz" {
kb.addButton(text, row, false, false, &objs.KeyboardButtonPollType{Type: pollType}, nil)
}
}
/* AddWebAppButton adds a button which opens a web app when it's pressed.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
*/
func (kb *keyboard) AddWebAppButton(text string, row int, url string) {
kb.addButton(text, row, false, false, nil, &objs.WebAppInfo{URL: url})
}
func (kb *keyboard) addButton(text string, row int, contact, location bool, poll *objs.KeyboardButtonPollType, webApp *objs.WebAppInfo) {
if row >= 1 {
kb.fixRows(row)
kb.keys[row-1] = append(kb.keys[row-1], &objs.KeyboardButton{
Text: text,
RequestContact: contact,
RequestLocation: location,
RequestPoll: poll,
WebApp: webApp,
})
}
}
func (kb *keyboard) toMarkUp() objs.ReplyMarkup {
return &objs.ReplyKeyboardMarkup{
Keyboard: kb.keys,
ResizeKeyboard: kb.resizeKeyBoard,
OneTimeKeyboard: kb.oneTimeKeyboard,
InputFieldPlaceholder: kb.inputFieldPlaceHolder,
Selective: kb.selective,
}
}
type inlineKeyboard struct {
keys [][]*objs.InlineKeyboardButton
}
/*AddURLButton adds a button that will open an url when pressed.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added*/
func (in *inlineKeyboard) AddURLButton(text, url string, row int) {
in.addButton(text, url, "", "", "", nil, nil, nil, false, row)
}
/*AddLoginURLButton adds a button that will be used for automatic authorization. According to telegram bot api, login url is an HTTP URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
Arguments :
1. url : An HTTP URL to be opened with user authorization data added to the query string when the button is pressed. If the user refuses to provide authorization data, the original URL without information about the user will be opened. The data added is the same as described in Receiving authorization data. NOTE: You must always check the hash of the received data to verify the authentication and the integrity of the data as described in Checking authorization.
2. forwardText : New text of the button in forwarded messages.
3. botUsername : Username of a bot, which will be used for user authorization. See Setting up a bot for more details. If not specified, the current bot's username will be assumed. The url's domain must be the same as the domain linked with the bot. See Linking your domain to the bot for more details.
4. requestWriteAccess : Pass True to request the permission for your bot to send messages to the user.
*/
func (in *inlineKeyboard) AddLoginURLButton(text, url, forwardText, botUsername string, requestWriteAccess bool, row int) {
in.addButton(text, "", "", "", "", &objs.LoginUrl{
URL: url,
ForwardText: forwardText,
BotUsername: botUsername,
RequestWriteAccess: requestWriteAccess,
}, nil, nil, false, row)
}
/*AddCallbackButton adds a button that when its pressed, a call back query with the given data is sen to the bot
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
*/
func (in *inlineKeyboard) AddCallbackButton(text, callbackData string, row int) {
in.addButton(text, "", callbackData, "", "", nil, nil, nil, false, row)
}
/*AddCallbackButtonHandler adds a button that when its pressed, a call back query with the given data is sen to the bot. A handler is also added which will be called everytime a call back query is received for this button.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
*/
func (in *inlineKeyboard) AddCallbackButtonHandler(text, callbackData string, row int, handler func(*objs.Update)) {
in.addButton(text, "", callbackData, "", "", nil, nil, nil, false, row)
upp.AddCallbackHandler(callbackData, handler)
}
/*AddSwitchInlineQueryButton adds a switch inline query button. According to tlegram bot api, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. Can be empty, in which case just the bot's username will be inserted. Note: This offers an easy way for users to start using your bot in inline mode when they are currently in a private chat with it. Especially useful when combined with switch_pm… actions – in this case the user will be automatically returned to the chat they switched from, skipping the chat selection screen.
Note : If "currentChat" option is true, the inline query will be inserted in the current chat's input field.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
*/
func (in *inlineKeyboard) AddSwitchInlineQueryButton(text, inlineQuery string, row int, currenChat bool) {
if currenChat {
in.addButton(text, "", "", "", inlineQuery, nil, nil, nil, false, row)
} else {
in.addButton(text, "", "", inlineQuery, "", nil, nil, nil, false, row)
}
}
/*AddGameButton adds a game button. Everytime a user presses this button a game will be launched. Use botfather to set up a game.
NOTE: This type of button must always be the first button in the first row.*/
func (in *inlineKeyboard) AddGameButton(text string, row int) {
in.addButton(text, "", "", "", "", nil, &objs.CallbackGame{}, nil, false, row)
}
/*AddPayButton adds a pay button.
NOTE: This type of button must always be the first button in the first row.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
*/
func (in *inlineKeyboard) AddPayButton(text string, row int) {
in.addButton(text, "", "", "", "", nil, nil, nil, true, row)
}
/* AddWebAppButton adds a button which opens a web app when it's pressed.
Note : row number starts from 1. (it's not zero based). If any number lower than 1 is passed, no button will be added.
*/
func (in *inlineKeyboard) AddWebAppButton(text string, row int, url string) {
in.addButton(text, "", "", "", "", nil, nil, &objs.WebAppInfo{URL: url}, false, row)
}
func (in *inlineKeyboard) addButton(text, url, callbackData, switchInlineQuery, switchInlineQueryCurrentChat string, loginUrl *objs.LoginUrl, callbackGame *objs.CallbackGame, webApp *objs.WebAppInfo, pay bool, row int) {
if row >= 1 {
in.fixRows(row)
in.keys[row-1] = append(in.keys[row-1], &objs.InlineKeyboardButton{
Text: text,
URL: url,
LoginURL: loginUrl,
CallbackData: callbackData,
SwitchInlineQuery: switchInlineQuery,
SwitchInlineQueryCurrentChat: switchInlineQueryCurrentChat,
CallbackGame: callbackGame,
Pay: pay,
WebApp: webApp,
})
}
}
func (in *inlineKeyboard) fixRows(row int) {
dif := (row) - len(in.keys)
for i := 0; i < dif; i++ {
in.keys = append(in.keys, make([]*objs.InlineKeyboardButton, 0))
}
}
func (in *inlineKeyboard) toInlineKeyboardMarkup() objs.InlineKeyboardMarkup {
return objs.InlineKeyboardMarkup{
InlineKeyboard: in.keys,
}
}
func (in *inlineKeyboard) toMarkUp() objs.ReplyMarkup {
return &objs.InlineKeyboardMarkup{
InlineKeyboard: in.keys,
}
}