forked from FAForever/QAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.py
134 lines (109 loc) · 3.77 KB
/
slack.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
import json
import threading
import time
from slackclient import SlackClient
class slackThread(threading.Thread):
def __init__(self, apikey):
threading.Thread.__init__(self)
self.APIKEY = apikey
self.DATA = {}
self.SC = SlackClient(self.APIKEY)
self.CON = None
self.lock = threading.Lock()
self.messageId = 0
self.handledEvents = {
'message': self.__event__message,
}
self.ready = False
def run(self):
works = True
self.CON = self.SC.rtm_connect()
if self.CON == False:
print('Failed starting a Slack RTM session.')
works = False
if not self.rebuildData():
print('Failed accessing slack data.')
works = False
if works:
print('Established Slack connection')
self.ready = True
countForPing = 0
while True:
for event in self.SC.rtm_read():
try:
self.handledEvents[event['type']](event)
except:
#print(event)
pass
countForPing += 0.1
if countForPing > 3:
self.SC.server.ping()
countForPing = 0
time.sleep(0.1)
def rebuildData(self):
self.lock.acquire()
test = json.loads((self.SC.api_call("api.test")).decode())
if test.get('ok') == False:
print('API Test failed. Full response:')
print(test)
self.lock.release()
return False
self.DATA['users'] = {}
for user in json.loads((self.SC.api_call("users.list")).decode()).get('members'):
self.DATA['users'][user['id']] = {
'name': user['name'],
}
self.DATA['channels'] = {}
for channel in json.loads((self.SC.api_call("channels.list")).decode()).get('channels'):
self.DATA['channels'][channel['id']] = {
'name': channel['name'],
}
self.lock.release()
return True
def __getMessageId(self):
self.lock.acquire()
mId = self.messageId
self.messageId += 1
self.lock.release()
return mId
def __getUserId(self, name):
return self.__getId('users', name)
def __getChannelId(self, name):
return self.__getId('channels', name)
def __getId(self, sub, name):
if self.ready:
for key in self.DATA[sub].keys():
if self.DATA[sub][key]['name'] == name:
return key
return None
def __getPmChannelId(self, name):
user = self.__getUserId(name)
if user == None:
return None
channelId = self.DATA['users'][user].get('pm_channel_id')
if channelId == None:
data = json.loads((self.SC.api_call("im.open", user=user)).decode())
channelId = data["channel"]["id"]
self.DATA['users'][user]['pm_channel_id'] = channelId
return channelId
def sendMessageToUser(self, user, text):
channelId = self.__getPmChannelId(user)
if channelId == None:
return
self.__sendMessage(channelId, text)
def sendMessageToChannel(self, channel, text):
channelId = self.__getChannelId(channel)
if channelId == None:
return
self.__sendMessage(channelId, text)
def __sendMessage(self, target, text):
self.lock.acquire()
self.SC.api_call(
"chat.postMessage",
as_user="true",
channel=target,
text=text)
self.lock.release()
def __event__message(self, event):
#print(self.DATA['users'][event['user']]['name'] + ": " + event['text'])
pass