Intro
Hey everyone. I have recently created a small internal automation that manages a simple Slack command for internal purposes, and I figured it might be fun to share the concept with you via this small tutorial.
The process looks like this:
Prerequisites
There are a few important elements:
- Slack app - I created a simple Slack app to manage my command and where I could pass my HTTP Webhook Event URL;
But it can be any messaging app that works similarly. - Studio Web - it was the perfect match for my use case; it made the automation super quick to build and then publish for instant results
- and a Webhook Event Happened trigger activity:
Given that it is currently in Preview, you will have to enable access to Preview packages in Studio Web via an Automation Ops Studio Web policy
- and a Webhook Event Happened trigger activity:
- Data Service (optional) - I already had my Data Service entity with some specific information I wanted to use as my input data set;
In theory, this can be any input data set you want to use, and as long as it will fit in the instruction prompt of the Open AI. As such, technically you don’t need to store it in a Data Service entity. But for my purposes, it was easier this way because I could then use it across multiple processes and only update my data in one place. - Open AI - the missing piece was something that can parse my input data based on the user input (and some custom instructions) to generate the answer;
- HTTP Request activity (WebAPI package) - it is used purely to deliver the response back to the user
What does it do?
To keep things fun for whoever will want to try it out (and also because I believe it to be super simple to build, but please let me know!), this time I will try to explain the general idea rather than going over all the details.
Let’s go
Step 1 - User request
The flow of the process starts when the user issues a simple command from Slack:
/command [Your question]
This command is linked to a Slack app which in turn generates an API call to a specific endpoint. As you can imagine, it is the exact endpoint that we got from our Webhook Event Happened trigger activity:
If the process is published, this will automatically create an event trigger and start new jobs on each received API call from Slack API
Extracting the user input
After the process is triggered and we get to see the actual payload, we will need to deserialize the JSON sent to us from Slack by using the Deserialize JSON activity.
Side note: To make it even simpler, our latest WebAPI 1.19.0-preview activity package delivers a way for you to provide a sample JSON structure, based on which you will get instant access to your JSON string objects during design time.
You can refer to the Slack API documentation for more details about the response structure, but it will look like this:
{
(...)
"command": "/getproductowner",
(...)
"text": "System Activities",
(...)
"response_url": "https://hooks.slack.com/commands/some/url/bits"
(...)
}
Where:
- the command tells you which command was issued by the user
- the text tells you what they asked about
- the response_url tells you where to send your reply back
Fetching the data from the Data Service entity
Because our use case requires us to provide some input data, and we already store it in a specific Data Service entity - let’s extract it!
This will happen in three simple steps:
- Get all records with a Query Entity Records activity.
This way we get instant access to our entity, but for not in a format that we need (a simple string with everything) - Iterate over the output and add each row to a collection. In this case, we simply put a product tag and their owner as each new collection item:
- Lastly, let’s combine our collection into a single string by using the Combine Text activity. Each record will then be separated by a new line:
Kindly asking GPT4 chat completion for the answer
This is actually the simplest step. We will need an Open AI Chat Completion activity:
Same preview disclaimer as in the case of the webhook event trigger above.
And we will provide the user Slack command input and some custom instructions with our base data as the instructions:
It works better than one may expect
Sending the answer back to the user
We reached the final step. We will now use the HTTP Request activity to send the reply. All we have to do is set it up with the response_url provided by the Slack API:
Add a Content-Type: application/json
header:
And add our body in the response:
And this is it!
In my case, the process takes 10-15 seconds, the delay that I found to be linked mostly to the Data Service entity extraction step.
It works like this:
For the final process, I also added a bunch of improvements, after the users got to break it a bunch:
- handle empty command prompts (some users sent nothing)
- escape double quotes and ask GPT4 to not reply with any, because it was breaking the response_url payload
- I also use a separate Data Service entity to log whenever users ask for something that was not there (so that I can improve my data set accordingly)
These were not in the scope of this tutorial.
Please let me know if you gave it a try and it worked out well for you. I am always curious what our users are tinkering with!