Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add help command, add fix for new users #646

Merged
merged 4 commits into from
Jun 22, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions qcodes/utils/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def try_convert_str(string):

return string


# Format text to lowercase, and remove trailing whitespaces
text = text.lower().rstrip(' ')
command, *args_str = text.split(' ')
Expand Down Expand Up @@ -82,6 +81,7 @@ class Slack(threading.Thread):
notify/task {cmd} *args: register task with name `cmd` that is
performed every time `update()` is called.
"""

def __init__(self, interval=3, config=None, auto_start=True, **commands):
"""
Initializes Slack bot, including auto-updating widget if in notebook
Expand Down Expand Up @@ -111,6 +111,7 @@ def __init__(self, interval=3, config=None, auto_start=True, **commands):
'msmt': self.print_measurement_information,
'measurement': self.print_measurement_information,
'notify': self.add_task,
'help': self.help_message,
'task': self.add_task,
**commands}
self.task_commands = {'finished': self.check_msmt_finished}
Expand Down Expand Up @@ -235,9 +236,13 @@ def get_im_messages(self, username, **kwargs):
Returns:
List of IM messages
"""
response = self.slack.im.history(channel=self.users[username]['im_id'],
**kwargs)
return response.body['messages']
channel = self.users[username].get('im_id', None)
if channel is None:
return []
else:
response = self.slack.im.history(channel=channel,
**kwargs)
return response.body['messages']

def get_new_im_messages(self):
"""
Expand Down Expand Up @@ -276,6 +281,11 @@ def update(self):
new_messages = self.get_new_im_messages()
self.handle_messages(new_messages)

def help_message(self):
""" Return simple help message """
cc = ', '.join(['`' + str(k) + '`' for k in self.commands.keys()])
return '\nAvailable commands: %s' % cc

def handle_messages(self, messages):
"""
Performs commands depending on messages.
Expand All @@ -302,7 +312,8 @@ def handle_messages(self, messages):
if isinstance(func, _BaseParameter):
results = func(*args, **kwargs)
else:
# Only add channel and Slack if they are explicit kwargs
# Only add channel and Slack if they are explicit
# kwargs
func_sig = inspect.signature(func)
if 'channel' in func_sig.parameters:
kwargs['channel'] = channel
Expand All @@ -321,7 +332,8 @@ def handle_messages(self, messages):
channel=channel)
else:
self.slack.chat.post_message(
text='Command {} not understood'.format(command),
text='Command {} not understood. Try `help`'.format(
command),
channel=channel)

def add_task(self, command, *args, channel, **kwargs):
Expand Down