#
#
#yamynginx2020-01-29 01:47:51
PHP
#yamynginx, 2020-01-29 01:47:51

How to generate JSON for keyboard VK API?

Hello. I want to make a keyboard to send it to the user.
Here is the JSON that should be returned:

spoiler

{
    "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"
            }
        ]
    ]
}



These blocks are inserted to the full width of the screen, because they come as different arrays:
spoiler
[{
            "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"
            }
        }]

It doesn't matter to me how it will all be: one button in full width, or a grid. I have already spent 4 hours building this JSON. I can't do anything at all.
Here's how I did it:
spoiler
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);
    }

Here's how I call it:
spoiler
$vk->sendMessage([
           'user_ids' => $app->array['object']['message']['from_id'],
           'message' => 'Привет!',
           'keyboard' => $vk->generateButtons([
               [
                   'type' => 'text',
                   'payload' => ['button' => 'btn_1'],
                   'label' => 'Кнопочка',
                   'color' => 'secondary',
               ]
           ]),
       ]);


Here is the JSON I get:
spoiler
{
  "one_time": true,
  "inline": false,
  "buttons": [
    {
      "action": {
        "type": "text",
        "payload": "{\"button\":\"btn_1\"}",
        "label": "Привет"
      },
      "color": "secondary"
    }
  ]
}

And this is one of the thousands of results that I did. All wrong.
I will be very (!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!) grateful for the help ... I just don't know how to do it right anymore, there have never been any problems with parsing JSON from the VK API, but to generate it according to their criteria...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2020-01-29
@muxui

{
  "one_time": true,
  "inline": false,
  "buttons": [    <------- а где ещё массив?
    {
      "action": {
        "type": "text",
        "payload": "{\"button\":\"btn_1\"}",
        "label": "Привет"
      },
      "color": "secondary"
    }
  ]
}

Each element of the button must be inside the array - layer, and each layer inside the buttons array
. buttons is an array of layers of buttons (levels, lines), each containing an array of buttons.
Total:
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);
    }

Result:
{
    "one_time": true,
    "inline": false,
    "buttons": [
        [
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\":\"btn_1\"}",
                    "label": "1233123"
                },
                "color": "secondary"
            }
        ]
    ]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question