Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. import vk_api as vk
  2. from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
  3. import wikipedia as wiki
  4. from random import randint
  5.  
  6. # vktoken = input("Введите ваш токен:")
  7. vktoken = "37dae4366219f68f90186905ce81d7be0c997ecbccd54f76c00ecb9173ba7" \
  8.             "26bbb66149ccd01eac17757a"
  9. # group = input("ваш id группы")
  10. group = 187423793
  11. wvars = ["wiki","wp","вики","вп"]
  12. wmodessearch = ["search", "поиск"]
  13. wmodesrandom = ["random", "рандом", "случайная_статья"]
  14. vksession = vk.VkApi(token=vktoken)
  15. vksession._auth_token()
  16. vapi = vksession.get_api()
  17. lpoll = VkBotLongPoll(vksession, group_id=group)
  18.  
  19. print("ready")
  20.  
  21.  
  22. def send(msg, cid):
  23.     vapi.messages.send(users_id=cid, message=str(msg),
  24.                        random_id=randint(0, 18446744073709551615))
  25.  
  26. def send_chat(msg, cid):
  27.     vapi.messages.send(chat_id=cid, message=str(msg),
  28.                        random_id=randint(0, 18446744073709551615))
  29.  
  30.  
  31. wiki.set_lang("ru")
  32. while True:
  33.     for event in lpoll.listen():
  34.         if event.type == VkBotEventType.MESSAGE_NEW:
  35.             msg_text = event.object.text
  36.             # command, mode, text  = event.object.msg_text.split("!")
  37.             if msg_text[0] == "!":
  38.                 msg_text = msg_text[1:]
  39.                 raw = msg_text.split(" ")
  40.                 if len(raw) >= 3:
  41.                     command = raw[0]
  42.                     mode = raw[1]
  43.                     raw = raw[2:]
  44.                     text = ""
  45.                     for item in raw:
  46.                         text += item + " "
  47.                 elif len(raw) == 2:
  48.                     command = raw[0]
  49.                     mode = raw[1]
  50.                 else:
  51.                     continue
  52.  
  53.                 print(command, ":", mode)
  54.  
  55.                 if command in wvars:
  56.                     print('command')
  57.                     if mode.lower() in wmodessearch:
  58.                         print("search")
  59.                         if event.from_user:
  60.                             try:
  61.                                 send(wiki.summary(text), event.user_id)
  62.                             except wiki.exceptions.DisambiguationError as e:
  63.                                 send(str(e), event.user_id)
  64.                             except wiki.exceptions.PageError as e:
  65.                                 send(e, event.user_id)
  66.  
  67.                         elif event.from_chat:
  68.                             try:
  69.                                 send_chat(wiki.summary(text), event.chat_id)
  70.                             except wiki.exceptions.DisambiguationError as e:
  71.                                 send_chat(str(e), event.chat_id)
  72.                             except wiki.exceptions.PageError as e:
  73.                                 send_chat(e, event.chat_id)
  74.  
  75.                     elif mode.lower() in wmodesrandom:
  76.                         print('random')
  77.                         if event.from_user:
  78.                             send(wiki.random(1), event.user_id)
  79.                         elif event.from_chat:
  80.                             send_chat(wiki.random(1), event.chat_id)
  81.  
  82.         elif event.type == VkBotEventType.AUDIO_NEW:
  83.             if event.from_user:
  84.                 send("Кто шлет голосовые - тот виндузятник", event.user_id)
  85.             elif event.from_chat:
  86.                 send_chat("Кто шлет голосовые - тот виндузятник", event.chat_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement