Integrating Dynamics365 with SLACK

On a recent project, we wanted to notify users when new items were added into Dynamics (by third-party systems).  The initial goal was to use email as the delivery mechanism but seeing as how everyone was using SLACK for messaging I decided this would be a better (and much cooler delivery mechanism).

This is my first time working with the Slack API and I have to say it was surprising how easy it was to put together some demo functionality to prove it all out.

First thing you are going to need to do is to create an application.  When you create your application you can then enable features for your application.

For my application, I created two incoming WebHooks that enabled me to post to the my #general channel and send to direct messages to a user.

Note: For these demos, I was only concerned with pushing content out.  If I were to implement this correctly, I would create a bot to handle some back and forth with our users.

slack

In my use case, I wanted to post to a channel when a user created a lead as they were doing it, i.e., a plugin.  The problem, of course, is that embedding Newtonsoft’s JSON library into a Dynamics plugin required some ILMerge work which I did not want to do, hence the simple code here that makes it work.

To do this, I followed the MSDN article here, to build the initial framework to support pushing the content to SLACK.

My code to get this up and running is as follows and only required base .NET assemblies to be used.

static void Connect()
{
string urlWithAccessToken = "https://hooks.slack.com/services/#YOURTOKENHERE#";

WebRequest request = WebRequest.Create(urlWithAccessToken);
request.Method = "POST";
request.ContentType = "application/json";

SlackMessage sm = new SlackMessage();
sm.user = "testing@testing.com";
sm.text = "THIS IS A TEST MESSAGE from my Console";
sm.channel = "#general";

MemoryStream ms = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(SlackMessage));
ser.WriteObject(ms, sm);

byte[] bytearray = ms.ToArray();

Stream datastream = request.GetRequestStream();
datastream.Write(bytearray,0,bytearray.Length);
datastream.Close();

WebResponse response = request.GetResponse();

Console.WriteLine(((HttpWebResponse)response).StatusDescription);
Console.ReadLine();
}
}

[DataContract]
public class SlackMessage
{
[DataMember]
public string user { get; set; }
[DataMember]
public string text { get; set; }
[DataMember]
public string channel { get; set; }
}

What this does is assemble my message into a JsonDataContract which then gets serialized and sent to SLACK via the token URL (use your own token URL here which is available from your My Apps section in SLACK).

The end result was being able to post some information into our general SLACK channel from our my app.

slack2

All in all, a great way to build a very simplistic demo and get Dynamics posting content to your Slack Channels for your users to see.

Post A Reply

%d bloggers like this: