I
I
Ivan Ivanov2019-12-21 18:20:57
In contact with
Ivan Ivanov, 2019-12-21 18:20:57

Do you get an error when adding buttons in the VK chatbot?

I want to try out the buttons in VK. From the beginning, I made a simple message sending according to the guide:

public function send_message($peer_id,$text){
        $request_params = [
            'message' => $text,
            'peer_id' => $peer_id,
            'access_token' => $this->token,
            'v' => '5.50',
        ];
        $get_params = http_build_query($request_params);
        file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
    }

Everything works, there are no errors, but if I add a keyboard to this array:
$request_params = [
            'message' => $text,
            'peer_id' => $peer_id,
            'access_token' => $this->token,
            'v' => '5.50',
            'keyboard' => [
                'one_time' => false,
                'buttons' => [
                    [
                        'action' => [
                            'type' => 'text',
                            'payload' => '{\"button\": \"1\"}',
                            'label' => 'negative'
                        ],
                        'color' => 'negative'
                    ]
                ]
            ]
        ];

That does not work, and php swears at file_get_contents();
[2019-12-21 15:09:02] local.ERROR: file_get_contents( https://api.vk.com/method/messages.send?{ "message":"321","peer_id":433199225," access_token":"56ec33373a94029f6d42279957c1aa8c76bec4fd0fd4cc822abc9f8cd54f7b96a213600c4d89c22042cd6","v":"5.50","keyboardtype":{"one_time":false,"buttons":{"{"action"", {\\\"button\\\": \\\"1\\\"}","label":"negative"},"color":"negative"}]}}): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
{"exception":"[object] (ErrorException(code: 0):"message": "321", "peer_id": 433199225, "access_token": "56ec33373a940rew111329f6d423131279957c1aa8c31376bec4fd0f3131d4cc822abc9f8cd54f7b96a213600c4d89c22042cd6" , "v": "5.50", "keyboard": { "one_time": false, "buttons": [{ "action" :{"type":"text","payload":"{\\\\\\"button\\\\\\": \\\\\\"1\\\\\\"}", "label":"negative"},"color":"negative"}]}}): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
at /home/riimukrg/shop.anonask.ru/app/VKAPI.php:55)
Tried to change http_build_query to json_encode - same problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Habr, 2019-12-21
@GA_Roman

The keyboard only works on version 5.103, you have 5.50

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question