1212from .utils import sjson_dumps
1313
1414
15-
15+ MAX_BUTTONS_ON_LINE = 5
16+ MAX_DEFAULT_LINES = 10
17+ MAX_INLINE_LINES = 6
1618
1719
1820class VkKeyboardColor (Enum ):
@@ -22,14 +24,15 @@ class VkKeyboardColor(Enum):
2224 PRIMARY = 'primary'
2325
2426 #: Белая
25- DEFAULT = 'default '
27+ SECONDARY = 'secondary '
2628
2729 #: Красная
2830 NEGATIVE = 'negative'
2931
3032 #: Зелёная
3133 POSITIVE = 'positive'
3234
35+
3336class VkKeyboardButton (Enum ):
3437 """ Возможные типы кнопки """
3538
@@ -44,10 +47,12 @@ class VkKeyboardButton(Enum):
4447
4548 #: Кнопка с приложением VK Apps
4649 VKAPPS = "open_app"
47-
50+
4851 #: Кнопка с ссылкой
4952 OPENLINK = "open_link"
5053
54+ #: Callback-кнопка
55+ CALLBACK = "callback"
5156
5257
5358class VkKeyboard (object ):
@@ -82,9 +87,9 @@ def get_empty_keyboard(cls):
8287 keyboard .keyboard ['buttons' ] = []
8388 return keyboard .get_keyboard ()
8489
85- def add_button (self , label , color = VkKeyboardColor .DEFAULT , payload = None ):
90+ def add_button (self , label , color = VkKeyboardColor .SECONDARY , payload = None ):
8691 """ Добавить кнопку с текстом.
87- Максимальное количество кнопок на строке - 4
92+ Максимальное количество кнопок на строке - MAX_BUTTONS_ON_LINE
8893
8994 :param label: Надпись на кнопке и текст, отправляющийся при её нажатии.
9095 :type label: str
@@ -96,8 +101,8 @@ def add_button(self, label, color=VkKeyboardColor.DEFAULT, payload=None):
96101
97102 current_line = self .lines [- 1 ]
98103
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' )
101106
102107 color_value = color
103108
@@ -118,6 +123,42 @@ def add_button(self, label, color=VkKeyboardColor.DEFAULT, payload=None):
118123 }
119124 })
120125
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+
121162 def add_location_button (self , payload = None ):
122163 """ Добавить кнопку с местоположением.
123164 Всегда занимает всю ширину линии.
@@ -130,8 +171,8 @@ def add_location_button(self, payload=None):
130171
131172 if len (current_line ) != 0 :
132173 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+ )
135176
136177 if payload is not None and not isinstance (payload , six .string_types ):
137178 payload = sjson_dumps (payload )
@@ -160,8 +201,8 @@ def add_vkpay_button(self, hash, payload=None):
160201
161202 if len (current_line ) != 0 :
162203 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+ )
165206
166207 if payload is not None and not isinstance (payload , six .string_types ):
167208 payload = sjson_dumps (payload )
@@ -198,8 +239,8 @@ def add_vkapps_button(self, app_id, owner_id, label, hash, payload=None):
198239
199240 if len (current_line ) != 0 :
200241 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+ )
203244
204245 if payload is not None and not isinstance (payload , six .string_types ):
205246 payload = sjson_dumps (payload )
@@ -216,10 +257,10 @@ def add_vkapps_button(self, app_id, owner_id, label, hash, payload=None):
216257 'hash' : hash
217258 }
218259 })
219-
260+
220261 def add_openlink_button (self , label , link , payload = None ):
221262 """ Добавить кнопку с ссылкой
222- Максимальное количество кнопок на строке - 4
263+ Максимальное количество кнопок на строке - MAX_BUTTONS_ON_LINE
223264
224265 :param label: Надпись на кнопке
225266 :type label: str
@@ -230,8 +271,8 @@ def add_openlink_button(self, label, link, payload=None):
230271 """
231272 current_line = self .lines [- 1 ]
232273
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' )
235276
236277 if payload is not None and not isinstance (payload , six .string_types ):
237278 payload = sjson_dumps (payload )
@@ -241,19 +282,23 @@ def add_openlink_button(self, label, link, payload=None):
241282 current_line .append ({
242283 'action' : {
243284 'type' : button_type ,
244- 'link' : link ,
285+ 'link' : link ,
245286 'label' : label ,
246287 'payload' : payload
247288 }
248289 })
249-
250290
251291 def add_line (self ):
252292 """ Создаёт новую строку, на которой можно размещать кнопки.
253- Максимальное количество строк - 10.
293+ Максимальное количество строк:
294+ Стандартное отображение - MAX_DEFAULT_LINES;
295+ Inline-отображение - MAX_INLINE_LINES.
254296 """
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' )
258303
259304 self .lines .append ([])
0 commit comments