-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSlack.py
390 lines (314 loc) · 13.6 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import sublime
import sublime_plugin
import threading
from os.path import isfile
from .api import api_call
from .loader import Loader
# methods
API_CHANNELS = 'channels.list'
API_USERS = 'users.list'
API_GROUPS = 'groups.list'
API_POST_MESSAGE = 'chat.postMessage'
API_UPLOAD_FILES = 'files.upload'
API_OPEN_IM_SESSION = 'im.open'
API_CLOSE_IM_SESSION = 'im.close'
class BaseSend(sublime_plugin.TextCommand):
""" Base class which shows a channels list and sends messages """
def __init__(self, view):
super(BaseSend, self).__init__(view)
# here we will store all messages
self.messages = []
self.last_receiver = None
# this channels list is only populated once, per sublime session
# you need to restart sublime to re-take channels from API
self.receivers = []
# load plugin settings
self.settings = sublime.load_settings("Slack.sublime-settings")
def _api_call(self, *args, **kwargs):
if args[0] == API_POST_MESSAGE:
kwargs['icon'] = self.settings.get('avatar_url')
return api_call(*args, **kwargs)
def run(self, view):
# load settings each time a command is ran
self.settings = sublime.load_settings("Slack.sublime-settings")
# reset older messages stored in memory
self.messages = []
# check if token is set
if not self.settings.get('team_tokens'):
sublime.error_message('SLACK Error: Please set your API "team_tokens" in Preferences -> Package Settings -> Slack -> Settings - User')
raise Exception('Team Token missing')
def on_select_receiver(self, index):
""" Triggered after a received is selected from the quick_panel """
# start in a new thread, otherwise quick_panel is stuck
threading.Thread(
target=self.send_messages,
args=(index, )).start()
def send_messages(self, index):
if index is -1:
return
receiver = self.receivers[index]
token = receiver.get('token')
username = self.settings.get('username')
info = ''
subtext = self.settings.get('username_subtext')
if subtext and subtext is not None:
info = "({0})".format(subtext)
elif self.settings.get('show_plaform_and_name'):
info = sublime.platform()
try:
import getpass
info = "{0}, {1}".format(getpass.getuser(), info)
except:
# cannot get current OS user
pass
info = "({0})".format(info)
loading = Loader('Sending message ...')
channel = receiver.get('id')
channel_needs_closing = False
if receiver.get('type') == 'user':
session_open_response = self._api_call(API_OPEN_IM_SESSION, {
'token': token,
'user': channel
})
# print(session_open_response)
if not session_open_response.get('ok'):
loading.done = True
sublime.status_message('SLACK: Unexpected error!')
return
if not session_open_response.get('already_open'):
channel_needs_closing = True
im_channel = session_open_response.get('channel')
if im_channel and im_channel.get('id'):
channel = im_channel.get('id')
for message in self.messages:
args = {
'token': token,
'channel': channel,
'text': message,
'username': "{0} {1}".format(username, info)
}
response = self._api_call(API_POST_MESSAGE, args)
loading.done = True
if response['ok']:
sublime.status_message('SLACK: Message sent successfully!')
# set recipient for next autocomplete
self.next_input_val = self.get_receiver_display(receiver)
else:
sublime.status_message('SLACK: Unexpected error!')
if channel_needs_closing:
self._api_call(API_CLOSE_IM_SESSION, {
'token': token,
'channel': channel
})
loading.done = True
def init_message_send(self):
# check is channels are cached in memory
receivers = []
if not self.receivers:
# get the channels and users for each team
loading = Loader('Loading channels/users/groups')
for team, token in self.settings.get('team_tokens').items():
channels_response = self._api_call(API_CHANNELS, {
'token': token,
'exclude_archived': 1
}, loading=loading)
for channel in channels_response['channels']:
# bind the token and team to the channel
channel['token'] = token
channel['team'] = team
channel['type'] = 'channel'
self.receivers.append(channel)
groups_response = self._api_call(API_GROUPS, {
'token': token,
'exclude_archived': 1
}, loading=loading)
for group in groups_response['groups']:
# bind the token and team to the group
group['token'] = token
group['team'] = team
group['type'] = 'group'
self.receivers.append(group)
users_response = self._api_call(API_USERS, {
'token': token
}, loading=loading)
for user in users_response['members']:
if not user['deleted']:
# bind the token and team to the user
user['token'] = token
user['team'] = team
user['type'] = 'user'
self.receivers.append(user)
# loading done, remove status message
loading.done = True
# check if the message begins with #channel or @user, and if receiver exists
if self._must_send_directly():
return self.send_messages(self.forced_receiver_index)
# create receivers dropdown
for receiver in self.receivers:
if receiver['type'] is 'channel':
item = "# {0}".format(receiver['name'])
elif receiver['type'] is 'group':
item = "● {0}".format(receiver['name'])
else: # is user
item = "@ {0}".format(receiver['name'])
if len(self.settings.get('team_tokens')) > 1:
item += "({0})".format(receiver['team'])
receivers.append(item)
# display a popup and let the user pick a channel
self.view.window().show_quick_panel(
receivers,
self.on_select_receiver)
def get_receiver_display(self, receiver):
pre = ''
if receiver['type'] == 'user':
pre = '@'
elif receiver['type'] == 'channel':
pre = '#'
elif receiver['type'] == 'group':
pre = '.'
else:
return ''
return "{0}{1} ".format(pre, receiver['name'])
def _must_send_directly(self):
""" Checks if the message from input box begins with @user #channel .group """
if len(self.messages) > 1 or not self.messages:
return False
msg = self.messages[0]
# check if user
if msg.startswith('@'):
user = msg[1:msg.find(' ')]
# check if the user exists in recipients list
for index, receiver in enumerate(self.receivers):
if receiver['type'] is 'user' and receiver['name'] == user:
self.messages[0] = self.messages[0].replace('@'+user, '', 1)
self.forced_receiver_index = index
return True
# check if channel
if msg.startswith('#'):
channel = msg[1:msg.find(' ')]
# check if the user exists in recipients list
for index, receiver in enumerate(self.receivers):
if receiver['type'] is 'channel' and receiver['name'] == channel:
self.messages[0] = self.messages[0].replace('#'+channel, '', 1)
self.forced_receiver_index = index
return True
# check if group
if msg.startswith('.'):
group = msg[1:msg.find(' ')]
# check if the user exists in recipients list
for index, receiver in enumerate(self.receivers):
if receiver['type'] is 'group' and receiver['name'] == group:
self.messages[0] = self.messages[0].replace('.'+group, '', 1)
self.forced_receiver_index = index
return True
return False
class SendSelectionCommand(BaseSend):
""" Send the selected text to slack. Multiple selections supported """
def run(self, view):
super(SendSelectionCommand, self).run(view)
# get all selected regions
for region in self.view.sel():
text = self.view.substr(region)
if not text:
sublime.error_message("SLACK Error: No text selected")
return
self.messages.append("```{0}```".format(text))
threading.Thread(target=self.init_message_send).start()
class SendMessageCommand(BaseSend):
""" Send a message from user input """
def run(self, view):
super(SendMessageCommand, self).run(view)
ac = ''
if hasattr(self, 'next_input_val'):
ac = self.next_input_val
del(self.next_input_val)
self.view.window().show_input_panel(
"Slack: Enter a message", ac, self.on_done, None, None)
def on_done(self, message):
if not message:
return sublime.error_message('ERROR: Please enter a message')
self.messages = [message]
threading.Thread(target=self.init_message_send).start()
class UploadCurrentFileCommand(BaseSend):
""" Uploads the current file (active tab) """
def run(self, view):
super(UploadCurrentFileCommand, self).run(view)
self.file = self.view.file_name()
if not self.file:
return sublime.error_message('SLACK: No file open')
threading.Thread(target=self.init_message_send).start()
def on_select_receiver(self, index):
if index == -1:
return
if self.receivers[index]['type'] == 'user':
return sublime.error_message('Slack: sending files to users is disabled for the moment.\nHowever, you can send files to channels/groups.')
threading.Thread(target=self.upload_file, args=(index,)).start()
def upload_file(self, receiver_index):
receiver = self.receivers[receiver_index]
loading = Loader('Uploading file ...', False)
self._api_call(API_UPLOAD_FILES, {
'token': receiver.get('token'),
'channels': receiver.get('id'),
'filename': self.friendly_filename()
}, loading=loading, filename=self.file)
loading.done = True
sublime.status_message('File uploaded successfully!')
self.file = None
def friendly_filename(self):
filename = self.file.split('/').pop()
if self.settings.get('repeat_file_ext') and len(filename.split('.')) > 1:
filename += '.' + filename.split('.').pop()
return filename
class UploadSelectionAsFileCommand(UploadCurrentFileCommand):
""" Uploads current selected text as file """
def run(self, view):
super(UploadCurrentFileCommand, self).run(view)
self.file = self.view.file_name()
# get all selected regions
for region in self.view.sel():
text = self.view.substr(region)
if not text:
sublime.error_message("SLACK Error: No text selected")
return
self.messages.append(text)
threading.Thread(target=self.init_message_send).start()
def friendly_filename(self):
if self.file is None:
self.file = 'Untitled'
ext = self.get_extension_from_scope()
if ext:
self.file = '{}.{}'.format(self.file, ext)
return super(UploadSelectionAsFileCommand, self).friendly_filename()
def get_extension_from_scope(self):
scope = self.view.scope_name(0)
if 'html' in scope:
return 'html'
if 'python' in scope:
return 'py'
if 'sql' in scope:
return 'sql'
return ''
def upload_file(self, receiver_index):
receiver = self.receivers[receiver_index]
loading = Loader('Uploading file ...', False)
for content in self.messages:
self._api_call(API_UPLOAD_FILES, {
'token': receiver.get('token'),
'channels': receiver.get('id'),
'filename': self.friendly_filename(),
'content': content
}, loading=loading)
loading.done = True
sublime.status_message('Selection uploaded successfully!')
self.file = None
class UploadFromPathCommand(UploadCurrentFileCommand):
def run(self, view):
self.settings = sublime.load_settings("Slack.sublime-settings")
self.view.window().show_input_panel(
'Slack: Enter file path:', '',
self.on_done, False, False)
def on_done(self, path):
if not isfile(path):
return sublime.error_message('SLACK Error: File not found!\n'+path)
self.file = path
threading.Thread(target=self.init_message_send).start()