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

4

16

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 :).

Cash answered 24/3, 2015 at 15:14 Comment(0)
I
4

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.

Irvine answered 21/4, 2015 at 14:3 Comment(1)
@Willen Duncan thank you very much for this response, I will use it to try and create a solution over the weekend - appreciate it ! :)Cash
S
5

Yep, absolutely. UCMA (the Unified Communications Managed API) would be my choice of API to use here, and a good place to start - UCMA apps are "normal" .net applications, but also expose an application endpoint, which can be added to a user's contact list. When users send messages, that can trigger events in your application so you can take the incoming IM, do the acronym translation and return the full wording.

I have a bunch of blog posts about UCMA, but as of yet no defined collection of "useful" posts to work through, but coming soon! In the meantime, feel free to browse the list.

-tom

Severance answered 25/3, 2015 at 17:0 Comment(0)
I
4

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.

Irvine answered 21/4, 2015 at 14:3 Comment(1)
@Willen Duncan thank you very much for this response, I will use it to try and create a solution over the weekend - appreciate it ! :)Cash
L
0

I never used Lync but while I was looking at the dev doc, I stumble upon a sample which could be what you're looking for.

Lync 2013: Filter room messages before they are posted

Once you have filtered the messages, you just need to catch the acronym and call your custom code that calls your API.

Unless I'm missing something, I think you could do it with a simple GET request as well. Just call your API like this yoursite.com/api/acronym/[the_acronym_here].

Lobbyist answered 31/3, 2015 at 1:39 Comment(0)
G
0

You can use UCWA (Microsoft Unified Communications Web API),is a REST API.For detail , can reference as the following..

https://ucwa.lync.com/documentation/what-is-lync-ucwa-api

Googolplex answered 14/4, 2015 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.