We just added a couple of new features in ASP.NET WebHooks that makes it easier to build some nice integrations with Slack Slash Commands. These commands make it possible to trigger any kind of processing from a Slack channel by generating an HTTP request containing details about the command. For example, a command typed in a Slack channel can be configured to send an HTTP request to an ASP.NET WebHook endpoint where processing can happen:
/henrik Remember to buy flowers!
Parsing Slack Commands
In the post Updates to Microsoft ASP.NET WebHooks Preview we described the basics of how to use ASP.NET WebHooks with Slash Commands. That used a simple string to trigger the command without going into detail with what the command would look like. However, what if you want to build a Slash Command that can manage a list of users like this:
/list add <user>
/list list
/list remove <user>
public class SlackWebHookHandler : WebHookHandler
{
public SlackWebHookHandler()
{
this.Receiver = SlackWebHookReceiver.ReceiverName;
}
public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
{
// Get Slash Command data as a name value collection
var command = context.GetDataOrDefault<NameValueCollection>();
// Parse the text of the form 'action parameter'.
var slashCommand = SlackCommand.ParseActionWithValue(command["text"]);
// Look at action and parameter
string action = slashCommand.Key;
string parameter = slashCommand.Value;
return Task.FromResult(true);
}
}
/list add title=Remember to buy flowers!; assignedTo: henrik
/list list
public class SlackWebHookHandler : WebHookHandler
{
public SlackWebHookHandler()
{
this.Receiver = SlackWebHookReceiver.ReceiverName;
}
public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
{
// Get Slash Command data as a name value collection
var command = context.GetDataOrDefault<NameValueCollection>();
// Parse the text of the form 'action p1=v1; p2=v2; ...'.
var slashCommand = SlackCommand.ParseActionWithParameters(command["text"]);
// Look at action and parameters
string action = slashCommand.Key;
NameValueCollection parameters = slashCommand.Value;
return Task.FromResult(true);
}
}
Responding with Data, Images, and More
Just like it is possible to pass more complex commands, it is also possible to respond with structured data and even to include tabular data, images, and more. To facilitate this we have added the SlackSlashResponse class which helps you build a response to send back to Slack. In its simplest form, it takes just a string but you can add attachments to it containing structured text, tabular data, and images:
public class SlackWebHookHandler : WebHookHandler
{
public SlackWebHookHandler()
{
this.Receiver = SlackWebHookReceiver.ReceiverName;
}
public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
{
// Information can be returned using a SlackSlashResponse
var slashReply = new SlackSlashResponse("Hello from ASP.NET WebHooks!");
// Slash replies can be augmented with attachments containing data, images, and more
// The fallback description is used in clients that can only show plain-text replies.
var att = new SlackAttachment("Attachment Text", "Fallback description")
{
Color = "#439FE0",
Pretext = "This is an attachment!",
Title = "Attachment title",
ImageLink = new Uri("http://www.example.com"),
};
// Slash attachments can contain tabular data as well
att.Fields.Add(new SlackField("Field1", "1234"));
att.Fields.Add(new SlackField("Field2", "5678"));
// A reply can contain multiple attachments
slashReply.Attachments.Add(att);
// Return slash command response
context.Response = context.Request.CreateResponse(slashReply);
return Task.FromResult(true);
}
}
Trying it out
We provide a sample that shows both parsing commands and generating responses using the same model as outlined above. By setting up the Slash Command as described in the blog Updates to Microsoft ASP.NET WebHooks Preview and publishing an ASP.NET WebHook Slack Receiver with a handler like above you can get a structured response similar to this:
That’s it! Now you can process complex Slack Slash Commands and generate structured responses!
Happy Valentine’s Day!
Henrik
0 comments