Bridge Communications

Saturday, February 4, 2017

Extending Microsoft Teams for Developers - Part I

With the roll out of Microsoft Teams in preview, the workplace for users who work in small groups got a lot easier.  Teams fills in some feature gaps in Skype for Business that were lost during it's move to the cloud, like persistent chat.  Teams makes it pretty easy to forget about the quirky persistent chat mechanism available in Lync / Skype for Business on-prem.


Teams contains a bunch of other functionality that I won't be talking about today.  Instead I want to focus on how developers can extend Microsoft Teams today, and what I hope will be coming in the future.

As of today there are 3 ways developers can extend Microsoft Teams, Tabs, Bots, and Connectors.

Tabs

Tabs are pretty self explanatory, in fact if you have used teams at all, you have probably added a OneNote, or Power Bi tab to a channel already.  I am not going to cover this today, because as it sits right now, you can only add a custom tab that lasts for 30 days, so I am going to wait for this to mature a little more before diving in.

Bots

You can easily add a bot in Teams as well.  It will end up in the chat section which is divided into users and bots that you can chat with.  You can use the Microsoft Bot Framework to build your bot, and adding it to teams is very easy.  I have previously posted on this blog on how to create a bot.


When I chat with my bot in teams it is a similar experience as I get on the web, with some minor word wrapping picture issues.

Connectors

Connectors are where we are going to focus our effort today.  If you go to a channel in Teams, click the 3 dots and bring up the connectors option you will see a bunch of ready made connectors you can use.


We could connect with any of those, but we want to make our own Connector so we are going to select Incoming Webhook.  From here you give your webhook a name, and I would recommend a photo, because it will show up with that photo in teams, rather than the generic webhook one.  This is important if you add multiple connectors to a channel.  It makes it exponentially easier to tell which one the message came from.  When you create it, you will get the webhook url, copy this to notepad or somewhere, as we are going to need it later.


Here is a sample one I created to report on website visitors that got to the site from a paid ad, or who are looking at a number of different pages, and are a potential sales lead.

The Code

As their documentation shows you can trigger a post from curl or powershell of you are command line junkie.
curl -H "Content-Type: application/json" -d "{\"text\": \"Hello World!\"}" <YOUR WEBHOOK URL>

Personally I want to use c# and make a simple function that I can call from the web, a windows service, wpf, a console app, winforms, uwp, mobile, etc.

I am going to make first a very simple Payload class that will be for the simplest type of message, containing a title and some text in the body.

public class Payload
        {
            [JsonProperty("title")]
            public string title { get; set; }

            [JsonProperty("text")]
            public string text { get; set; }
        }

From here we need a function to push this to our webhook url.

static async void PostTeamsMessage(Payload payload, string uristring)
        {
            try
            {
                string payloadJson = JsonConvert.SerializeObject(payload);
                var content = new StringContent(payloadJson);

                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var client = new HttpClient();
                Uri uri = new Uri(uristring);

                HttpResponseMessage response = await client.PostAsync(uri, content);

                HttpContent responseContent = response.Content;

                // Get the stream of the content.
                using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
                {
                    // Write the output.
                    Console.WriteLine(await reader.ReadToEndAsync());
                }

            }
            catch { }
        }


So in just a few lines of code we take out payload class, serialize it to json, and sent it the webhook url we copied earlier.  I also have a section that write the output to the console which can be useful for debugging as you get started.

When we call our function the message is instantly posted to our Microsoft Teams channel.  I listed a copy of examples below.


Next week I will show you how to extend this to more complicated posts, and in a few weeks I will show you some very cool things you can do to extend Office Groups even more.

Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC, 
Full Stack Developer  BridgeOC Bridge Operator Console
Twitter - @droutledge @ndbridge


















No comments:

Post a Comment

Any spam comments will be deleted and your user account will be disabled.