F
F
Frayl2021-05-07 12:19:43
PHP
Frayl, 2021-05-07 12:19:43

How to solve the problem with sending the keyboard?

There is this code:

public function sendMessage($user_id, $message, $keyboard = null) {
        if (!is_null($keyboard)) {
            return $this->params("messages.send", [
                "message" => $message,
                "peer_id" => $user_id,
                "random_id" => 0,
                "keyboard" => $keyboard
            ]);
        }
        else {
            $this->params("messages.send", [
                "message" => $message,
                "peer_id" => $user_id,
                "random_id" => 0
            ]);
        }
    }

public function buttonText($text, $color, $payload) {
        $button = [
            "action" => [
                "type" => "text",
                "payload" => '{
                    "command": '.$payload.'
                }',
                "label" => $text
            ],
            "color" => $color
        ];
    }
    
    public function getKeyboard($buttons = [], $one_time = false) {
        $keyboard = [
            "one_time" => $one_time,
            "buttons" => [
                $buttons
            ]
        ];
        
        return json_encode($keyboard, JSON_UNESCAPED_UNICODE);
    }


And when creating a keyboard, sending it, the following error occurs:

Keyboard format is invalid: button [0][0] has invalid action

And here is sending the buttons:

$button1 = $request->buttonText("Кнопка 1", "secondary", "button1");
$button2 = $request->buttonText("Кнопка 2", "secondary", "button2");
$keyboard = $request->getKeyboard();
                
$request->sendMessage($user_id, "Message", $keyboard);

//$request is a class interpreter, what make a query to vk api

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question