Slack is one of the big messaging apps for team collaboration, and it’s the perfect platform to demo how to integrate K2. This is the first article on how we go about doing it. Slack is free to use, so it’s great to demo with, and it has an ever-growing list of third-party integration plugins to play with.
Before we start with the demo, how would we expect a K2 Slack plugin to behave? I believe it should:
- Send notifications of tasks we have to action
- Notify us when a task has been completed and what the action was, when the workflow has moved to a certain stage, or when there is a workflow escalation
- Let us action a task from inside Slack
- Let us ask Slack what our outstanding tasks are, or what the status of a certain workflow is
So let’s start with the first two points and deal with simple notifications.
Building a simple messaging app for Slack
Let’s start with a simple example, where K2 sends notification messages to Slack, whether that’s a public message, a message to a particular group or a message to an individual person.
First of all, we need to sign up for Slack and create a team. Once we have a Slack team, we need to go to the Slack API site to get access to the API.
We are starting simple, so click on ‘Incoming webhooks’ and then on the ‘incoming webhook integration’ link.
Building the webhook URL
First, choose the channel you want to send the messages to. Don’t worry, we will be able to override this later on.
Click on the green ‘Add incoming WebHooks integration’ button. You can now see your webhook URL, so copy that.
Further down you can also customise the actual message. I have gone for a K2 look.
Click on ‘Save’ and we have created our webhook for incoming messages.
Slack endpoint assembly
Now that we have the webhook, we can write some code so K2 can use it. We are going to use an endpoint assembly for this, with a class that takes the endpoint and lets us pass in a message, an optional username and an optional group.
private static void PostMessage(Payload payload)
{
Encoding _encoding = new UTF8Encoding();
Uri endpoint = new Uri("web hook here");
string payloadJson = JsonConvert.SerializeObject(payload);
using (System.Net.WebClient client = new System.Net.WebClient())
{
System.Collections.Specialized.NameValueCollection data =
new System.Collections.Specialized.NameValueCollection();
data["payload"] = payloadJson;
var response = client.UploadValues(endpoint, "POST", data);
//The response text is usually "ok"
string responseText = _encoding.GetString(response);
}
}That’s the simple code for posting a payload of information to the webhook URL.
public static void PostMessage(string text, string username = null, string channel = null)
{
Payload payload = new Payload()
{
Channel = channel,
Username = username,
Text = text
};
PostMessage(payload);
}This is the public static method we will create a SmartObject from and then use inside a workflow.
We can then build the solution, take the DLL and tell K2 about it using the Endpoint Assembly broker. If you don’t know how to do that, have a look at my previous post on creating an endpoint assembly.
Now just build a SmartObject that uses the service instance you have just created.
We can test it in the SmartObject tester.
When it executes, we get this message in Slack.
Now that the SmartObject has been created, we can use this method inside a workflow.
Workflow
I am just going to use a simple workflow for this, with one task and two actions.
We are going to use a SmartObject event to call the Slack notification SmartObject. It sends a message to the destination user when a task is generated, and then a message to the originator when the task is approved or rejected.
We do something similar for the approve and reject activities, except we put in the originator’s name, and the message says the task has been approved or rejected depending on the activity.
When we run the workflow, the destination user gets this message in Slack:
It includes a link to the task. When they action the task, the originator gets this message:
Next time we will expand on this by making the notifications more advanced and by letting the user ask questions about K2.
The source code for this example was available to download from the original post.