Advertisement
stuppid_bot

Бот-автоответчик

Dec 5th, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import utils
  3. import account1
  4. import urllib
  5.  
  6. c = utils.create_client()
  7. c.auth(account1.login, account1.password)
  8. print c.user_id
  9. # c.api('wall.post', {'message': u'тест'})
  10.  
  11. # список всех кто онлайн получаем
  12. users_online = c.api('friends.getOnline')
  13. print users_online
  14. info = c.api('messages.getLongPollServer', {'use_ssl': 1})
  15. ts = info['ts']
  16. key = info['key']
  17. server = info['server']
  18. wait_time = 25
  19. import json
  20. import random
  21. defaults = [u'тю', u'азаза', u'иди нахуй']
  22. while True:
  23.     # устанавливаем онлайн
  24.     print c.api('account.setOnline')
  25.     url = 'https://%s?act=a_check&key=%s&ts=%s&wait=%s&mode=2' % (server, key, ts, wait_time)
  26.     # print url
  27.     info = c.fetch_json(url)
  28.     # подробнее тут
  29.     # https://vk.com/pages?oid=-1&p=%D0%9F%D0%BE%D0%B4%D0%BA%D0%BB%D1%8E%D1%87%D0%B5%D0%BD%D0%B8%D0%B5_%D0%BA_LongPoll_%D1%81%D0%B5%D1%80%D0%B2%D0%B5%D1%80%D1%83
  30.     print info
  31.     ts = info['ts']
  32.     if len(info['updates']):
  33.         for v in info['updates']:
  34.             print v
  35.             # отвечаем на личные сообщения
  36.             # v[2] - флаги. Флаг 2 означает что сообщение исходящее, т.е. мы ищем только входящие
  37.             if v[0] == 4 and v[2] & 2 == 0:
  38.                 from_id = v[3]
  39.                 message = v[6]
  40.                 # отправляем запрос на xu.su
  41.                 res = urllib.urlopen('http://xu.su/send.php', 'bot=1&text=' + urllib.quote(message.encode('utf8')))
  42.                 data = res.read()
  43.                 result = json.loads(data)
  44.                 message = result['text'] if 'text' in result else random.choice(defaults)
  45.                 try:
  46.                     c.api('messages.send', {'user_id': from_id, 'message': message})
  47.                 except Exception, e:
  48.                     print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement