Advertisement
Guest User

Untitled

a guest
May 16th, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. #Проектная деятельность
  2. #Бот-автоответчик для социальной сети “ВКонтакте”
  3.  
  4. from time import sleep
  5. import vk_api
  6.  
  7. first_msg_text = '''
  8. Привет! Это бот Давида, моего хозяина сейчас нет у компьютера,
  9. так что можешь задать вопрос мне:
  10. (Введите нужное число)
  11. '''
  12.  
  13. questions = ['1.Где сейчас Давид?','2.Когда он будет?']
  14.  
  15. answers = ['У него миллион долгов в школе, поэтому он пишет уроки по ОБЖ.', 'Когда в три часа ночи закончит, ответит.']
  16.  
  17. goodbye_text = 'Я ответил на все вопросы, поэтому прощаюсь с вами, чтобы задать вопросы заного, напишите "бот".'
  18.  
  19. vk = vk_api.VkApi(login='davidok2001@mail.ru', password='zGOPRw5YyL5')
  20. # vk_api.VkApi(token = 'a02d...e83fd') #Авторизоваться как сообщество
  21. vk.auth()
  22.  
  23. print('Авторизация прошла успешно.')
  24.  
  25. values = {'out': 0, 'count': 1, 'time_offset': 5}
  26.  
  27. def write_msg(user_id, text):
  28.     vk.method('messages.send', {'user_id' : user_id, 'message' : text})
  29.  
  30. user_messages = {}
  31.  
  32. while True:
  33.     response = vk.method('messages.get', values)
  34.  
  35.     #print(response)
  36.  
  37.     for item in response['items']:
  38.         if item['title'] == '':
  39.             if item['user_id'] not in user_messages:
  40.                 user_messages[item['user_id']] = [0] * len(answers)
  41.                 write_msg(item['user_id'], first_msg_text + '\n'.join(questions))
  42.             elif sum(user_messages[item['user_id']]) == 0 and item['body'].lower() == 'бот':
  43.                 write_msg(item['user_id'], first_msg_text + '\n'.join(questions))
  44.             elif sum(user_messages[item['user_id']]) == len(answers):
  45.                 write_msg(item['user_id'], goodbye_text)
  46.                 user_messages[item['user_id']] = [0] * len(answers)
  47.             else:
  48.                 try:
  49.                     write_msg(item['user_id'], answers[int(item['body']) - 1])
  50.                     user_messages[item['user_id']][int(item['body']) - 1] = 1
  51.                 except:
  52.                     None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement