/
slackclient.py
210 lines (172 loc) · 6.83 KB
/
slackclient.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
# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import
import os
import json
import logging
import time
from ssl import SSLError
import slacker
from six import iteritems
from websocket import (
create_connection, WebSocketException, WebSocketConnectionClosedException
)
from slackbot.utils import to_utf8, get_http_proxy
logger = logging.getLogger(__name__)
class SlackClient(object):
def __init__(self, token, timeout=None, bot_icon=None, bot_emoji=None, connect=True,
rtm_start_args=None):
self.token = token
self.bot_icon = bot_icon
self.bot_emoji = bot_emoji
self.username = None
self.domain = None
self.login_data = None
self.websocket = None
self.users = {}
self.channels = {}
self.connected = False
self.rtm_start_args = rtm_start_args
if timeout is None:
self.webapi = slacker.Slacker(self.token)
else:
self.webapi = slacker.Slacker(self.token, timeout=timeout)
if connect:
self.rtm_connect()
def rtm_connect(self):
reply = self.webapi.rtm.start(**(self.rtm_start_args or {})).body
time.sleep(1)
self.parse_slack_login_data(reply)
def reconnect(self):
while True:
try:
self.rtm_connect()
logger.warning('reconnected to slack rtm websocket')
return
except Exception as e:
logger.exception('failed to reconnect: %s', e)
time.sleep(5)
def parse_slack_login_data(self, login_data):
self.login_data = login_data
self.domain = self.login_data['team']['domain']
self.username = self.login_data['self']['name']
self.parse_user_data(login_data['users'])
self.parse_channel_data(login_data['channels'])
self.parse_channel_data(login_data['groups'])
self.parse_channel_data(login_data['ims'])
proxy, proxy_port, no_proxy = get_http_proxy(os.environ)
self.websocket = create_connection(self.login_data['url'], http_proxy_host=proxy,
http_proxy_port=proxy_port, http_no_proxy=no_proxy)
self.websocket.sock.setblocking(0)
def parse_channel_data(self, channel_data):
self.channels.update({c['id']: c for c in channel_data})
def parse_user_data(self, user_data):
self.users.update({u['id']: u for u in user_data})
def send_to_websocket(self, data):
"""Send (data) directly to the websocket."""
data = json.dumps(data)
self.websocket.send(data)
def ping(self):
return self.send_to_websocket({'type': 'ping'})
def websocket_safe_read(self):
"""Returns data if available, otherwise ''. Newlines indicate multiple messages """
data = ''
while True:
try:
data += '{0}\n'.format(self.websocket.recv())
except WebSocketException as e:
if isinstance(e, WebSocketConnectionClosedException):
logger.warning('lost websocket connection, try to reconnect now')
else:
logger.warning('websocket exception: %s', e)
self.reconnect()
except Exception as e:
if isinstance(e, SSLError) and e.errno == 2:
pass
else:
logger.warning('Exception in websocket_safe_read: %s', e)
return data.rstrip()
def rtm_read(self):
json_data = self.websocket_safe_read()
data = []
if json_data != '':
for d in json_data.split('\n'):
data.append(json.loads(d))
return data
def rtm_send_message(self, channel, message, attachments=None, thread_ts=None):
message_json = {
'type': 'message',
'channel': channel,
'text': message,
'attachments': attachments,
'thread_ts': thread_ts,
}
self.send_to_websocket(message_json)
def upload_file(self, channel, fname, fpath, comment):
fname = fname or to_utf8(os.path.basename(fpath))
self.webapi.files.upload(fpath,
channels=channel,
filename=fname,
initial_comment=comment)
def upload_content(self, channel, fname, content, comment):
self.webapi.files.upload(None,
channels=channel,
content=content,
filename=fname,
initial_comment=comment)
def send_message(self, channel, message, attachments=None, as_user=True, thread_ts=None):
self.webapi.chat.post_message(
channel,
message,
username=self.login_data['self']['name'],
icon_url=self.bot_icon,
icon_emoji=self.bot_emoji,
attachments=attachments,
as_user=as_user,
thread_ts=thread_ts)
def get_channel(self, channel_id):
return Channel(self, self.channels[channel_id])
def open_dm_channel(self, user_id):
return self.webapi.im.open(user_id).body["channel"]["id"]
def find_channel_by_name(self, channel_name):
for channel_id, channel in iteritems(self.channels):
try:
name = channel['name']
except KeyError:
name = self.users[channel['user']]['name']
if name == channel_name:
return channel_id
def get_user(self, user_id):
return self.users.get(user_id)
def find_user_by_name(self, username):
for userid, user in iteritems(self.users):
if user['name'] == username:
return userid
def react_to_message(self, emojiname, channel, timestamp):
self.webapi.reactions.add(
name=emojiname,
channel=channel,
timestamp=timestamp)
class SlackConnectionError(Exception):
pass
class Channel(object):
def __init__(self, slackclient, body):
self._body = body
self._client = slackclient
def __eq__(self, compare_str):
name = self._body['name']
cid = self._body['id']
return name == compare_str or "#" + name == compare_str or cid == compare_str
def upload_file(self, fname, fpath, initial_comment=''):
self._client.upload_file(
self._body['id'],
to_utf8(fname),
to_utf8(fpath),
to_utf8(initial_comment)
)
def upload_content(self, fname, content, initial_comment=''):
self._client.upload_content(
self._body['id'],
to_utf8(fname),
to_utf8(content),
to_utf8(initial_comment)
)