A
A
Artem Chernousov2020-01-02 01:52:16
Keyboard
Artem Chernousov, 2020-01-02 01:52:16

How to move the VK bot buttons down?

The first day on Habré do not judge strictly ...

spoiler
А еще мне 12 лет)))000)

Well, here's how to move the keyboard shortcut buttons down.
Python code
import vk_api
from vk_api.bot_longpoll import VkBotEventType, VkBotLongPoll
import json
vk = vk_api.VkApi(token="токен")
 
vk._auth_token()
 
vk.get_api()
 
keyboard = {
    "one_time": False,
    "buttons": [
        [{
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"1\"}",
                    "label": "верхняя левая кнопка"
                },
                "color": "negative"
            },
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"1\"}",
                    "label": "верхняя правая кнопка"
                },
                "color": "positive"
            },
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"2\"}",
                    "label": "нижняя левая кнопка"
                },
                "color": "primary"
            },
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"3\"}",
                    "label": "Нижняя правая"
                },
                "color": "secondary"
            }
        ]
    ]
}
 
keyboard = json.dumps(keyboard, ensure_ascii=False).encode('utf-8')
keyboard = str(keyboard.decode('utf-8'))
 
longpoll = VkBotLongPoll(vk, айди сообщ-ва)
 
while True:
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW:
            if event.object.text.lower() == "новые кнопки":
                vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "Новые кнопки", "random_id": 0,
                                            "keyboard": keyboard})
            if "Negative" in event.object.text:
                vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "Красная кнопка", "random_id": 0
                                            })
            if "Positive" in event.object.text:
                vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "Зелёная кнопка", "random_id": 0
                                            })
            if "Primary" in event.object.text:
                vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "Синяя кнопка", "random_id": 0
                                            })
            if "Secondary" in event.object.text:
                vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "Обычная кнопка", "random_id": 0
                                            })

What I have in the first photo, an example of how you need it in the second photo.
5e0d2213b994c043048445.png
5e0d2249e835f895212233.png
Ps I'm very dumb, so please write in detail

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2020-01-02
@anerev

Although I didn’t really understand, but krch, vk_api already has a class for working with the keyboard, this is how what you need looks like:

from vk_api.keyboard import VkKeyboard, VkKeyboardColor

keyboard = VkKeyboard(one_time=False)
        keyboard.add_button('нижняя левая кнопка', color=VkKeyboardColor.PRIMARY)
        keyboard.add_button('Нижняя правая', color=VkKeyboardColor.SECONDARY) 
        keyboard.add_line() 
        keyboard.add_button('верхняя левая кнопка', color=VkKeyboardColor.NEGATIVE)
        keyboard.add_button('верхняя правая кнопка', color=VkKeyboardColor.POSITIVE)

Read about this library on github, you use it in a strange way
https://github.com/python273/vk_api/blob/master/ex...
https://github.com/python273/vk_api/tree/master /ex...
https://github.com/python273/vk_api/blob/master/ex...

A
Artem Chernousov, 2020-01-02
@Bruuh

The issue is resolved, that's it, hmm

import vk_api
from vk_api.bot_longpoll import VkBotEventType, VkBotLongPoll
import json
vk = vk_api.VkApi(token="токен")
 
vk._auth_token()
 
vk.get_api()
 
keyboard = {
    "one_time": False,
    "buttons": [
        [{
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"1\"}",
                    "label": "верхняя левая кнопка"
                },
                "color": "negative"
            },
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"1\"}",
                    "label": "верхняя правая кнопка"
                },
                "color": "positive"
            },
],[
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"2\"}",
                    "label": "нижняя левая кнопка"
                },
                "color": "primary"
            },
            {
                "action": {
                    "type": "text",
                    "payload": "{\"button\": \"3\"}",
                    "label": "Нижняя правая"
                },
                "color": "secondary"
            }
        ]
    ]
}
 
keyboard = json.dumps(keyboard, ensure_ascii=False).encode('utf-8')
keyboard = str(keyboard.decode('utf-8'))
 
longpoll = VkBotLongPoll(vk, айди сообщ-ва)
 
while True:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question