@@ -28,7 +28,6 @@ def try_convert_str(string):
2828
2929 return string
3030
31-
3231 # Format text to lowercase, and remove trailing whitespaces
3332 text = text .lower ().rstrip (' ' )
3433 command , * args_str = text .split (' ' )
@@ -82,6 +81,7 @@ class Slack(threading.Thread):
8281 notify/task {cmd} *args: register task with name `cmd` that is
8382 performed every time `update()` is called.
8483 """
84+
8585 def __init__ (self , interval = 3 , config = None , auto_start = True , ** commands ):
8686 """
8787 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):
111111 'msmt' : self .print_measurement_information ,
112112 'measurement' : self .print_measurement_information ,
113113 'notify' : self .add_task ,
114+ 'help' : self .help_message ,
114115 'task' : self .add_task ,
115116 ** commands }
116117 self .task_commands = {'finished' : self .check_msmt_finished }
@@ -235,9 +236,13 @@ def get_im_messages(self, username, **kwargs):
235236 Returns:
236237 List of IM messages
237238 """
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' ]
241246
242247 def get_new_im_messages (self ):
243248 """
@@ -276,6 +281,11 @@ def update(self):
276281 new_messages = self .get_new_im_messages ()
277282 self .handle_messages (new_messages )
278283
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+
279289 def handle_messages (self , messages ):
280290 """
281291 Performs commands depending on messages.
@@ -302,7 +312,8 @@ def handle_messages(self, messages):
302312 if isinstance (func , _BaseParameter ):
303313 results = func (* args , ** kwargs )
304314 else :
305- # Only add channel and Slack if they are explicit kwargs
315+ # Only add channel and Slack if they are explicit
316+ # kwargs
306317 func_sig = inspect .signature (func )
307318 if 'channel' in func_sig .parameters :
308319 kwargs ['channel' ] = channel
@@ -321,7 +332,8 @@ def handle_messages(self, messages):
321332 channel = channel )
322333 else :
323334 self .slack .chat .post_message (
324- text = 'Command {} not understood' .format (command ),
335+ text = 'Command {} not understood. Try `help`' .format (
336+ command ),
325337 channel = channel )
326338
327339 def add_task (self , command , * args , channel , ** kwargs ):
0 commit comments