Tuesday, August 9, 2016

Integrating Lync/Skype for Business with Bot Framework

Since the release of the Bot Framework, I've been waiting for Lync/Skype for Business support for two main reasons:

      • Lync API is outdated and not very reliable.
      • Easy and straightforward dialogs in the framework

And recently I've found a very interesting Bot Framework API called DirectLine.
It allows you to connect directly to the bot with a few lines of code, impersonate users and easily get and post messages. You can grab it from NuGet here: https://www.nuget.org/packages/Microsoft.Bot.Connector.DirectLine/. It also needs to be enabled at your bot configuration page:



Since I had some code that worked with Skype for Business already (using Lync 2013 SDK), I decided to use DirectLine as a proxy.

The idea is very simple:
  1. Receive message on Skype for Business
  2. Forward it to the Bot Framework via DirectLine API
  3. Wait for reply from DirectLine API
  4. Get the reply and send it to the user
Here's how you subscribe to the incoming chat messages in Skype for Business:


Now, when the message is received, the ImModality_InstantMessageReceived is called. All you need to do is to grab the message text and send it to the Direct Line.


Of course you'll have to add some optimizations to close unused dialogs, and keep track of conversations, but overall, in less that 100 lines of code you can connect to the
server and start the bot conversation.