Advertisement
Fsoky

Vk Bot of lesson (3) | Carousel and new keyboard

Aug 13th, 2021
2,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import vk_api
  2. from vk_api.longpoll import VkLongPoll, VkEventType
  3. from config import token
  4. from vktools import Keyboard, KeyboardButton, Carousel, CarouselButton
  5.  
  6. session = vk_api.VkApi(token=token)
  7.  
  8.  
  9. def send_message(user_id, message, keyboard=None, carousel=None):
  10.     post = {
  11.         "user_id": user_id,
  12.         "message": message,
  13.         "random_id": 0
  14.     }
  15.  
  16.     if keyboard != None:
  17.         post["keyboard"] = keyboard.add_keyboard()
  18.     if carousel != None:
  19.         post["template"] = carousel.add_carousel()
  20.  
  21.     session.method("messages.send", post)
  22.  
  23.  
  24. for event in VkLongPoll(session).listen():
  25.     if event.type == VkEventType.MESSAGE_NEW and event.to_me:
  26.         text = event.text.lower()
  27.         user_id = event.user_id
  28.  
  29.         if text == "start":
  30.  
  31.             keyboard = Keyboard(
  32.                 [
  33.                     [
  34.                         KeyboardButton().text("RED", "negative"),
  35.                         KeyboardButton().text("GREEN", "positive"),
  36.                         KeyboardButton().text("BLUE", "primary"),
  37.                         KeyboardButton().text("WHITE")
  38.                     ],
  39.                     [
  40.                         KeyboardButton().openlink("YouTube", "https://youtube.com/c/Фсоки")
  41.                     ],
  42.                     [
  43.                         KeyboardButton().location()
  44.                     ]
  45.                 ]
  46.             )
  47.  
  48.             send_message(user_id, "New Keyboard", keyboard)
  49.  
  50.         elif text == "test":
  51.             carousel = Carousel(
  52.                 [
  53.                     CarouselButton().openlink(
  54.                         [
  55.                             CarouselButton().element(
  56.                                 title="Title 1",
  57.                                 description="Description 1",
  58.                                 photo_id="-203980592_457239030",
  59.                                 link="https://vk.com/fsoky",
  60.                                 buttons=[KeyboardButton().text("Test button", "primary")]
  61.                             ),
  62.                             CarouselButton().element(
  63.                                 title="Title 2",
  64.                                 description="Description 2",
  65.                                 photo_id="-203980592_457239030",
  66.                                 link="https://vk.com/fsoky",
  67.                                 buttons=[KeyboardButton().text("Test button 2", "negative")]
  68.                             ),
  69.                         ]
  70.                     )
  71.                 ]
  72.             )
  73.  
  74.             send_message(user_id, "Carousel", carousel=carousel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement