How I built an Ms Lync ‘Bot’ formally known as D.A.V.E.

Lync_LogoMicrosoft Lync is Microsoft’s latest offering of a unified communications system. Sporting features you’d expect to find in a SIP/Unified Comms client, Lync is a pretty powerful piece of kit.

What’s more, Microsoft offer an SDK to enable easy development of tools and add-ons using Lync technologies. Most of the tutorials and blog posts seem to focus on either C# or Silverlight, which is fine – however, as I am primarily a VB.Net developer I decided to publish my own guide on how to intercept and respond to received Lync messages.

Firstly, I need to point out that I have only had access to Lync for a couple of months now and only downloaded the SDK earlier this month.. So although the code below is functional, I am constantly learning and may find a better way of doing things as my learning continues. To make things easier, make sure you subscribe to the post and you’ll get an update each time I adjust the code.

You will need to download and install the Microsoft Lync SDK then fire up your copy of Visual Studio, create a new windows form project. Obviously you could use any type of project here, however I have plans for a GUI in the future so I am using a windows form project.

Now, open the code view of your new form and import the following:

Now, under your “Public Class Form1” but before your “Private Sub Form1_Load()” enter the following lines of code:

That’s all there is to it for the declarations, as you can see I have used ‘WithEvents’ to expose the ‘methods’ in the GUI and make things a little easier developing with them.

Next in the “Form_Load()” event, paste in the following code:

This essentially ties your code to the Lync client running on your machine, then using ‘automation’ it confirms the client is signed in, if not it signs in for you.

Now, your app is hooked into the Lync client, you will need to ‘capture’ the message received event and handle it appropriately.

As you can see from the post above, I am using the built in replace functionality of the string. This is because the Lync client passes line feeds after its message, which makes handling the received message quite complicated.

So, from the code above – each time an instant message is received by your Lync client the variable strRec will contain the received message.

Now, upon receipt of a message you may want to send a response – which is where my next code snippet comes in. The following Sub send’s an instant message to the participants of the conversation:

From the code above, you should see the sub requires a string to be passed to it. This is what will be relayed to the client who sent the original message.

So there you have it, a fairly simple way of receiving and sending back a Lync ‘IM’.

Obviously, you may want to improve this slightly by trimming the received message and handling it depending on the users request. So, for example – you may want to ‘serve’ a simple weather forecast based on the users request. To do this all you need to do is enhance the ‘_LocalIMModality_InstantMessageReceived’ sub to include code something like this:

Now, create another sub which makes use of Yahoo’s weather forecasting, to reply to the Lync user with details of the local forecast:

As you can see from the code above, I have also built in the functionality for the Lync user to request weather for a couple of locations around the UK. This could be increased to as many as you want, or removed to only send details of one particular place.

Some other things you might want to consider would be for your ‘Bot’ to query a SQL table and pass back the results, or perhaps perform other functionality such as PING a device and send the reply details in an IM.

In a future blog post I will show you how to connect your new ‘BOT’ into a AI handler to provide an AI Bot capable of holding a ‘conversation’ of sorts.

As per my opening paragraph, I am very much in the early days of my Lync development – so if you have any suggestions on how I could improve the code above, please get in touch.

Mike Hudson

Mike Hudson is a Infrastructure Analyst living and working in Kingston Upon Hull. With extensive experience in Microsoft and Apple technologies, ranging from desktop OS’s to Server OS’s and hardware. By day working as part of an infrastructure team, and by night ridding the world of IT issues through blog posts..

%d bloggers like this: