Using Speech Recognition in UCMA 3.0 and Lync 2010: Lync Application (Part 4 of 5)

Summary:   This is the fourth in a series of five articles that describe how a Microsoft Unified Communications Managed API (UCMA) 3.0 application and a Microsoft Lync 2010 application can be combined to perform speech recognition. Part 4 describes essential parts of the Lync 2010 application.

Applies to:   Microsoft Lync Server 2010 | Microsoft Unified Communications Managed API 3.0 Core SDK | Microsoft Lync 2010

Published:   April 2011 | Provided by:   Mark Parker, Microsoft | About the Author

Contents

  • The Lync Application

  • Lync Application Requirements

  • Sending and Receiving Data in the Lync Application

  • Part 5

  • Additional Resources

Code Gallery  Download code

This is the fourth in a five-part series of articles that describe how to incorporate speech recognition in Lync 2010 applications that interoperate with UCMA 3.0.

The Lync Application

The Lync 2010 application is a Microsoft Silverlight-based application that is embedded in a webpage. The scenario that is described in Using Speech Recognition in UCMA 3.0 and Lync 2010: Scenario Overview (Part 1 of 5) works equally well with a Microsoft Lync Controls Windows Presentation Foundation (WPF) desktop application. In the scenario implemented in this series of articles, the UCMA 3.0 application starts the web session by opening the Lync Conversation Window Extension (CWE), which makes the UI available to the Lync 2010 user. The UCMA 3.0 application sends a conversation invitation to the user who is signed in to Lync 2010 on a separate computer, and then the Lync 2010 application opens in the CWE.

This article does not describe the steps that are needed to embed a Lync 2010 Silverlight application in a webpage. For more information, see Walkthrough: Write a Silverlight Application for the Extensibility Window.

Note

To see all of the UCMA and Lync code that is used in the scenario, see Using Speech Recognition in UCMA 3.0 and Lync 2010: Code Walkthrough (Part 5 of 5).

Lync Application Requirements

Microsoft Visual Studio development system and Microsoft Lync 2010 SDK is required to develop the Lync 2010 side of the application. To develop a Lync 2010 Silverlight application you will need, in addition, the Microsoft Silverlight 4 SDK and runtime. For more information about prerequisites, see Walkthrough: Presence Hello World.

To deploy the Lync 2010 application, you must register it on the Lync 2010 computer that will be communicating with the UCMA 3.0 computer. For information about registering Lync 2010 applications for contextual communications, see Register Contextual Conversation Packages.

Important

The Lync 2010 application runs on the Lync 2010 computer, but does not run on the UCMA 3.0 computer. The application ID should be registered on the Lync 2010 computer and must be known to the UCMA 3.0 computer.

Sending and Receiving Data in the Lync Application

Three important Lync 2010 application run-time tasks appear in the following list.

  • Receive the data that is sent by the UCMA 3.0 application when the context channel is established. This action changes the channel status of the form to “Ready”, which indicates that the Lync 2010 user can specify his or her preferences for a flight.

    This task is performed by the Initialize method.

  • Receive the data that is sent by the UCMA 3.0 application on the context channel. The data consists of the recognized semantic items and the ticket price.

    This task is performed by the handler for the ContextDataReceived event. When this event is raised, the handler for this event is invoked.

  • Send data to the UCMA 3.0 application on the context channel, acknowledging the recognized semantic items, and closing the Lync 2010 application.

    This task is performed by the handler for the Click() event. When the Exit button on the form is clicked, the Click event is raised, and its handler is invoked.

The Initialize method and the handlers for the ContextDataReceived and Click events appear in the following sections.

Initialize Method

The Initialize method appears in the following example. The Initialize method calls the GetHostingConversation() method to get the hosting conversation that was created by the UCMA 3.0 application, and then registers handlers for the specified events. The handler then calls the GetApplicationData method. The BeginEstablish method in the UCMA 3.0 application sends the string “Context channel is open.”, so if the string returned by GetApplicationData method contains “open”, the Lync 2010 application updates the channel status in the user interface to “Ready”.

// Get the hosting Conversation object and application data, register for event notification,
// and update the user interface to “Ready”.
private void Initialize()
{
  String appData;
  try
  {
    _conversation = (Conversation)Microsoft.Lync.Model.LyncClient.GetHostingConversation();
  }

  catch (LyncClientException ex)
  {
    Logger("LyncClientException error: " + ex);
  }

  catch (Exception ex)
  {
    Logger("Other conversation initialization error: " + ex);
  }

  _conversation.ContextDataReceived += OnContextDataReceived;
  _conversation.ContextDataSent += OnContextDataSent;
  _conversation.InitialContextReceived += OnInitialContextReceived;
  _conversationWindow = _automation.GetConversationWindow(_conversation);

  appData = _conversation.GetApplicationData(_appId);
  Logger("Application data: " + appData);
  if (appData.Contains("open"))
  {
    channelStatus.Foreground = new SolidColorBrush(Colors.Green);
    channelStatus.Text = "Ready";
  }
}

ContextDataReceived Event Handler

The ContextDataReceived event is raised when the UCMA 3.0 application calls the BeginSendData method. When the handler for the ContextDataReceived event is invoked, it processes the string sent from the UCMA application. This string contains the semantic information about the origination and destination cities, in addition to the ticket price. The data in this string is separated by semicolons.

The following example shows the implementation of the ContextDataReceived event handler.

// Handler for the ContextDataReceived event on the Conversation object.
// This handler splits the string in args.ContextData into three substrings.
// Semicolons divide the three substrings.
public void OnContextDataReceived(object sender, ContextEventArgs args)
{
  if ( (args != null) && (args.ContextData.Length != 0) )
  {
    // Split the ContextData string at the semicolons.
    string str = args.ContextData;
    string[] substr = str.Split(new char[] { ';' });

    // Populate the three data boxes.
    fltOrigination.Text = substr[0];
    fltDestination.Text = substr[1];
    fltCost.Text = substr[2];

    Logger("OnContextDataReceived: str = " + args.ContextData + "\nConversation ID = " + ((Conversation)sender).Properties[ConversationProperty.Id].ToString());
  }
}

Click Event Handler

The handler for the Click event is invoked when the Lync 2010 user clicks the Exit button. The handler calls the BeginSendContextData method, passing the application ID, the MIME type for the data, the string to be sent, the name of the callback method (SendAdditionalDataCallback), and an optional parameter that is not used in this example. The code for the SendAdditionalDataCallback callback appears after the following example. After calling the BeginSendContextData method, the handler for the Click event updates the form.

// Handler for the Click event on the Exit button.
private void SendAdditionalData_Click(object sender, RoutedEventArgs e)
{
  try
  {
    Logger("Sending additional context");
    
    _conversation.BeginSendContextData(AppId, @"plain/text", "exit", SendAdditionalDataCallBack, null);
    channelStatus.Foreground = new SolidColorBrush(Colors.Red);
    channelStatus.Text = "Closed";
    fltOrigination.Text = "";
    fltDestination.Text = "";
    fltCost.Text = "";
  }

  catch (InvalidOperationException ex)
  {
    Logger("Invalid operation in SendAdditionalData handler: " + ex.ToString());
  }

  catch (Exception ex)
  {
    Logger("Other SendAdditionalData error: " + ex.ToString());
  }
}

The following example defines the BeginSendContextData callback method. BeginSendContextData calls the EndSendContextData method, and then closes the Lync Conversation Window Extension (CWE).

// Callback for the BeginSendContextData method.
private void SendAdditionalDataCallBack(IAsyncResult asyncResult)
{
  try
  {
    if (asyncResult.IsCompleted)
    {
      _conversation.EndSendContextData(asyncResult);
      
      Logger("Additional context sent successfully.");
      // Close the CWE.
      _conversationWindow.CloseExtensibilityWindow(AppId);
    }
    else
    {
      Logger("Could not send additional context: " + asyncResult.AsyncState);
    }
  }
  catch (Exception ex)
  {
    Logger("SendAdditionalDataCallBack error: " + ex);
  }
}

Part 5

Using Speech Recognition in UCMA 3.0 and Lync 2010: Code Walkthrough (Part 5 of 5)

Additional Resources

For more information, see the following resources:

About the Author

Mark Parker is a programming writer at Microsoft whose current responsibility is the UCMA SDK documentation. Mark previously worked on the Microsoft Speech Server 2007 documentation.