Controlling Asterisk based on Microsoft OCS 2007 user's presence

Tom Keating : VoIP & Gadgets Blog
Tom Keating
CTO
| VoIP & Gadgets blog - Latest news in VoIP & gadgets, wireless, mobile phones, reviews, & opinions

Controlling Asterisk based on Microsoft OCS 2007 user's presence

Paul Adams contacted me recently since he's been reading my posts about Asterisk and 'Microsoft OCS 2007' integration.

Paul wrote an interesting application that enables call queuing that 'respects' OCS presence. That is, if the agent's OCS status is "Busy", "Away" or "Do not Disturb", don't send them a call from the call queue. Leveraging Asterisk.NET & the Microsoft OCS development tools - in C# (Visual Studio 2005) - he was successfully able to control calls in Asterisk 1.4 based on any user's presence in OCS.

He wrote a simple test app that register's with OCS 2007 for a single or multiple users presence. Whenever a users presence changes - OCS 2007 informs his app directly.

Using an agi entry in the Asterisk dial plan, Asterisk asks his app what to do next. Then - based on the user's presence - the app tells Asterisk to queue the call or pass it to the user.

Eventually, he intends that the user can 'register' themselves for call queues (via a web page perhaps) - and this information will be used by the app to determine what calls should go to what users & if they are available right now or now. He explained that he intends to turn this app into a service and run it on his OCS or OCS Mediation server - and control incoming calls for his call center.

He told me, "I'm impressed with Asterisk.NET - and with the tools from Microsoft - they have made it really easy to monitor presence. It's more difficult to CHANGE presence - but I'm not so worried about that right now."

With information Paul sent me I was able to write a tutorial on controlling Asterisk based on an OCS user's presence. Credit goes to Paul for this tutorial.

Controlling Asterisk based on an OCS 2007 User's Presence Tutorial

This tutorial, although very basic, demonstrates is that it's easy to add 'OCS presence' awareness to desktop apps written in Visual Studio. Then you can control Asterisk using Asterisk.NET.

This is not intended to be a professional, server-based solution (because it uses the desktop Office Communicator client) - there is other MS development API's for OCS server interaction. Still, this provides some powerful presence integration with the popular Asterisk and OCS 2007 platforms.

Microsoft's Office Communication Server 2007 does not include call queuing, (it is believed call queuing shall be included in R2 of OCS 2007 - but how it will function or what it will offer is not yet clear).

Asterisk can provide call queuing - but Asterisk is not aware of the presence of a user in OCS. So how do we control Asterisk to pass calls to OCS users based on their presence?

Tools needed:
- Microsoft's C# - there is a free version of C# called the Express Version which maybe suitable for this - Visual Studio Professional 2005 or later works just as well.
- Asterisk.NET (1.4.0.1) - http://sourceforge.net/projects/asterisk-dotnet
- Microsoft Office Communicator 2007 SDK 
- Office Communicator Presence Controls

You can 'tweak' the example app provided with Asterisk.NET & demonstrate this can be done.

"Preparing your Persona"
Install the "Office Communicator Presence Controls".
Now open "Program Files\ Microsoft Office Communicator 2007 Presence Controls".

Here you will find a readme.doc. Pages 7 & 8 tells you how to compile the managed control - which will produce "PresenceControls.dll".

Remember where this dll is - we'll need it later. Close this project.

"Check Asterisk.NET works with your Asterisk server"

On your Asterisk server, edit your extensions.conf file and add these 2 lines in any context you wish to use:
exten => 200,1,agi(agi://<ip address of dev PC in here>/customivr)
exten => 200,2,Hangup()


Open Asterisk.NET in Visual Studio - once loaded - you should see two projects inside it:

Asterisk.NET & Asterisk.NET.Test

Under the Asterisk.NET.Test project - view the code in "Program.cs". Update the IP address & login credentials to match your Asterisk server.

From the Solution Explorer panel, right-click on the Asterisk.2005 solution - and choose Rebuild. When it finishes - navigate in Windows to the "bin\Release" subfolder under the Asterisk.NET folder. Run the Asterisk.NET.Test.exe

You should see a command window running the test app. You should now be able to use a softphone to connect to Asterisk - and dial 200. If you are watching the Asterisk console - you should be able to see Asterisk receiving instructions from the test app running on your desktop.

"Amend the Asterisk.NET test app to react to OCS presence"

Return to the Asterisk.NET solution in Visual Studio. Right click on Asterisk.NET.Test - choose Add...- and from the sub-menu, choose Windows Form... I shall leave the name of the form as default - Form1.cs

Look at Form1.cs in the Designer View (not code view). Right-click anywhere in the toolbox panel & select "Choose Items..." You should see this window...
visual-studio-toolbox-items.jpg


Press "Browse...". Now find the "PresenceControls.dll" from earlier. This will add the following two controls to the toolbox.
personalist.jpg

The "Persona" control is to monitor one user, and the "PersonaList" monitors multiple users.

Drag a "Persona" control onto your form. Now change the "Modifiers" property of the "Persona1" control to Public.

Now change to the code view for Form1.cs. Immediately after the InitalizeComponent line, I added a line to assign a user to the persona control - as below:

InitializeComponent();
persona1.SipUri = "<username>@<domainname.com>";


Once assigned a SipUri - the Persona control will register with the OCS server for that user - and continue to receive updates from the OCS server whenever the presence changes for that user.

You do have to have Office Communicator installed for the persona control to work - but the assigned user can be any user that you can detect presence for. Basically - if you have the permissions to add a user to your contacts in the Office Communicator (OC) client - you can monitor the presence of that user here.

Move to the code view of Program.cs - and change line 29 to exclude the checkManagerAPI(); command - like this below:

// checkManagerAPI();
checkFastAGI();

Move to the code view of CustomIVR.cs - here is the code controlling Asterisk when you dial the extension numbered 200.

Approx line 40 - after the answer command, add a new line to create an instance of the form1 we created.

Form1 testform = new Form1();

The form contains the persona control for our user.

We then use the persona control within the IVR code to control the call flow based on the presence of that user. "TextStatus" from the Persona control will give us a text response of the status of that user.

Use this line to display to the console the status of the user:

Console.WriteLine(testform.persona1.TextStatus);

Then use this code anywhere within the IVR code to control call flow based on the presence of the user in OCS.

if (testform.persona1.TextStatus == "Busy" || testform.persona1.TextStatus == "On the Phone")
{
<runs some code in here if the user is busy or one the phone>
}


Note that within OCS the "On the Phone" status is used when a user is on the phone (doh!) - but from an OC client it appears their status is set to "Busy".

If a user is not logged in to OCS - the presence is set to "Unknown". It also shows the text from the four OC client custom presence states if you have used them.

Rebuild the Asterisk.2005 solution - and run the Asterisk.NET.Test.exe again. Now when you call 200 from a Asterisk connected softphone - you should see the presence of the OCS user you are monitoring appear on the console of the test app each time the IVR menu 'loops'.

To help with troubleshooting - this is the contents of the "Release" directory when finshed.
asterisk-net-release-directory.jpg

Note: Make sure you have you need RTMPLTFM.dll & Uccp.dll in the working directory.

Happy OCS 2007 presence integration with Asterisk! If you try it, let me know how it goes.


Related Articles to 'Controlling Asterisk based on Microsoft OCS 2007 user's presence'
webrtc2sip-click-to-call.png

fonality-co-founders.jpg

Featured Videos
Featured Events