要详细说明Tom Morgan 的答案,为此创建一个UCMA 应用程序很容易。
创建一个 UCMA 应用程序
现在这不必复杂。由于您只想接收即时消息并回复它,因此您不需要受信任应用程序的全部功能。我的选择是使用简单的UserEndpoint。幸运的是,汤姆在网上有一个很好的例子:Simplest example using UCMA UserEndpoint to send an IM。
让它监听传入的消息
虽然示例应用在连接时会发送消息,但我们需要收听消息。在UserEndpoint 上,为即时消息设置消息处理程序:
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);
}
处理消息
这里有点复杂,您的第一条消息可以在新消息参数的“toast”中,或者稍后到达消息流(流)。
处理 Toast 消息
toast 消息是对话设置的一部分,但它可以为 null 或不是文本消息。
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.
}
处理流程
您的消息流是传递实际数据的地方。获取流的句柄并存储它,因为稍后需要它来发送消息。
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;
}
并创建一个消息处理程序来处理传入的消息:
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);
}
}
这将概括为发送/接收消息的最基本示例。让我知道是否有任何部分需要进一步澄清,我可以在需要的地方添加到答案中。
我创建了一个包含完整解决方案的 Gist。遗憾的是,它没有经过测试,因为我目前不在 Lync 开发环境附近。见UCMA UserEndpoint replying to IM Example.cs。