@@ -28,7 +28,6 @@ def try_convert_str(string):
28
28
29
29
return string
30
30
31
-
32
31
# Format text to lowercase, and remove trailing whitespaces
33
32
text = text .lower ().rstrip (' ' )
34
33
command , * args_str = text .split (' ' )
@@ -82,6 +81,7 @@ class Slack(threading.Thread):
82
81
notify/task {cmd} *args: register task with name `cmd` that is
83
82
performed every time `update()` is called.
84
83
"""
84
+
85
85
def __init__ (self , interval = 3 , config = None , auto_start = True , ** commands ):
86
86
"""
87
87
Initializes Slack bot, including auto-updating widget if in notebook
@@ -111,6 +111,7 @@ def __init__(self, interval=3, config=None, auto_start=True, **commands):
111
111
'msmt' : self .print_measurement_information ,
112
112
'measurement' : self .print_measurement_information ,
113
113
'notify' : self .add_task ,
114
+ 'help' : self .help_message ,
114
115
'task' : self .add_task ,
115
116
** commands }
116
117
self .task_commands = {'finished' : self .check_msmt_finished }
@@ -235,9 +236,13 @@ def get_im_messages(self, username, **kwargs):
235
236
Returns:
236
237
List of IM messages
237
238
"""
238
- response = self .slack .im .history (channel = self .users [username ]['im_id' ],
239
- ** kwargs )
240
- return response .body ['messages' ]
239
+ channel = self .users [username ].get ('im_id' , None )
240
+ if channel is None :
241
+ return []
242
+ else :
243
+ response = self .slack .im .history (channel = channel ,
244
+ ** kwargs )
245
+ return response .body ['messages' ]
241
246
242
247
def get_new_im_messages (self ):
243
248
"""
@@ -276,6 +281,11 @@ def update(self):
276
281
new_messages = self .get_new_im_messages ()
277
282
self .handle_messages (new_messages )
278
283
284
+ def help_message (self ):
285
+ """ Return simple help message """
286
+ cc = ', ' .join (['`' + str (k ) + '`' for k in self .commands .keys ()])
287
+ return '\n Available commands: %s' % cc
288
+
279
289
def handle_messages (self , messages ):
280
290
"""
281
291
Performs commands depending on messages.
@@ -302,7 +312,8 @@ def handle_messages(self, messages):
302
312
if isinstance (func , _BaseParameter ):
303
313
results = func (* args , ** kwargs )
304
314
else :
305
- # Only add channel and Slack if they are explicit kwargs
315
+ # Only add channel and Slack if they are explicit
316
+ # kwargs
306
317
func_sig = inspect .signature (func )
307
318
if 'channel' in func_sig .parameters :
308
319
kwargs ['channel' ] = channel
@@ -321,7 +332,8 @@ def handle_messages(self, messages):
321
332
channel = channel )
322
333
else :
323
334
self .slack .chat .post_message (
324
- text = 'Command {} not understood' .format (command ),
335
+ text = 'Command {} not understood. Try `help`' .format (
336
+ command ),
325
337
channel = channel )
326
338
327
339
def add_task (self , command , * args , channel , ** kwargs ):
0 commit comments