Fsoky

Vk BOT of lesson (2) | nobody

Apr 17th, 2021 (edited)
1,179
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 1 0
  1. import vk_api
  2. from vk_api.longpoll import VkLongPoll, VkEventType
  3. from vk_api.keyboard import VkKeyboard, VkKeyboardColor
  4.  
  5. session = vk_api.VkApi(token="TOKEN")
  6.  
  7.  
  8. def send_message(user_id, message, keyboard=None):
  9.     post = {
  10.         "user_id": user_id,
  11.         "message": message,
  12.         "random_id": 0
  13.     }
  14.  
  15.     if keyboard != None:
  16.         post["keyboard"] = keyboard.get_keyboard()
  17.  
  18.     session.method("messages.send", post)
  19.  
  20.  
  21. for event in VkLongPoll(session).listen():
  22.     if event.type == VkEventType.MESSAGE_NEW and event.to_me:
  23.         text = event.text.lower()
  24.         user_id = event.user_id
  25.  
  26.         if text == "start":
  27.             keyboard = VkKeyboard()
  28.             keyboard.add_location_button()
  29.             keyboard.add_line()
  30.  
  31.             buttons = ["blue", "red", "white", "green"]
  32.             button_colors = [VkKeyboardColor.PRIMARY, VkKeyboardColor.NEGATIVE,
  33.                                 VkKeyboardColor.DEFAULT, VkKeyboardColor.POSITIVE]
  34.  
  35.             for btn, btn_color in zip(buttons, button_colors):
  36.                 keyboard.add_button(btn, btn_color)
  37.  
  38.             send_message(user_id, "The first button!", keyboard)
  39.  
  40.         elif text == "blue":
  41.             send_message(user_id, "BLUE")
Add Comment
Please, Sign In to add comment