12
12
from .utils import sjson_dumps
13
13
14
14
15
-
15
+ MAX_BUTTONS_ON_LINE = 5
16
+ MAX_DEFAULT_LINES = 10
17
+ MAX_INLINE_LINES = 6
16
18
17
19
18
20
class VkKeyboardColor (Enum ):
@@ -22,14 +24,15 @@ class VkKeyboardColor(Enum):
22
24
PRIMARY = 'primary'
23
25
24
26
#: Белая
25
- DEFAULT = 'default '
27
+ SECONDARY = 'secondary '
26
28
27
29
#: Красная
28
30
NEGATIVE = 'negative'
29
31
30
32
#: Зелёная
31
33
POSITIVE = 'positive'
32
34
35
+
33
36
class VkKeyboardButton (Enum ):
34
37
""" Возможные типы кнопки """
35
38
@@ -44,10 +47,12 @@ class VkKeyboardButton(Enum):
44
47
45
48
#: Кнопка с приложением VK Apps
46
49
VKAPPS = "open_app"
47
-
50
+
48
51
#: Кнопка с ссылкой
49
52
OPENLINK = "open_link"
50
53
54
+ #: Callback-кнопка
55
+ CALLBACK = "callback"
51
56
52
57
53
58
class VkKeyboard (object ):
@@ -82,9 +87,9 @@ def get_empty_keyboard(cls):
82
87
keyboard .keyboard ['buttons' ] = []
83
88
return keyboard .get_keyboard ()
84
89
85
- def add_button (self , label , color = VkKeyboardColor .DEFAULT , payload = None ):
90
+ def add_button (self , label , color = VkKeyboardColor .SECONDARY , payload = None ):
86
91
""" Добавить кнопку с текстом.
87
- Максимальное количество кнопок на строке - 4
92
+ Максимальное количество кнопок на строке - MAX_BUTTONS_ON_LINE
88
93
89
94
:param label: Надпись на кнопке и текст, отправляющийся при её нажатии.
90
95
:type label: str
@@ -96,8 +101,8 @@ def add_button(self, label, color=VkKeyboardColor.DEFAULT, payload=None):
96
101
97
102
current_line = self .lines [- 1 ]
98
103
99
- if len (current_line ) >= 4 :
100
- raise ValueError ('Max 4 buttons on a line' )
104
+ if len (current_line ) >= MAX_BUTTONS_ON_LINE :
105
+ raise ValueError (f 'Max { MAX_BUTTONS_ON_LINE } buttons on a line' )
101
106
102
107
color_value = color
103
108
@@ -118,6 +123,42 @@ def add_button(self, label, color=VkKeyboardColor.DEFAULT, payload=None):
118
123
}
119
124
})
120
125
126
+ def add_callback_button (self , label , color = VkKeyboardColor .SECONDARY , payload = None ):
127
+ """ Добавить callback-кнопку с текстом.
128
+ Максимальное количество кнопок на строке - MAX_BUTTONS_ON_LINE
129
+
130
+ :param label: Надпись на кнопке и текст, отправляющийся при её нажатии.
131
+ :type label: str
132
+ :param color: цвет кнопки.
133
+ :type color: VkKeyboardColor or str
134
+ :param payload: Параметр для callback api
135
+ :type payload: str or list or dict
136
+ """
137
+
138
+ current_line = self .lines [- 1 ]
139
+
140
+ if len (current_line ) >= MAX_BUTTONS_ON_LINE :
141
+ raise ValueError (f'Max { MAX_BUTTONS_ON_LINE } buttons on a line' )
142
+
143
+ color_value = color
144
+
145
+ if isinstance (color , VkKeyboardColor ):
146
+ color_value = color_value .value
147
+
148
+ if payload is not None and not isinstance (payload , six .string_types ):
149
+ payload = sjson_dumps (payload )
150
+
151
+ button_type = VkKeyboardButton .CALLBACK .value
152
+
153
+ current_line .append ({
154
+ 'color' : color_value ,
155
+ 'action' : {
156
+ 'type' : button_type ,
157
+ 'payload' : payload ,
158
+ 'label' : label ,
159
+ }
160
+ })
161
+
121
162
def add_location_button (self , payload = None ):
122
163
""" Добавить кнопку с местоположением.
123
164
Всегда занимает всю ширину линии.
@@ -130,8 +171,8 @@ def add_location_button(self, payload=None):
130
171
131
172
if len (current_line ) != 0 :
132
173
raise ValueError (
133
- 'This type of button takes the entire width of the line'
134
- )
174
+ 'This type of button takes the entire width of the line'
175
+ )
135
176
136
177
if payload is not None and not isinstance (payload , six .string_types ):
137
178
payload = sjson_dumps (payload )
@@ -160,8 +201,8 @@ def add_vkpay_button(self, hash, payload=None):
160
201
161
202
if len (current_line ) != 0 :
162
203
raise ValueError (
163
- 'This type of button takes the entire width of the line'
164
- )
204
+ 'This type of button takes the entire width of the line'
205
+ )
165
206
166
207
if payload is not None and not isinstance (payload , six .string_types ):
167
208
payload = sjson_dumps (payload )
@@ -198,8 +239,8 @@ def add_vkapps_button(self, app_id, owner_id, label, hash, payload=None):
198
239
199
240
if len (current_line ) != 0 :
200
241
raise ValueError (
201
- 'This type of button takes the entire width of the line'
202
- )
242
+ 'This type of button takes the entire width of the line'
243
+ )
203
244
204
245
if payload is not None and not isinstance (payload , six .string_types ):
205
246
payload = sjson_dumps (payload )
@@ -216,10 +257,10 @@ def add_vkapps_button(self, app_id, owner_id, label, hash, payload=None):
216
257
'hash' : hash
217
258
}
218
259
})
219
-
260
+
220
261
def add_openlink_button (self , label , link , payload = None ):
221
262
""" Добавить кнопку с ссылкой
222
- Максимальное количество кнопок на строке - 4
263
+ Максимальное количество кнопок на строке - MAX_BUTTONS_ON_LINE
223
264
224
265
:param label: Надпись на кнопке
225
266
:type label: str
@@ -230,8 +271,8 @@ def add_openlink_button(self, label, link, payload=None):
230
271
"""
231
272
current_line = self .lines [- 1 ]
232
273
233
- if len (current_line ) >= 4 :
234
- raise ValueError ('Max 4 buttons on a line' )
274
+ if len (current_line ) >= MAX_BUTTONS_ON_LINE :
275
+ raise ValueError (f 'Max { MAX_BUTTONS_ON_LINE } buttons on a line' )
235
276
236
277
if payload is not None and not isinstance (payload , six .string_types ):
237
278
payload = sjson_dumps (payload )
@@ -241,19 +282,23 @@ def add_openlink_button(self, label, link, payload=None):
241
282
current_line .append ({
242
283
'action' : {
243
284
'type' : button_type ,
244
- 'link' : link ,
285
+ 'link' : link ,
245
286
'label' : label ,
246
287
'payload' : payload
247
288
}
248
289
})
249
-
250
290
251
291
def add_line (self ):
252
292
""" Создаёт новую строку, на которой можно размещать кнопки.
253
- Максимальное количество строк - 10.
293
+ Максимальное количество строк:
294
+ Стандартное отображение - MAX_DEFAULT_LINES;
295
+ Inline-отображение - MAX_INLINE_LINES.
254
296
"""
255
-
256
- if len (self .lines ) >= 10 :
257
- raise ValueError ('Max 10 lines' )
297
+ if self .inline :
298
+ if len (self .lines ) >= MAX_INLINE_LINES :
299
+ raise ValueError (f'Max { MAX_INLINE_LINES } lines for inline keyboard' )
300
+ else :
301
+ if len (self .lines ) >= MAX_DEFAULT_LINES :
302
+ raise ValueError (f'Max { MAX_DEFAULT_LINES } lines for default keyboard' )
258
303
259
304
self .lines .append ([])
0 commit comments