Search code examples
restpostweblynclync-2013

Is it possible to have Lync communicate with a REST API?


I have created a basic REST API where a user can ask for an acronym, and the web-page will return the meaning of the acronym via a POST call.

The majority of my end-users don't use the Internet as much as they use the Microsoft Lync application.

Is it possible for me to create a Lync account, and have it pass questions to my API, and return the answers to the user? Meaning the user just needs to open a new chat in Lync rather than a new web-page.

I'm sure this is possible, but I can't find any information on Google or on the web. How can this be accomplished?

Thanks very much.

Edit :

Adding a bounty in the hopes of someone creating a simple example as I believe it would be very useful for a large number of devs :).


Solution

  • To elaborate on Tom Morgan's answer, it would be easy to create an UCMA application for this.

    Create an UCMA application

    Now this doesn't have to be complicated. Since all you want is to receive an InstantMessage and reply to it, you don't need the full power of a trusted application. My choice would be to use a simple UserEndpoint. As luck would have it, Tom has a good example of that online: Simplest example using UCMA UserEndpoint to send an IM.

    Make it listen to incoming messages

    Whereas the sample app sends a message when it is connected, we need to listen to messages. On the UserEndpoint, set a message handler for instant messages:

    endpoint.RegisterForIncomingCall<InstantMessagingCall>(HandleInstantMessagingCall);
    
    private void HandleInstantMessagingCall(object sender, CallReceivedEventArgs<InstantMessagingCall> e)
    {
        // We need the flow to be able to send/receive messages.
        e.Call.InstantMessagingFlowConfigurationRequested += HandleInstantMessagingFlowConfigurationRequested;
        // And the message should be accepted.
        e.Call.BeginAccept(ar => {
            e.Call.EndAccept(ar);
    
            // Grab and handle the toast message here.
    
        }, null);
    }
    

    Process the message

    There is a little complication here, your first message can be in the 'toast' of the new message argument, or arrive later on the message stream (the flow).

    Dealing with the Toast message

    The toast message is part of the conversation setup, but it can be null or not a text message.

     if (e.ToastMessage != null && e.ToastMessage.HasTextMessage)
     {
          var message = e.ToastMessage.Message;
    
          // Here message is whatever initial text the 
          // other party send you.
    
          // Send it to your Acronym webservice and 
          // respond on the message flow, see the flow
          // handler below.
     }
    

    Dealing with the flow

    Your message flow is where the actual data is passed around. Get a handle on the flow and store it, because it's needed later to send messages.

    private void HandleHandleInstantMessagingFlowConfigurationRequested(object sender, InstantMessagingFlowConfigurationRequestedEventArgs e)
    {
        // Grab your flow here, and store it somewhere.
        var flow = e.Flow;
        // Handle incoming messages
        flow.MessageReceived += HandleMessageReceived;
    }
    

    And create a message handler to deal with incoming messages:

    private void HandleMessageReceived(object sender, InstantMessageReceivedEventArgs e)
    {
        if (e.HasTextBody)
        {
            var message = e.TextBody;
    
            // Send it to your Acronym webservice and respond 
            // on the message flow.
    
            flow.BeginSendInstantMessage(
                "Your response", 
                ar => { flow.EndSendInstantMessage(ar); }, 
                null);
        }
    }
    

    That would about sum it up for the most basic example of sending/receiving messages. Let me know if any parts of this need more clarification, I can add to the answer where needed.

    I created a Gist with a full solution. Sadly it is not tested because I'm currently not near a Lync development environment. See UCMA UserEndpoint replying to IM Example.cs.