Home python Help Add Keyboard to VK bot

Help Add Keyboard to VK bot

Author

Date

Category

Here is my code, in no way you get add keyboard

from vk_api.bot_longpoll import vkbotlongpoll, vkboteventtype
from vk_api.utils import get_random_id
from vk_api.keyboard import vkkeyboard, vkkeyboardcolor
From vk_api.upload Import vkupload
Import Config AS CFG
Import OS, VK_API, JSON
Import Requests.
vk_session = vk_api
vk_session = vk_api.vkapi (token = cfg.token)
LongPoll = VKBOTLONGPOLL (VK_SESSION, CFG.Group_ID)
DEF Send_Message (User_ID, MSG, Attachments):
  vk_session.method ('messages.send', {'user_id': user_id,
                    'Random_ID': get_random_id (),
                    'Message': MSG,
                    'Attachment': attachments,
                    'Keyboard': none})
DEF ON_MESSAGE (User_ID, MSG):
  Sended = false
  For x in Range (1):
    If Sended == True:
      Break
    IF "Hi" == MSG:
      Send_Message (User_ID, "Hi", None)
      Sended = True.
    ELIF "Schedule" == MSG:
      OS.System (R'schedule.py ')
      Upload = vk_api.vkupload (vk_session)
      photo = upload.photo_messages (photos = "images / 1.jpg") [0]
      attachments = []
      attachments.append ('Photo {} _ {}'. Format (Photo ['Owner_ID'], photo ['id']))
      If os.path.exists (R'images \ 1.jpg '):
        Send_Message (User_ID, "", attachments)
        Path = R'images \ 1.jpg '
        OS.Remove (Path)
      Sended = True.
      Return Attachments.
  IF Sended == False:
    Send_Message (User_ID, CFG.Wrong_Text, None)
For event in longpoll.listen ():
  If event.Type == vkboteventtype.Message_New:
    ON_MESSAGE (event.obj.from_id, event.obj.text)
  ELIF EVENT.TYPE == vkboteventtype.group_join:
    Send_Message (event.obj.user_id, CFG.Welcome_Text, None)
  ELIF EVENT.TYPE == vkboteventtype.group_leave:
    Send_Message (event.obj.user_id, CFG.Leaving_Text, None)

Answer 1

In principle, I can show you how it prescribed it, then you can already depact for myself, also advise you not to use it in conversations

Def Create_Keyboard ():
  Keyboard = vk_api.Keyboard.vkkeyboard (one_time = false)
  #FALSE If the keyboard should remain open after pressing the button
  #True if she should go
  keyboard.add_button ("close", color = vk_api.keyboard.vkkeyboardcolor.default)
  keyboard.add_button ("Button", color = vk_api.keyboard.vkkeyboardcolor.positive)
  keyboard.add_line () # refers to adding a new string
  keyboard.add_button ("Button", color = vk_api.keyboard.vkkeyboardcolor.negative)
  Keyboard.add_Line ()
  Keyboard.add_Button ("Button", color = vk_api.keyboard.vkkeyboardcolor.primary)
  Keyboard.add_Button ("Button", color = vk_api.keyboard.vkkeyboardcolor.primary)
  Return Keyboard.Get_Keyboard ()
  # Returns keyboard
def create_empty_keyboard ():
  keyboard = vk_api.keyboard.vkkeyboard.get_empty_keyboard ()
  Return Keyboard.
  # This feature is used to close the keyboard

We register the

method

for event in longpoll.listen ():
  If event.Type == vk_api.bot_longpoll.vkboteventtype.message_new:
    Try:
      Print (event.obj.from_id)
      Print ("Message Text:" + Str (event.obj.text or event.message.Text))
      Print ('*' * 30)
      response = event.obj.text.casefold () or event.message.text. Casefold ()
      # Not sure about event.message.text
      Keyboard = Create_Keyboard ()
      Empty_Keyboard = Create_Empty_Keyboard ()

After that, we prescribe a condition, I prescribe it like this

if event.from_user:
      IF Response == "Hi": 
API.Messages.send (peer_id = event.obj.peer_id or event.message.peer_id, Message = "Hello", Keyboard = Keyboard, Random_ID = 0)
       ELIF Response == "Start":
         API.Messages.send (peer_id = event.obj.peer_id or event.message.peer_id, Message = "Begin", keyboard = Keyboard, Random_id = 0)
       ELIF RESPONSE == "Close":
         API.Messages.send (peer_id = event.obj.peer_id or event.message.peer_id, Message = "Close", Keyboard = Empty_Keyboard, Random_ID = 0)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions