{
"one_time": false,
"buttons": [
[{
"action": {
"type": "location",
"payload": "{\"button\": \"1\"}"
}
}],
[{
"action": {
"type": "open_app",
"app_id": 6979558,
"owner_id": -181108510,
"hash": "sendKeyboard",
"label": "Отправить клавиатуру"
}
}],
[{
"action": {
"type": "vkpay",
"hash": "action=transfer-to-group&group_id=181108510&aid=10"
}
}],
[{
"action": {
"type": "text",
"payload": "{\"button\": \"1\"}",
"label": "Negative"
},
"color": "negative"
},
{
"action": {
"type": "text",
"payload": "{\"button\": \"2\"}",
"label": "Positive"
},
"color": "positive"
},
{
"action": {
"type": "text",
"payload": "{\"button\": \"2\"}",
"label": "Primary"
},
"color": "primary"
},
{
"action": {
"type": "text",
"payload": "{\"button\": \"2\"}",
"label": "Secondary"
},
"color": "secondary"
}
]
]
}
[{
"action": {
"type": "location",
"payload": "{\"button\": \"1\"}"
}
}],
[{
"action": {
"type": "open_app",
"app_id": 6979558,
"owner_id": -181108510,
"hash": "sendKeyboard",
"label": "Отправить клавиатуру"
}
}],
[{
"action": {
"type": "vkpay",
"hash": "action=transfer-to-group&group_id=181108510&aid=10"
}
}]
public function generateButtons(array $buttons, bool $inline = false) : string {
$array = [
'one_time' => !$inline,
'inline' => $inline,
'buttons' => [],
];
foreach ($buttons as $item) {
$array['buttons'][] = [
'action' => [
'type' => $item['type'],
'payload' => json_encode($item['payload'], JSON_UNESCAPED_UNICODE),
'label' => $item['label'],
],
'color' => $item['color'],
];
}
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
$vk->sendMessage([
'user_ids' => $app->array['object']['message']['from_id'],
'message' => 'Привет!',
'keyboard' => $vk->generateButtons([
[
'type' => 'text',
'payload' => ['button' => 'btn_1'],
'label' => 'Кнопочка',
'color' => 'secondary',
]
]),
]);
{
"one_time": true,
"inline": false,
"buttons": [
{
"action": {
"type": "text",
"payload": "{\"button\":\"btn_1\"}",
"label": "Привет"
},
"color": "secondary"
}
]
}
{
"one_time": true,
"inline": false,
"buttons": [ <------- а где ещё массив?
{
"action": {
"type": "text",
"payload": "{\"button\":\"btn_1\"}",
"label": "Привет"
},
"color": "secondary"
}
]
}
public function generateButtons(array $buttons, bool $inline = false) : string {
$array = [
'one_time' => !$inline,
'inline' => $inline,
'buttons' => [],
];
foreach ($buttons as $item) {
$array['buttons'][][] = [ <---- ещё один массив
'action' => [
'type' => $item['type'],
'payload' => json_encode($item['payload'], JSON_UNESCAPED_UNICODE),
'label' => $item['label'],
],
'color' => $item['color'],
];
}
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
{
"one_time": true,
"inline": false,
"buttons": [
[
{
"action": {
"type": "text",
"payload": "{\"button\":\"btn_1\"}",
"label": "1233123"
},
"color": "secondary"
}
]
]
}