Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import time
  3. import random
  4. import requests
  5.  
  6. import vk_api
  7. from vk_api.longpoll import VkLongPoll, VkEventType
  8.  
  9.  
  10.  
  11. def main():
  12. """ Пример использования longpoll
  13. https://vk.com/dev/using_longpoll
  14. https://vk.com/dev/using_longpoll_2
  15. """
  16.  
  17. login, password = '', ''
  18. vk_session = vk_api.VkApi(login, password)
  19.  
  20.  
  21. try:
  22. vk_session.auth()
  23. except vk_api.AuthError as error_msg:
  24. print(error_msg)
  25. return
  26.  
  27.  
  28.  
  29.  
  30. vk = vk_session.get_api()
  31.  
  32. longpoll = VkLongPoll(vk_session)
  33.  
  34. img_razm = [2560, 1280, 807, 604, 130, 75]
  35. def get_image(msg_id):
  36. img = 'https://vk.com/photo{}_{}'.format(img_razm[0]['owner_id'], img_razm[0]['id'])
  37. for i in img_razm:
  38. if img.get('photo_'+str(img_razm[i])):
  39. return img.get('photo_'+str(img_razm[i]))
  40. else:
  41. print('error')
  42.  
  43. def save_img(url):
  44. p = requests.get(url)
  45. out = open("tmp.png", "wb")
  46. out.write(p.content)
  47. out.close()
  48.  
  49. for event in longpoll.listen():
  50.  
  51. if event.type == VkEventType.MESSAGE_NEW:
  52. print('Новое сообщение:')
  53.  
  54. if event.from_me:
  55. print('От меня для: ', end='')
  56. elif event.to_me:
  57. print('Для меня от: ', end='')
  58.  
  59. if event.from_user:
  60. print(event.user_id)
  61. elif event.from_chat:
  62. print(event.user_id, 'в беседе', event.chat_id)
  63. elif event.from_group:
  64. print('группы', event.group_id)
  65.  
  66. print('Текст: ', event.text)
  67.  
  68. if "помощь" in event.text:
  69. if event.from_user:
  70. vk.messages.send(user_id=event.user_id, message='/me помогает')
  71. elif event.from_chat:
  72. vk.messages.send(chat_id=event.chat_id, message='/me помогает')
  73.  
  74. if event.text == '!фото':
  75. if event.from_user:
  76. vk.messages.send(user_id=event.user_id, message='Скоро будет')
  77. elif event.from_chat:
  78. vk.messages.send(chat_id=event.chat_id, message='Скоро будет')
  79. print(event.message_id)
  80. print('event.message_id event.message_id event.message_id')
  81. save_img(get_image(event.message_id))
  82.  
  83.  
  84.  
  85.  
  86. elif event.type == VkEventType.USER_TYPING:
  87. print('Печатает ', end='')
  88.  
  89. if event.from_user:
  90. print(event.user_id)
  91. elif event.from_group:
  92. print('администратор группы', event.group_id)
  93.  
  94. elif event.type == VkEventType.USER_TYPING_IN_CHAT:
  95. print('Печатает ', event.user_id, 'в беседе', event.chat_id)
  96.  
  97. elif event.type == VkEventType.USER_ONLINE:
  98. print('Пользователь', event.user_id, 'онлайн', event.platform)
  99.  
  100. elif event.type == VkEventType.USER_OFFLINE:
  101. print('Пользователь', event.user_id, 'оффлайн', event.offline_type)
  102.  
  103. else:
  104. print(event.type, event.raw[1:])
  105.  
  106. if __name__ == '__main__':
  107. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement