qcodes.extensions.slack

Slack bot is used to send information about qcodes via Slack IMs. Some default commands are provided, and custom commands/tasks can be attached (see below).

To setup the Slack bot, a bot first has to be created via Slack by clicking ‘Create New App’ on https://api.slack.com/apps. Once created, the bot will have a name and unique token. These and other settings have to be saved in a config dict (see init( or Parameters) in Slack).

The App containing your bot needs to have the following bot token scopes to perform all methods successfully: - channels:history - channels:read - chat:write - files:write - users:read These can be set after clicking OAuth & Permissions in the left menubar after selecting your bot at https://api.slack.com/apps (or during creation).

Communication with the Slack bot is performed via instant messaging. When an IM is sent to the Slack bot, it will be processed during the next update() call (provided the username is registered in the config). Standard commands provided to the Slack bot are:

  • plot: Upload latest qcodes plot.

  • msmt/measurement: Print information about latest measurement.

  • notify finished: Send message once measurement is finished.

Custom commands can be added as (cmd, func) key-value pairs to self.commands. When cmd is sent to the bot, func is evaluated.

Custom tasks can be added as well. These are functions that are performed every time an update is called. The function must return a boolean that indicates if the task should be removed from the list of tasks. A custom task can be added as a (cmd, func) key-value pair to self.task_commands. They can then be called through Slack IM via:

notify/task {cmd} *args: register task with name cmd that is performed every time update() is called.

Exceptions:

SlackTimeoutWarning

Functions:

convert_command(text)

Classes:

Slack([interval, config, auto_start])

Initializes Slack bot, including auto-updating widget if in notebook and using multiprocessing.

exception qcodes.extensions.slack.SlackTimeoutWarning[source]

Bases: UserWarning

qcodes.extensions.slack.convert_command(text)[source]
class qcodes.extensions.slack.Slack(interval=3, config=None, auto_start=True, **commands)[source]

Bases: Thread

Initializes Slack bot, including auto-updating widget if in notebook and using multiprocessing.

Parameters
  • interval (int) – Update interval for widget (must be over 1s).

  • config (Optional[dict]) –

    Config dict If not given, uses qc.config[‘user’][‘slack’] The config dict must contain the following keys:

    • ’bot_name’: Name of the bot

    • ’bot_token’: Token from bot (obtained from slack website)

    • ’names’: Usernames to periodically check for IM messages

  • auto_start (bool) – Defaults to True.

Methods:

start()

Start the thread's activity.

run()

Thread event loop that periodically checks for updates.

stop()

Stop checking for updates.

exit()

Exit event loop, stop Thread.

user_from_id(user_id)

Retrieve user from user id.

get_users(usernames)

Extracts user information for users.

get_im_ids(users)

Adds IM ids of users to users dict.

get_im_messages(username, **kwargs)

Retrieves IM messages from username.

get_new_im_messages()

Retrieves new IM messages for each user in self.users.

update()

Performs tasks, and checks for new messages.

help_message()

Return simple help message

handle_messages(messages)

Performs commands depending on messages.

add_task(command, *args, channel, **kwargs)

Add a task to self.tasks, which will be executed during each update :param command: Task command.

upload_latest_plot(channel, **kwargs)

Uploads latest plot (if any) to slack channel.

print_measurement_information(channel, **kwargs)

Prints information about the current measurement.

check_msmt_finished(channel, **kwargs)

Checks if the latest measurement is completed.

start()[source]

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

run()[source]

Thread event loop that periodically checks for updates. Can be stopped via stop() , after which the Thread is stopped. :returns: None.

stop()[source]

Stop checking for updates. Can be started again via start(). :returns: None.

exit()[source]

Exit event loop, stop Thread. :returns: None

user_from_id(user_id)[source]

Retrieve user from user id. :param user_id: Id from which to retrieve user information.

Returns

User information.

Return type

dict

get_users(usernames)[source]

Extracts user information for users. :param usernames: Slack usernames of users.

Returns

{username: user}

Return type

dict

get_im_ids(users)[source]

Adds IM ids of users to users dict. Also adds last_ts to the latest IM message :param users: {username: user} :type users: dict

Returns

None.

get_im_messages(username, **kwargs)[source]

Retrieves IM messages from username. :param username: Name of user. :param **kwargs: Additional kwargs for retrieving IM messages.

Returns

List of IM messages.

get_new_im_messages()[source]

Retrieves new IM messages for each user in self.users. Updates user[‘last_ts’] to ts of newest message. :returns: {username: [messages list]} newer than last_ts. :rtype: im_messages (Dict)

update()[source]

Performs tasks, and checks for new messages. Periodically called from widget update. :returns: None.

help_message()[source]

Return simple help message

handle_messages(messages)[source]

Performs commands depending on messages. This includes adding tasks to be performed during each update.

add_task(command, *args, channel, **kwargs)[source]

Add a task to self.tasks, which will be executed during each update :param command: Task command. :param *args: Additional args for command. :param channel: Slack channel (can also be IM channel). :param **kwargs: Additional kwargs for particular.

Returns

None.

upload_latest_plot(channel, **kwargs)[source]

Uploads latest plot (if any) to slack channel. The latest plot is retrieved from qcodes.plots.base.BasePlot, which is updated every time a new qcodes plot is instantiated. :param channel: Slack channel (can also be IM channel). :param **kwargs: Not used.

Returns

None.

print_measurement_information(channel, **kwargs)[source]

Prints information about the current measurement. Information printed is percentage complete, and dataset representation. Dataset is retrieved from DataSet.latest_dataset, which updates itself every time a new dataset is created :param channel: Slack channel (can also be IM channel). :param **kwargs: Not used.

Returns

None.

check_msmt_finished(channel, **kwargs)[source]

Checks if the latest measurement is completed. :param channel: Slack channel (can also be IM channel). :param **kwargs: Not used.

Returns

True if measurement is finished, False otherwise.

Return type

bool