Answer the question
In order to leave comments, you need to log in
How to move the VK bot buttons down?
The first day on Habré do not judge strictly ...
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
})
Answer the question
In order to leave comments, you need to log in
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)
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 questionAsk a Question
731 491 924 answers to any question