(by Scott Chapman)

Recently I’ve been writing a lot about building Watson Work applications in a serverless environment (specifically IBM Cloud Functions). And I have published a framework that takes care of the heavy lifting to bring the promise of serverless development to Watson Work developers. If you’ve ever tried to build slash commands in Watson Work you know it can be a little daunting. So, I’ve added support for slash commands to my Watson Work serverless framework to help! Let me explain…

In Watson Workspace, users collaborate in a space, a virtual workspace. They can converse, share files, work with other tools and more. There are cognitive capabilities such as moments and action fulfillment built in. As an app developer, there is a lot you can do to enhance that experience. Suppose you want a way to invite participants in a space to an event. An easy way to do that is through slash commands. Slash commands provide a simple way to invoke your application.

Slash commands can simply do work and report back to the space, of they can start an Action Fulfillment flow to interact with the user directly. With Action Fulfillment you can present the user with a set of cards each with a set of buttons providing the user with options. For example:

Action fulfillment card
Action Fulfillment

You can see my example here, and it is deployable with wskdeploy.

To get started you need to add a command to your Watson Work application. Edit the settings for your application from your list of apps. On the left side select “Add an Action”. This will walk you through the process of setting up the action:

Enter the Name and Description of your action and press Next.

Add an action dialog

Enter the actual command name along with a hint when the command is offered. For this example you can use “Invite” and “Invite space members to Event”. You will see how the command will manifest in the command preview.

configure command dialog

On the next dialog you will select your webhook that will process the command, simply select the one you already created.
Review the summary to make sure it is what you want, and Add Your Action.

Now that you have added the action your webhook will start receiving events and the framework will process them for you. I’ve created two new triggers to make handling the events much easier.

WWActionSelected — Command has been selected by the user
WWButtonSelected — Button had been selected by the user.

So, to implement your command you will need to write an action that will receive events from the WWActionSelected trigger and you can also write an action to handle events from WWButtonSelected trigger (you can deploy the Dumper sample to understand what structure of the information is for the events). But let me walk you through what this sample is doing.

Command Selected Processing

I’ve written an action CommandSelected which will be receiving the Command selected events (which are trigger when the user enters the slash command). You can see it does the following:

It checks the actionId to assure that it is the action we care about (in this case it sees if it begins with /invite.
Then it runs a graphQL command to get the list of participants in the space (up to a limit).

And then for each user it generates a card with the following information:

  • Title, Subtitle and Text saying who to invite for the special event. You can look at the code to understand the structure of cards, and the structure of buttons.
  • A button with the user’s information (to be processed by the ButtonSelected Action later) and what response should be given when selected.

It then returns the annotation with array of cards attached. The flow looks like:
command selected flow
Command Selected Flow

Button Selected Action

The next action is the Button Selected action which handles the event when the user selects a button on one of the cards you generated in the Command Selected action above. You can do whatever processing you want, but what I have chosen for this example is to respond back to the space that the person was invited (and who invited them) and complete the command. The flow for this action looks like this:
button selected flow
Button Selected Flow

Command Experience

When you have this all set up you will be able to invoke your command by typing in the command action you defined (/invite).
invite slash command
Select the command, and your Command Selected action will be fired and produce cards with buttons:
command selected card
Command Selected card

Then pressing the button will trigger the Button Selected event and report back to the space:
button selected even response
Button selected, user invited!

And you can close the command dialog. That’s it!

This post was originally published in Scott’s blog at https://medium.com/@scottedchapman.

Join The Discussion

Your email address will not be published. Required fields are marked *