· 6 years ago · Dec 11, 2019, 05:48 PM
1import vk, os, json, time
2from threading import Thread
3from requests import post
4
5TOKEN = ''
6API = '5.80'
7BOT_ID = 186656440
8time = time.time()
9session = vk.Session(access_token = TOKEN)
10vk = vk.API(session, v = API)
11
12# первый запрос к лонгпулу: получение server, key и ts
13long_poll = vk.groups.getLongPollServer(group_id = BOT_ID)
14server, key, ts = long_poll['server'], long_poll['key'], long_poll['ts']
15def addslashes(s):
16 return repr('"' + s)[2:-1].replace('"', '\\"')
17while True:
18 try:
19 long_poll = post('%s'%server, data = {'act': 'a_check', 'key': key, 'ts': ts, 'wait': 90}).json() # запрос к лонгпулу
20 except Exception:
21 quit();
22 updates = long_poll['updates']
23 if updates is None:
24 long_poll = vk.groups.getLongPollServer(group_id = BOT_ID)
25 server, key, ts = long_poll['server'], long_poll['key'], long_poll['ts']
26 if server == null:
27 quit()
28 continue
29 if long_poll['updates'] and len(long_poll['updates']) != 0:
30 for up in long_poll['updates']:
31
32 if ((up['type'] == 'message_new' or up['type'] == 'message_reply') and up['object']['from_id'] > 0) or up['type'] == 'group_join':
33 print(os.system('php main.php "'+addslashes(json.dumps(up['object']))+'" &'))
34 ts = long_poll['ts']