Hello, I found the code in the Internet through which you can add a keyboard in the LS group of VKontakte. For some reason, this code is started without errors, but in LAN Bota after sending a message with the text ‘Keyboard’ keyboard does not appear.
import vk_api
Import Time.
Import json.
Token = "Token"
vk = vk_api.vkapi (token = token)
vk._auth_token ()
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 = "Button 1", color = "POSITIVE"),
get_button (label = "button 2", color = "negative"),
get_button (label = "button 3", color = "primary"),
Get_Button (Label = "Button 4", color = "default")
]
}
Keyboard = JSON.DUMPS (Keyboard, Ensure_Ascii = False) .Encode ('UTF-8')
Keyboard = STR (utf-8 '))
While True:
Try:
Messages = VK.Method ("Messages.getConversations", {"Offset": 0, "Count": 20})
If Messages ["Count"] & gt; = 1:
id = Messages ["Items"] [0] ["Last_Message"] ["FROM_ID"]
Body = Messages ["Items"] [0] ["Last_Message"] ["Text"]
IF Body == "Hi":
vk.method ("Messages.send", {"peer_id": ID, "Message": "Hi friend"})
ELIF Body == "Keyboard":
VK.Method ("Messages.send", {"peer_id": ID, "Message": "Select the button!", "Keyboard": Keyboard})
ELIF Body == "Button 1":
vk.method ("Messages.send", {"peer_id": ID, "Message": Body})
ELIF Body == "Button 2":
vk.method ("Messages.send", {"peer_id": ID, "Message": Body})
ELIF Body == "Button 3":
vk.method ("Messages.send", {"peer_id": ID, "Message": Body})
Elif Body == "Button 4":
vk.method ("Messages.send", {"peer_id": ID, "Message": Body})
ELIF BODY == "Start":
vk.method ("Messages.send", {"peer_id": ID, "Message": "Let's start"})
ELSE:
vk.method ("Messages.send", {"peer_id": ID, "Message": "I did not understand you!"})
Time.Sleep (0.5)
Except Exception AS E:
Time.Sleep (1)
Answer 1, Authority 100%
In the library VK_API
already have methods that allow you to work with the keyboard. Do not invent a bike
from vk_api.keyboard import vkkeyboard, vkkeyboardcolor
Keyboard = VKKeyboard (one_time = false)
keyboard.add_button (Label = "Button 1", color = vkkeyboardcolor.positive) # Green
keyboard.add_line () # The following buttons will be under this
keyboard.add_button (label = "button 2", color = vkkeyboardcolor.primary) # blue color
Keyboard.add_Button (Label = "Button 3", color = vkkeyboardcolor.default) # white color
# ...
vk.method ("Messages.send",
{
"peer_id": id
"Message": "Select a button!",
"Keyboard": keyboard.Get_Keyboard () Returns JSON of our keyboard
}
)
If you want in your own way, then:
- First, replace the value of the
PAYLOAD
parameter by default
none
, because empty string andnone
are different things.
def get_button (label, color, payload = none):
- Second, the
buttons
field must contain List of lists , where the first level list is rows, and the second-level lists are the buttons in the row.
Keyboard = {
"One_time": False,
"Buttons": [
[# These buttons will be in the first row
Get_Button (Label = "Button 1", color = "POSITIVE"),
get_button (label = "button 2", color = "negative"),
Get_Button (Label = "Button 3", Color = "Primary")
],
[# This button will be in the second row
Get_Button (Label = "Button 4", color = "default")
]
]
}
Answer 2
maybe it will help the replacement of the string:
body = messages ["Items"] [0] ["Last_Message"] ["text"]
Next:
body = messages ["Items"] [0] ["Last_Message"] ["text"]. Lower ( )