-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.py
executable file
·211 lines (165 loc) · 5.9 KB
/
connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
import threading, logging, time
import multiprocessing
import requests
import random
import json
import re
import uuid
from configparser import ConfigParser
from datetime import datetime
from kafka import KafkaConsumer, KafkaProducer
API_VERSION = '5.65'
class VkListener(threading.Thread):
daemon = True
def run(self):
ts, server, key = self.getTsServerKey()
while True:
try:
url = "http://%s?act=a_check&key=%s&ts=%s&wait=25&mode=2&version=0" %(server, key, ts)
res = requests.get(url)
data = json.loads(res.text)
if 'error' in data:
print(data)
ts=data['ts']
for update in data['updates']:
if(update[0]==4):
if(update[6]!=''):
if(update[6][0]=='!' or update[6][0]=='/'):
t = threading.Thread(target=self.handler, args=(update))
t.start()
except ValueError:
logging.error('ValueError')
pass
except KeyError:
logging.error('KeyError')
print("KeyError")
ts, server, key = self.getTsServerKey()
pass
except requests.exceptions.ConnectionError as e:
print(datetime.now())
print(e)
logging.error('ConnectionError')
time.sleep(10)
ts, server, key = self.getTsServerKey()
pass
except Exception as e:
print("exception")
logging.exception("Exception")
pass
def getTsServerKey(self):
print("getTsServerKey")
url = u"https://api.vk.com/method/messages.getLongPollServer?use_ssl=1&v=5.44&need_pts=0&access_token="+ token
try:
res = requests.get(url)
data = json.loads( res.text )
server=data['response']['server']
ts=data['response']['ts']
key=data['response']['key']
print(ts)
return ts, server, key
except requests.exceptions.ConnectionError as e:
print(datetime.now())
print(e)
logging.error('Another ConnectionError')
time.sleep(20)
ts, server, key = self.getTsServerKey()
return ts, server, key
except Exception as e:
print(datetime.now())
print(e)
logging.exception("Exception in getTsServerKey")
ts, server, key = self.getTsServerKey()
return ts, server, key
def handler(self, *args):
code = args[0]
message_id = args[1]
flags = args[2]
from_id = args[3]
timestamp = args[4]
subject = args[5]
text = args[6]
attachments = args[7]
if('from' in attachments):
user_id = int(attachments['from'])
else:
user_id = from_id
space = text.find(' ')
if space == -1:
keyword = text[1:]
else:
keyword = text[1:space]
args = text.split()[1:]
message_json = {}
message_json['connector-id'] = 'vk'
message_json['command'] = keyword
message_json['params'] = args
producer = KafkaProducer(bootstrap_servers=kafka_address+':'+kafka_port)
key = str.encode(str(uuid.uuid4()))
producer.send('to-module', str.encode(json.dumps(message_json)), key=key)
global messages_dict
messages_dict[key] = from_id
time.sleep(20)
class VkSender(threading.Thread):
daemon = True
def run(self):
consumer = KafkaConsumer(bootstrap_servers=kafka_address+':'+kafka_port, group_id='vk-connector')
consumer.subscribe(['to-connector'])
for message in consumer:
key = message.key
message_string = message.value.decode("utf-8")
message_json = json.loads(message_string)
global messages_dict
response = json.loads(message.value.decode('utf-8'))['text']
try:
send_to = messages_dict[key]
self.sendMessage(send_to, response)
del(messages_dict[key])
except:
pass
def sendMessage(self, user_id, message):
guid = random.randint(1, 1000)
url = "https://api.vk.com/method/messages.send"
if(user_id<2000000000):
params = {
'user_id': user_id,
'message': message,
'access_token': token,
'v': API_VERSION,
}
else:
user_id=user_id-2000000000
params = {
'chat_id': user_id,
'message': message,
'access_token': token,
'v': API_VERSION,
}
res = requests.post(url, params=params)
data = json.loads(res.text)
response = data['response']
if 'error' in data:
print(data)
return response
def main():
tasks = [
VkListener(),
VkSender()
]
for t in tasks:
t.start()
while True:
time.sleep(10)
if __name__ == "__main__":
logging.basicConfig(
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
level=logging.WARNING
)
config = ConfigParser()
configName = "/etc/botoss/vk.conf"
config.read(configName)
token = config['main']['token']
messages_dict = {}
kafka_address = config['main']['kafka-address']
kafka_port = config['main']['kafka-port']
main()