A
A
andrey_levushkin2019-07-12 08:19:30
Python
andrey_levushkin, 2019-07-12 08:19:30

Why don't buttons submit in a vertical list?

I create a keyboard object and send it to the user

def get_button(label, color, payload=""):
    return {
        "action": {
            "type": "text",
            "payload": json.dumps(payload),
            "label": label
        },
        "color": color
    }


keyboard = {
    "one_time": False,
    "buttons": [
        [
            get_button(label="Кнопка 1", color="positive"),
            get_button(label="Кнопка 2", color="negative"),
            get_button(label="Кнопка 3", color="primary"),
            get_button(label="Кнопка 4", color="negative")
        ]
    ]
}
id = "id получателя"
vk.method("messages.send", {"peer_id": id, "message": "Открыто меню!", "keyboard": keyboard,
                            "random_id": random.randint(1, 2147483647)})

Everything is sent normally, a horizontal menu arrives
5d281810d07e5984454484.png
. I'm trying to send the same menu vertically * (each button on a separate line), changing the keyboard object itself in this way:
keyboard = {
    "one_time": False,
    "buttons": [
        [
            [get_button(label="Кнопка 1", color="positive")],
            [get_button(label="Кнопка 2", color="negative")],
            [get_button(label="Кнопка 3", color="primary")],
            [get_button(label="Кнопка 4", color="negative")]
        ]
    ]
}

But an error is thrown:
vk_api.exceptions.ApiError: [911] Keyboard format is invalid: button [0][3] has invalid action

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2019-07-13
@andrey_levushkin

No need to wrap the buttons in another array. Correct structure:

keyboard = {
    'one_time': False,
    'buttons': [
                [get_button(label="Кнопка 1", color="positive")],
                [get_button(label="Кнопка 2", color="negative")],
                [get_button(label="Кнопка 3", color="primary")],
                [get_button(label="Кнопка 4", color="negative")]
    ]
}

A
AlerX, 2019-07-12
@AlerX

In the get_button() function, payload is specified as json.dumps(payload),
to send a vertical menu, just change this parameter to "{\"button\": \"1\"}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question