Tuesday, July 17, 2012

Microsoft Lync Group Chat integration using C#

Recently we've been playing around with different ways of integrating and re-purposing Microsoft Lync and it's various server/client components into interesting applications with valuable and useful features. Over the past few weeks we've been working internally with the Lync Group Chat SDK, developing some prototypes for applications that could extend the functionality of the Group Chat rooms for other organizations.

For example, we are developing some applications that will monitor various services, such as email accounts or twitter feeds, and automically dispatch alerts into a Lync 2010 group chat room. 

Before we start with the code samples, I'll outline the system requirements below for packages to have installed.

Below is an example of a simple Group Chat task. This console application will log in to the Lync Server, log in to the Group Chat server, search for the chat room specified, join the chat room, send a sample chat message, and then disconnect from the room, chat server, and Lync server. This example provides a pretty comprehensive look at all the steps needed to perform simple to complex tasks on the Group Chat server using the Lync SDK and the Group Chat SDK.

For this application, we'll use the NDesk.Options library to parse our command line options to build up our message. Additionally, the configuration for the Group Chat settings is stored in an app.config file, which I'll outline below.

First, we build our Common class. This class holds some common variables which we'll use throughout the application, and some Group Chat specific functionality.

Some points about Common.cs:
  1. When building our UserEndpoint, we specify this application using the ClientPlatformSettings class, which for our console application, will give use a bit more ease of use and flexibility, rather than using the ServerPlatformSettings class.
  2. When joining a chat room, we first search for the room, and return a ChatRoomSnapshot. This snapshot provides us the information we need to then join the room using our Common.RoomJoinExisting method.

Next, we'll build up our actual program logic in Program.cs. This file holds 2 classes, our DispatchMessage class, and our Chat class. DispatchMethod is a simple object to hold the values that we'll be chatting into the chat room. This includes the message body, subject (if a story), if this message is an alert, and the name of the room we are looking to join. Our Chat class holds some various methods that we use at runtime, as well as our Main() program.

Some notes about Program.cs:

  1. After the NDesk.Options library parses the command line arguments, it populates the DispatchMessage class/object. Once this is complete, it runs through it's main try/catch block, which logs in to Lync, logs in to the Group Chat server, finds the room, and joins it.
  2. Once we have our session established, we move through our RoomChat method. This method reads from the DispatchMessage class/object and builds the actual chat message, then pushes it to the room.
  3. After the message has been posted, we leave room and disconnect our sessions.
In your app.config file, make sure to add the keys/values below to configure the environment settings for the application. We'll discuss what these keys mean below.

  <add key="OcsServer" value="######" />
  <add key="LookupServerUri" value="sip:###@###.###" />
  <add key="UserSipUri" value="sip:###@###.###" />
  <add key="Username" value="###@###.###" />
  <add key="Password" value="###" />
  • OcsServer
    • This is the server name to the Lync Front End Pool (probably something like 'lync.you.com' or 'lync.company.local'
  • LookupServerUri
    • This is the service account in SIP format that was setup with the Group Chat server. In our environment sip://ocschat@instant.local
  • UserSipUri
    • This is the account you'd like to send messages into the chat room as, in SIP format. This should be different than the LookupServerUri, and should be on the list of members for the chat room.
  • Username
    • The Active Directory user name that the user logs in to Lync as. Generally DOMAIN\username or username@domain.com or local.
  • Password
    • The users Active Directory password. Should be hashed or stored in some other fashion, but for our example, its plain-text in the file.
Once the program is built, run the .exe with the -? option to see the full help text, which will give you the parameters and syntax to run the program.

Using this core functionality we've wired up some protypes using Exchange Web Services, as well as Twitter's API. In Exchange, we monitor an inbox, when something arrives we parse out it's information, and send it to the appropriate chat room. Below, on the left you'll see the email drafted to the designated box, and on the right, the message as it appears as a story in the chat room.

For Twitter, we have the ability to watch user's Tweets, and track hashtags or other search queries. Below you'll see the tweet, as well as the message from our watched Twitter user in the chat room, complete with clickable links and some basic information.